[英]Running django app with docker, unrecognized runserver arguments
I'm having a problem with starting my django project in docker container.我在 docker 容器中启动我的 django 项目时遇到问题。 I have tried this in multiple ways, for example:
我已经尝试过多种方式,例如:
docker-compose run web python manage.py runserver
or just要不就
docker-compose up
first time i have tried this i got no error, but i couldn't open the app in browser so i tried it again and then it stopped working completely.我第一次尝试这个我没有错误,但我无法在浏览器中打开应用程序,所以我再次尝试,然后它完全停止工作。 I'm getting this error:
我收到此错误:
manage.py runserver: error: unrecognized arguments: python manage.py runserver
My docker-compose.yml我的 docker-compose.yml
version: "3.9"
services:
db:
image: postgres:alpine
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=gitlabdumptables
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=gitlabdumptables
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
My Dockerfile我的 Dockerfile
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip3 install -r requirements.txt
CMD python manage.py makemigrations
CMD python manage.py migrate
COPY . /code/
ENTRYPOINT [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]
The main process run in a Docker container is made up of two parts.在 Docker 容器中运行的主进程由两部分组成。 You're providing an
ENTRYPOINT
in the Dockerfile, and then the equivalent of a CMD
with the Compose command:
or docker run./manage.py...
arguments. You're providing an
ENTRYPOINT
in the Dockerfile, and then the equivalent of a CMD
with the Compose command:
or docker run./manage.py...
arguments. When you have both an ENTRYPOINT
and a CMD
, the CMD
gets passed as additional arguments to the ENTRYPOINT
;当您同时拥有
ENTRYPOINT
和CMD
时, CMD
将作为额外的 arguments 传递给ENTRYPOINT
; so in this case, you're running python manage.py runserver 0.0.0.0:8000 python manage.py runserver 0.0.0.0:8000
.所以在这种情况下,您正在运行
python manage.py runserver 0.0.0.0:8000 python manage.py runserver 0.0.0.0:8000
。
For this setup I'd suggest:对于此设置,我建议:
CMD
to ENTRYPOINT
in your Dockerfile.ENTRYPOINT
中选择CMD
而不是 ENTRYPOINT。 Then these command overrides will replace the command.CMD
.CMD
。 You do not need to specify a Compose command:
.command:
. You're also trying to run migrations in a way that won't work;您还试图以一种行不通的方式运行迁移; again, a container only runs a single process, and the last
CMD
or the startup-time override wins.同样,一个容器只运行一个进程,最后一个
CMD
或启动时覆盖获胜。 I'd use an entrypoint wrapper script to run migrations:我会使用入口点包装脚本来运行迁移:
#!/bin/sh
# entrypoint.sh
# Run migrations
python manage.py migrate
# Then run the main container command (passed to us as arguments)
exec "$@"
In your Dockerfile make sure this is COPY
ed in (the existing COPY
command you have will do it) and make this script be the ENTRYPOINT
(with JSON-array syntax).在您的 Dockerfile 中确保这是
COPY
编辑的(您现有的COPY
命令将执行此操作)并使此脚本成为ENTRYPOINT
(使用 JSON 数组语法)。
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./
ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array form
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] # not ENTRYPOINT
In your Compose file, you don't need to inject the code with volumes:
or replace the command:
, these are already part of your Dockerfile.在您的 Compose 文件中,您不需要使用
volumes:
注入代码或替换command:
,这些已经是您的 Dockerfile 的一部分。
version: '3.8'
services:
db: { ... }
web:
build: .
ports:
- '8000:8000'
environment: { ... }
depends_on:
- db
# but no volumes: or command:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.