Docker-compose不能反映Requirements.txt中的更改

时间:2019-02-23 19:25:53

标签: docker docker-compose requirements.txt

我的docker-compose -f docker-compose-dev.yml up -d 中的更改未在我运行时反映出来:

version: '3.6'

services:

  web:
    build:
      context: ./services/web
      dockerfile: Dockerfile-dev
    volumes:
      - './services/web:/usr/src/app'
    ports:
      - 5001:5000
    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:postgres@web-db:5432/web_dev 
      - DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/web_test  
    depends_on:  
      - web-db

  web-db:  
    build:
      context: ./services/web/project/db
      dockerfile: Dockerfile
    ports:
      - 5435:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  nginx:
    build:
      context: ./services/nginx
      dockerfile: Dockerfile-dev
    restart: always
    ports:
      - 80:80
    depends_on:
      - web
      - client

  client:
    build:
      context: ./services/client
      dockerfile: Dockerfile-dev
    volumes:
      - './services/client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - 3007:3000
    environment:
      - NODE_ENV=development
      - REACT_APP_WEB_SERVICE_URL=${REACT_APP_WEB_SERVICE_URL}
    depends_on:
      - web
  

docker-compose-dev.yml

# base image
FROM python:3.6-alpine

# install dependencies
RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add libffi-dev && \
    apk add postgresql-dev && \
    apk add netcat-openbsd && \
    apk add bind-tools && \
    apk add --update --no-cache g++ libxslt-dev && \
    apk add jpeg-dev zlib-dev

ENV PACKAGES="\
    dumb-init \
    musl \
    libc6-compat \
    linux-headers \
    build-base \
    bash \
    git \
    ca-certificates \
    freetype \
    libgfortran \
    libgcc \
    libstdc++ \
    openblas \
    tcl \
    tk \
    libssl1.0 \
    "

ENV PYTHON_PACKAGES="\
    numpy \
    matplotlib \
    scipy \
    scikit-learn \
    nltk \
    " 

RUN apk add --no-cache --virtual build-dependencies python3 \
    && apk add --virtual build-runtime \
    build-base python3-dev openblas-dev freetype-dev pkgconfig gfortran \
    && ln -s /usr/include/locale.h /usr/include/xlocale.h \
    && python3 -m ensurepip \
    && rm -r /usr/lib/python*/ensurepip \
    && pip3 install --upgrade pip setuptools \
    && ln -sf /usr/bin/python3 /usr/bin/python \
    && ln -sf pip3 /usr/bin/pip \
    && rm -r /root/.cache \
    && pip install --no-cache-dir $PYTHON_PACKAGES \
    && pip3 install 'pandas<0.21.0' \
    && apk del build-runtime \
    && apk add --no-cache --virtual build-dependencies $PACKAGES \
    && rm -rf /var/cache/apk/*

# set working directory
WORKDIR /usr/src/app

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt  # <--- refer to EDIT
RUN pip install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["/usr/src/app/entrypoint.sh"]
  

Dockerfile-dev

requirements.txt

我想念什么?

编辑

就像[Docker how to run pip requirements.txt only if there was a change?中接受的答案一样,在将整个应用程序添加到图像中之前,我已经在单独的构建步骤中复制了{{1}}文件,但是它似乎不起作用。

1 个答案:

答案 0 :(得分:1)

尝试从复制的文件安装要求

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

这是其 Dockerfile

的一个示例
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt

这就是你拥有的

RUN pip install -r requirements.txt

然后,在更改了docker文件之后,您必须停止容器,删除映像,构建新映像并从中运行容器。

停止容器并删除图像。

docker-compose down
docker-compose --rmi all

--rmi all-删除所有图像。您可能要使用--rmi IMAGE_NAME

并启动它(如果不使用默认参数,请使用参数更改这些命令)。

docker-compose up

更新

如果您正在运行docker并且不想停止它并重建映像(如果您只想安装软件包或运行某些命令甚至启动新应用程序),则可以从本地连接该容器机器和运行命令行命令。

docker exec -it [CONTAINER_ID] bash

要获取[CONTAINER_ID],请运行

docker ps

注意docker-compose ps将为您提供容器名称,但是您需要使用容器ID来对容器进行SSH。