[英]when creating django project via docker container it says sh: django-admin: not found
I created a docker python image on top of alpine the problem is that when I want to start a django app it can not find django and it is right bcz when I type pip list, it does not have django and other packages. I created a docker python image on top of alpine the problem is that when I want to start a django app it can not find django and it is right bcz when I type pip list, it does not have django and other packages.
ps: when creating the images it shows that it is collecting django and other packages ps:创建镜像时显示正在收集django等包
this is the requirements.txt file这是 requirements.txt 文件
Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13
this is my Dockerfile:这是我的 Dockerfile:
FROM python:3.9-alpine3.13
LABEL maintainer="siavash"
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./app /app
WORKDIR /app
EXPOSE 8000
ARG DEV=false
RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = "true" ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
fi && \
rm -rf /tmp && \
adduser \
--disabled-password \
--no-create-home \
django-user
ENV PATH = "/py/bin:$PATH"
USER django-user
and this is docker-compose.yml这是 docker-compose.yml
version: "3.9"
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
and this is the command that I use:这是我使用的命令:
docker-compose run --rm app sh -c "django-admin startproject app . "
BTW the image is created successfully顺便说一句,图像创建成功
Try pip3 install
instead of pip install
尝试
pip3 install
而不是pip install
If that doesn't work, try installing it separately in a step and check.如果这不起作用,请尝试在一个步骤中单独安装并检查。
Below is code that creates a new Django project through a Docker Container下面是通过 Docker 容器创建新 Django 项目的代码
requirements.txt要求.txt
Django>=3.2.4,<3.3
Dockerfile Dockerfile
FROM python:3.9-alpine3.13
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt && \
rm /tmp/requirements.txt
WORKDIR /app
docker-compose.yml docker-compose.yml
version: "3.9"
services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
The commands to create the new project创建新项目的命令
docker-compose build
docker-compose run --rm app sh -c "django-admin startproject app ."
docker-compose up -d
To edit files that created by the docker container, we need to fix the ownership of the new files.要编辑由 docker 容器创建的文件,我们需要修复新文件的所有权。
sudo chown -R $USER:$USER app
So the reason why this happening i believe is because of a very simple error that's hard to see ENV PATH = "/py/bin:$PATH"
should be ENV PATH="/py/bin:$PATH"
所以我认为发生这种情况的原因是因为一个非常简单的错误很难看到
ENV PATH = "/py/bin:$PATH"
应该是ENV PATH="/py/bin:$PATH"
and you might run into some django-user issue USER django-user
so you can use this one i pasted.你可能会遇到一些 django-user 问题
USER django-user
所以你可以使用我粘贴的这个。
Everything else looks correct.其他一切看起来都是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.