[英]When run docker-compose up I get python: can't open file 'manage.py': [Errno 2] No such file or directory
This is my Dockerfile:这是我的 Dockerfile:
FROM python:3.6.1
# set working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# add requirements (to leverage Docker cache)
ADD ./requirements.txt /usr/src/app/requirements.txt
# install requirements
RUN pip install -r requirements.txt
# add app
ADD . /usr/src/app
# run server
CMD python manage.py runserver -h 0.0.0.0
This is my docker-compose.yml:这是我的 docker-compose.yml:
version: '2.1'
services:
users-service:
container_name: users-service
build: .
volumes:
- .:/usr/src/app
ports:
- 5001:5000 # expose ports - HOST:CONTAINER
This is structure of my project dir (from which I run my docker commands):这是我的项目目录的结构(我从中运行我的 docker 命令):
├── docker-compose.yml
├── Dockerfile
├── env
│ ├── bin
│ ├── include
│ ├── lib
│ ├── lib64 -> lib
│ ├── pip-selfcheck.json
│ ├── pyvenv.cfg
│ └── share
├── manage.py
├── project
│ ├── config.py
│ ├── __init__.py
│ └── __pycache__
└── requirements.txt
First I run docker-compose build and get following output:首先我运行docker-compose build并得到以下输出:
Building users-service
Step 1/7 : FROM python:3.6.1
---> 74145628c331
Step 2/7 : RUN mkdir -p /usr/src/app
---> Using cache
---> 8b73b9540da2
Step 3/7 : WORKDIR /usr/src/app
---> Using cache
---> 28d3452f6021
Step 4/7 : ADD ./requirements.txt /usr/src/app/requirements.txt
---> Using cache
---> e92c334820c2
Step 5/7 : RUN pip install -r requirements.txt
---> Using cache
---> db7ea8211cd1
Step 6/7 : ADD . /usr/src/app
---> 472c303e4297
Removing intermediate container 7ee1b497cde4
Step 7/7 : CMD python manage.py runserver -h 0.0.0.0
---> Running in 31ae97876314
---> 42c79e68f692
Removing intermediate container 31ae97876314
Successfully built 42c79e68f692
Successfully tagged flaskmicroservicesusers_users-service:latest
And then I run docker-compose up which return error:然后我运行docker-compose up ,它返回错误:
Recreating users-service
Attaching to users-service
users-service | python: can't open file 'manage.py': [Errno 2] No such file or directory
users-service exited with code 2
What I am missing here?我在这里缺少什么?
Remove volumes from docker-compose.yml and build again.从 docker-compose.yml 中删除卷并重新构建。
version: '2.1'
services:
users-service:
container_name: users-service
build: .
ports:
- 5001:5000 # expose ports - HOST:CONTAINER
there is an issue with docker-compose on ubuntu. ubuntu 上的 docker-compose 存在问题。 it can't mount the volume.
它无法安装卷。 And as I can see you want to mount
.
正如我所看到的,你想安装
.
to /usr/src/app
到
/usr/src/app
You would need to build the image again after you update the code.更新代码后,您需要再次构建映像。
不要通过运行此eval $(docker-machine env <your-vm>
在 docker-machine vm 中运行您的应用程序。但如果有,请通过运行eval $(docker-machine env -u)
取消链接 docker-machine
As @Fartash said, there is a problem with mounting the folder as volume in ubuntu.正如@Fartash 所说,在 ubuntu 中将文件夹安装为卷存在问题。 In order to resolve the error you can use
named volume
in docker.为了解决错误,您可以在 docker 中使用
named volume
。
You can simply defining new named volume in docker-compose
file as below:您可以简单地在
docker-compose
文件中定义新的命名卷,如下所示:
version: '2.1'
services:
users-service:
container_name: users-service
build: .
volumes:
- app:/usr/src/app # the volume is defined at the end of this file
ports:
- 5001:5000
volumes: # You should add this and the following line
app: # you can define any name
for more information, you can check out this link.有关更多信息,您可以查看此链接。
I ran into a similar problem using docker-machine with a virtualbox driver on Ubuntu.我在 Ubuntu 上使用带有 virtualbox 驱动程序的 docker-machine 遇到了类似的问题。 It was due to my project's directory not being shared with the docker-machine.
这是由于我的项目目录没有与 docker-machine 共享。 To fix it,
cd
into your project root and specify the following flag when creating your environment: --virtualbox-share-folder=="$PWD:$PWD"
.要修复它,请
cd
进入您的项目根目录并在创建环境时指定以下标志: --virtualbox-share-folder=="$PWD:$PWD"
。
TLDR: I did not have any issues with ubuntu and docker-compose
. TLDR:我对 ubuntu 和
docker-compose
没有任何问题。 It is possible that there were problems with ubuntu images previously, however, I did not find that to be accurate.以前的 ubuntu 图像可能存在问题,但是,我认为这并不准确。 Additionally, if you're using a
virtual environment
in a docker container, you may need to specifically tell docker-compose
not to look for your virtual environment file on the host.此外,如果您在 docker 容器中使用
virtual environment
,您可能需要明确告诉docker-compose
不要在主机上查找您的虚拟环境文件。
I am posting this under this question specifically because of the comments and answers about ubuntu not working correctly with docker-compose
bind mounts.我之所以在此问题下发布此问题,是因为有关 ubuntu 无法与
docker-compose
bind mounts 一起正常工作的评论和答案。
For whom it may help: if you are using a virtual environment
inside your docker container, and you are using bind mounts, you may need to specify to have docker-compose
NOT look for your virtual environment folder.对谁有帮助:如果您在 docker 容器中使用
virtual environment
,并且使用绑定挂载,则可能需要指定让docker-compose
不查找您的虚拟环境文件夹。 This ensures that your virtual environment inside your container has the correct packages, paths, etc.这可确保容器内的虚拟环境具有正确的包、路径等。
Example dockerfile
:示例
dockerfile
:
FROM python:3.8-slim AS BUILDER
...# omitted code
ENV USERNAME=usr
ENV HOMEDIR=/home/$USERNAME
ENV VIRTUAL_ENV=$HOMEDIR/$VENV
# ... omitted code
WORKDIR $HOMEDIR
# Create virtual environment and activate it
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
COPY ./requirements.txt ./requirements-dev.txt ./constraints.txt ./
# Install python dependencies
RUN pip3 install --upgrade pip --no-cache-dir \
&& pip3 install --no-cache-dir -r requirements.txt -r requirements-dev.txt -c constraints.txt \
# ... omitted code
WORKDIR $HOMEDIR
# ... omitted code
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV PYTHONPATH="$PYTHONPATH:/"
# ... omitted code
ENTRYPOINT [ "python3", "-u", "path/to/file/to/run" ]
example docker-compose
file: docker-compose
文件示例:
version: "3"
services:
api:
# ... omitted code
build:
context: ./
dockerfile: Dockerfile.dev
# ... omitted code
volumes:
- $PWD:/home/usr # the bind mount
- /home/usr/venv # tell docker-compose not to look for the venv folder on the host
ports:
# ...omitted code
# ...omitted code
networks:
- ex-network
# ...omitted code
networks:
ex-network:
I was able to get ubuntu and bind mounts working properly with the above configuration by executing the standard docker-compose up --build
.通过执行标准
docker-compose up --build
,我能够使 ubuntu 和绑定挂载与上述配置正常工作。
I had the same issue, below is how I finally solved it.我有同样的问题,下面是我最终解决的方法。
Original code原始代码
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
Changed the volume setting a little bit:稍微改变了音量设置:
services:
db:
image: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
postgres-data:
driver: local
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.