简体   繁体   English

如何在docker-compose中缓存构建环境

[英]how to cache build env in docker-compose

I am new to using docker-compose so I am not sure if I am using the correct approach to building changes in code. 我是使用docker-compose新手,所以我不确定是否使用正确的方法来构建代码更改。 The docker-compose tutorial said that in order to update any changes in code, just make changes and refresh the link and the output will be refreshed(after code gets built by itself). docker-compose教程说,为了更新代码中的任何更改,只需进行更改并刷新链接,输出就会被刷新(在代码自行构建之后)。 That isn't happening for me. 那对我来说不是这样。 I have to stop the running docker-compose and then run the command docker-compose up --build which builds the system again. 我必须停止正在运行的docker-compose,然后运行命令docker-compose up --build来再次构建系统。 Now, my problem is that it downloads all the requirements in the python project. 现在,我的问题是它下载了python项目中的所有requirements Currently the libraries aren't that many but they will increase at which point it will be extremely time consuming to download everything again and again. 当前的库并不多,但是它们会增加,这时一次又一次下载所有内容将非常耗时。 What should I do to avoid doing this when I only make changes to code and not the requirements.txt file. 当我仅更改代码而不更改requirements.txt文件时,应该怎么做以避免这样做。

Here is an example of my setup: 这是我的设置示例:
docker-compose.yml 泊坞窗,compose.yml

version: '2'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    env_file: .env
    depends_on:
      - db
    volumes:
      - ./webapp:/opt/webapp

  db:
    image: postgres:latest
    ports:
      - "5432:5432"

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

Dockerfile: Dockerfile:

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

For every change I make in code and test it, I have to go through all these steps: 对于我在代码中进行的每项更改并进行测试,我都必须完成所有这些步骤:

Building web
Step 1/5 : FROM python:3.4-alpine
 ---> 765c483d587c
Step 2/5 : ADD . /code
 ---> ce6f587fe477
Removing intermediate container bceef7c06a89
Step 3/5 : WORKDIR /code
 ---> cd3f4951f718
Removing intermediate container 5022780e47eb
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 2dfe61c2537e
Collecting flask (from -r requirements.txt (line 1))
  Downloading Flask-0.12-py2.py3-none-any.whl (82kB)
Collecting redis (from -r requirements.txt (line 2))
  Downloading redis-2.10.5-py2.py3-none-any.whl (60kB)
Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1))
  Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting click>=2.0 (from flask->-r requirements.txt (line 1))
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1))
  Downloading Werkzeug-0.11.15-py2.py3-none-any.whl (307kB)
Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1))
  Downloading Jinja2-2.9.5-py2.py3-none-any.whl (340kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask->-r requirements.txt (line 1))
  Downloading MarkupSafe-0.23.tar.gz
Installing collected packages: itsdangerous, click, Werkzeug, MarkupSafe, Jinja2, flask, redis
  Running setup.py install for itsdangerous: started
    Running setup.py install for itsdangerous: finished with status 'done'
  Running setup.py install for MarkupSafe: started
    Running setup.py install for MarkupSafe: finished with status 'done'
Successfully installed Jinja2-2.9.5 MarkupSafe-0.23 Werkzeug-0.11.15 click-6.7 flask-0.12 itsdangerous-0.24 redis-2.10.5
 ---> 6adce98b4bd0
Removing intermediate container 2dfe61c2537e
Step 5/5 : CMD python app.py
 ---> Running in 554d0f0d4635
 ---> c28028d2c0de
Removing intermediate container 554d0f0d4635
Successfully built c28028d2c0de
Starting dockercompose_redis_1
Starting dockercompose_db_1
Recreating dockercompose_web_1
Attaching to dockercompose_db_1, dockercompose_redis_1, dockercompose_web_1

I found my answer here . 我在这里找到了答案。

I had to change these lines : 我不得不更改这些行:

volumes:
  - ./webapp:/opt/webapp

to : 至 :

volumes:
  - .:/code

since this directory is the one that is mentioned in my Dockerfile. 因为此目录是我的Dockerfile中提到的目录。 I could also change my Dockerfile instead. 我也可以改为更改Dockerfile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM