简体   繁体   English

通过 docker 容器创建 django 项目时,它说 sh: django-admin: not found

[英]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.如果这不起作用,请尝试在一个步骤中单独安装并检查。

  1. In normal cases, you should not use virtualenv inside Docker Container.在正常情况下,您不应在 Docker 容器内使用 virtualenv。 see https://stackoverflow.com/a/48562835/19886776https://stackoverflow.com/a/48562835/19886776
  2. Inside the container there is no need to create an additional "django-user" user because the container is an isolated environment.在容器内部,不需要创建额外的“django-user”用户,因为容器是一个隔离的环境。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM