Docker Compose不复制任何文件

时间:2019-05-25 13:19:00

标签: django docker docker-compose

非常简单:以下docker-compose配置不允许运行后任何文件持久化。所以当我做docker exec -i -t aas-job-hunter_web_1 ls /app -alt时,什么也看不到。

以下是(无效的)最小示例:https://github.com/philastrophist/test-docker

我在Windows 10上,已允许安装驱动器并启用TLS连接。我不确定该怎么办。最让我感到困惑的是,requirements.txt被清楚地复制了(因为它已经全部安装了),但是当我看一看docker exec时并没有出现。

我的目录结构是:

parent/
    website/
        manage.py
        ...
    Dockerfile
    docker-compose.yml
    ...

Dockerfile:

FROM python:3.6

#WORKDIR /app

# By copying over requirements first, we make sure that Docker will cache
# our installed requirements rather than reinstall them on every build
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

# Now copy in our code, and run it
COPY . /app
EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000
# CMD tail -f /dev/null # use when testing

docker-compose.yml:

version: '2'
services:
  web:
    build: .
    ports:
     - "8000:8000"
    volumes:
     - .:/app
    links:
     - db

  db:
    image: "postgres:9.6"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: hunter2

跟踪:

> docker-compose -f docker-compose.yml up --build
Building web
Step 1/6 : FROM python:3.6
 ---> 0668df180a32
Step 2/6 : COPY requirements.txt /app/requirements.txt
 ---> Using cache
 ---> 3073d0bef876
Step 3/6 : RUN pip install -r /app/requirements.txt
 ---> Using cache
 ---> 8ad63bbb3de5
Step 4/6 : COPY . /app
 ---> 16390cdd6c2c
Step 5/6 : EXPOSE 8000
 ---> Running in f628000e8961
Removing intermediate container f628000e8961
 ---> 80e6994cfbd2
Step 6/6 : CMD python website/manage.py runserver 0.0.0.0:8000
 ---> Running in acb6b25eb558
Removing intermediate container acb6b25eb558
 ---> da8876d78103
Successfully built da8876d78103
Successfully tagged aas-job-hunter_web:latest
Starting aas-job-hunter_db_1 ... done
Recreating aas-job-hunter_web_1 ... done
Attaching to aas-job-hunter_db_1, aas-job-hunter_web_1
db_1   | LOG:  database system was shut down at 2019-05-24 21:23:31 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
web_1  | python: can't open file 'website/manage.py': [Errno 2] No such file or directory
aas-job-hunter_web_1 exited with code 2

3 个答案:

答案 0 :(得分:1)

实际上它会复制文件。

  1. 解决方案1 ​​

将CMD更改为:

CMD python /app/website/manage.py runserver 0.0.0.0:8000
  1. 解决方案2

您在创建/app文件夹之前调用WORKDIR。因此,将您的Dockerfile更改为:

FROM python:3.6.2

# By copying over requirements first, we make sure that Docker will cache
# our installed requirements rather than reinstall them on every build
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

# Now copy in our code, and run it
COPY . /app
WORKDIR /app
#EXPOSE 8000
CMD python ./website/manage.py runserver 0.0.0.0:8000
# CMD tail -f /dev/null # use when testing

然后调用它。

此外请记住,在当前的docker-compose文件中,您使用的是绑定安装,而不是卷,因此上下文.完全替换 {{1}的内容}。

答案 1 :(得分:0)

我认为COPY没有任何问题。但是,您需要将工作目录设置为/app,因为您的manage.py文件位于/app/website内,而不是Docker内部的/website中。

所以,我认为您的Dockerfile应该是这样的:

FROM python:3.6

RUN mkdir /app

COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

COPY . /app
WORKDIR /app

EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000

答案 2 :(得分:0)

取消注释#WORKDIR /app。 我还清理了其他部分,以进一步利用WORKDIR

FROM python:3.6
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000