我有以下docker-compose.yml
文件,可以正常使用:
version: '2'
services:
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8001:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: dz01
depends_on:
- db
volumes:
- ./src:/src
expose:
- "8000"
db:
image: postgres:latest
container_name: pz01
ports:
- "5433:5432"
volumes:
- postgres_database:/var/lib/postgresql/data:Z
volumes:
postgres_database:
external: true
问题是当我运行docker-compose up
时出现此错误:
python: can't open file 'manage.py': [Errno 2] No such file or directory
python: can't open file 'manage.py': [Errno 2] No such file or directory
[2018-10-26 12:41:59 +0000] [8] [INFO] Starting gunicorn 19.7.1
[2018-10-26 12:41:59 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
[2018-10-26 12:41:59 +0000] [8] [INFO] Using worker: sync
[2018-10-26 12:41:59 +0000] [12] [INFO] Booting worker with pid: 12
[2018-10-26 12:41:59 +0000] [12] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
worker.init_process()
File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python2.7/site-packages/gunicorn/util.py", line 352, in import_app
__import__(module)
ImportError: No module named C.wsgi
[2018-10-26 12:41:59 +0000] [12] [INFO] Worker exiting (pid: 12)
[2018-10-26 12:41:59 +0000] [8] [INFO] Shutting down: Master
[2018-10-26 12:41:59 +0000] [8] [INFO] Reason: Worker failed to boot.
我知道这是因为在我的src
文件夹中不存在django项目C
,但是我尝试使用以下脚本在dockerfile
内自动创建它:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
RUN django-admin startproject C
WORKDIR /C
CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn C.wsgi -b 0.0.0.0:8000
我首先执行docker-compose构建,并且没有错误:
docker-compose build
db uses an image, skipping
Building web
Step 1/10 : FROM python:2.7
---> 3c43a5d4034a
Step 2/10 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> da201dd581f7
Step 3/10 : RUN mkdir /src
---> Using cache
---> 30938986b6e0
Step 4/10 : RUN mkdir /static
---> Using cache
---> ccc265f2cb16
Step 5/10 : WORKDIR /src
---> Using cache
---> 9886c1215843
Step 6/10 : ADD ./src /src
---> Using cache
---> 4b4861dc6d8d
Step 7/10 : RUN pip install -r requirements.pip
---> Using cache
---> e57283657840
Step 8/10 : RUN django-admin startproject bmat
---> Running in 0b25e38d9edd
Removing intermediate container 0b25e38d9edd
---> 1b39be297aac
Step 9/10 : WORKDIR /bmat
Removing intermediate container 5ed83a067b98
---> 9fd466c2d795
Step 10/10 : CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn bmat.wsgi -b 0.0.0.0:8000
---> Running in 3beba0b212f0
Removing intermediate container 3beba0b212f0
---> d83eb0913d98
Successfully built d83eb0913d98
Successfully tagged bmat_web:latest
nginx uses an image, skipping
然后,我运行docker-compose up -d
,返回所有容器都已启动,但是当进入kinematic
时,我发现上面提到的这个问题,这是因为django-admin startproject C
没有运行。
有没有一个选项可以使其在构建Docker容器时自动运行?
最好的选择可能是使用dockerfile
的第一个版本而不对框架执行任何操作,构建所有容器,创建项目,然后手动修改dockerfile
以添加相应的迁移和collectstatic功能
谢谢
答案 0 :(得分:2)
我怀疑您更大的问题是Dockerfile
以以下结尾:
WORKDIR /C
CMD python manage.py ...
但是/C
中什么都没有,/src/C
中一切都是(我想)。试试吧
WORKDIR /src/C
另外,您的docker-compose.yml
文件说:
volumes:
- ./src:/src
将所有内容隐藏在容器/src
中,并用主机上./src
中的所有内容替换。特别是,这几乎取代了Dockerfile所做的一切。虽然这是足够普遍的开发设置(它避免了进行编辑和运行测试后重新运行docker build
的步骤),但这并不是您实际在生产环境中运行的(您希望能够在其中使用映像隔离,而无需在源树周围单独复制。
尝试删除这两行,并使用图像中实际存在的树的副本。
答案 1 :(得分:0)
最后,我可以使用它了。感谢@DavidMaze,我解决了第一个错误,但是并没有解决django项目的产生。
最终的脚本是:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
CMD django-admin.py startproject bmat; cd /src/bmat; python manage.py collectstatic --no-input;python manage.py migrate; gunicorn bmat.wsgi -b 0.0.0.0:8000
现在Run django-admin
语句不在Docker中运行,而是在正在运行的容器中使用CMD。