題 如何讓Docker寫入本地目錄?


我正試圖設置一個w((django擴展, http://wagtail.io/)網站使用Docker,它似乎已經工作,但據我所知,它已經隱藏了我的網站遠離我在Docker的內部,我實際上無法使用它。

我的目錄設置如下:

dir
|- docker-compose.yml
|- web
    |- Dockerfile
    |- requirements.txt

我的docker-compose.yml

version: '2'
services:
  db:
    restart: always
    image: postgres
  web:
    build: ./web
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    restart: always

我的Dockerfile:

FROM python:3.5
MAINTAINER

RUN apt-get update -y

ENV PYTHONBUFFERED 1

RUN mkdir /code
WORKDIR /code

ADD requirements.txt /code/

RUN pip3 install -r requirements.txt

ADD . /code/

當我跑它時......

$docker-compose run web wagtail start site .
Building web
Step 1 : FROM python:3.5
 ---> 64869b3b2083
Step 2 : MAINTAINER 
 ---> Using cache
 ---> 2a539247021e
Step 3 : RUN apt-get update -y
 ---> Using cache
 ---> 6d1094098071
Step 4 : ENV PYTHONBUFFERED 1
 ---> Using cache
 ---> 12a153c417cd
Step 5 : RUN mkdir /code
 ---> Using cache
 ---> 4cc21041c1c8
Step 6 : WORKDIR /code
 ---> Using cache
 ---> eaca96927442
Step 7 : ADD requirements.txt /code/
 ---> Using cache
 ---> 2f1977d72542
Step 8 : RUN pip3 install -r requirements.txt
 ---> Using cache
 ---> a73e7640aa35
Step 9 : ADD . /code/
 ---> 31bd3c0432cd
Removing intermediate container 66e4fa5b074f
Successfully built 31bd3c0432cd
Creating a wagtail project called site
CommandError: /code/requirements.txt already exists, overlaying a project or app into an existing directory won't replace conflicting files

(我第一次跑它,它“完美地工作”。)

這些神秘文件在哪裡創建,如何清除它們並使其與我可以實際查看/編輯的目錄一起使用?


編輯:這種方法基於Docker文檔中的Django示例(https://docs.docker.com/compose/django/)。我試過這個例子(逐字使用文件),我遇到了同樣的問題:

$ docker-compose run web django-admin.py startproject composeexample .
Creating network "dockerdjangotest_default" with the default driver
Creating dockerdjangotest_db_1
Building web
Step 1 : FROM python:2.7
2.7: Pulling from library/python
efd26ecc9548: Already exists
a3ed95caeb02: Pull complete
d1784d73276e: Already exists
72e581645fc3: Already exists
9709ddcc4d24: Already exists
2d600f0ec235: Already exists
de1491f1bbd4: Pull complete
4becc568d2ff: Pull complete
Digest: sha256:ee46fc8b96ed0d1f1af71d0859ce6ba5f9be3b587f5a608193599b48d0a157c2
Status: Downloaded newer image for python:2.7
 ---> a3b29970a425
Step 2 : ENV PYTHONUNBUFFERED 1
 ---> Running in 35a6721182b3
 ---> d8424740fce4
Removing intermediate container 35a6721182b3
Step 3 : RUN mkdir /code
 ---> Running in d1c7e92821ab
 ---> 1c0b1da9e9d3
Removing intermediate container d1c7e92821ab
Step 4 : WORKDIR /code
 ---> Running in 120bb2d0e5dd
 ---> 72725e994f0e
Removing intermediate container 120bb2d0e5dd
Step 5 : ADD requirements.txt /code/
 ---> ef891d60690b
Removing intermediate container 862a5e314f2f
Step 6 : RUN pip install -r requirements.txt
 ---> Running in f51b2bc2ca48
Collecting Django (from -r requirements.txt (line 1))
  Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB)
Collecting psycopg2 (from -r requirements.txt (line 2))
  Downloading psycopg2-2.6.1.tar.gz (371kB)
Building wheels for collected packages: psycopg2
  Running setup.py bdist_wheel for psycopg2: started
  Running setup.py bdist_wheel for psycopg2: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/e2/9a/5e/7b620848bbc7cfb9084aafea077be11618c2b5067bd532f329
Successfully built psycopg2
Installing collected packages: Django, psycopg2
Successfully installed Django-1.9.5 psycopg2-2.6.1
 ---> 349d20d9823e
Removing intermediate container f51b2bc2ca48
Step 7 : ADD . /code/
 ---> 38190d0f4c65
Removing intermediate container 0da1169b5cfa
Successfully built 38190d0f4c65

但不是 composeexample 目錄已經創建(與文檔說這個目錄現在應該是什麼樣子相反):

$ ls -l
total 24
-rw-r--r--  1 user  wheel  146 Apr 17 17:50 Dockerfile
-rw-r--r--  1 user  wheel  209 Apr 17 17:51 docker-compose.yml
-rw-r--r--  1 user  wheel   16 Apr 17 17:50 requirements.txt

但碼頭工人似乎仍然確信一切正常;所以那些文件......在某個地方?

$ docker-compose run web django-admin.py startproject composeexample .
CommandError: /code/manage.py already exists, overlaying a project or app into an existing directory won't replace conflicting files

3
2018-04-17 08:50


起源


您可以使用“docker inspect”查看捲和根目錄的實際位置的路徑,但您可能不希望直接修改這些目錄,因為它們由docker管理。 - Lie Ryan


答案:


要訪問這些文件,您需要:


1
2018-04-18 16:21



好吧,好吧,但原則上有辦法讓文件出現在容器外面,是嗎?根據教程,它應該將scaffolded django應用程序放在項目目錄(docker之外)的新文件夾中,這是我想要的,而不是僅通過進入容器才能訪問它。 - futuraprime
正如所建議的那樣,一旦啟動帶有附加卷的容器,容器就可以隨意讀/寫 - Scott Stensland
容器可能能夠,但同樣,文件不能在容器外部訪問,文檔建議它們應該在容器外。本教程的下一行告訴我“在您的項目目錄中,編輯 composeexample/settings.py 文件。“我不能這樣做,因為上一步意味著將這些添加到我的項目目錄中,沒有。如果我自己創建它們,它沒有幫助,因為docker拋出錯誤而不是覆蓋容器內的文件與我的項目目錄中的文件。 - futuraprime
是個 volume 指令 docker-compose.yml 不同於 volumes 我已經在該文件中的行? - futuraprime