Home > front end >  when creating django project via docker container it says sh: django-admin: not found
when creating django project via docker container it says sh: django-admin: not found

Time:09-12

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

this is the requirements.txt file

Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13

this is my 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


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

CodePudding user response:

Try pip3 install instead of pip install

If that doesn't work, try installing it separately in a step and check.

CodePudding user response:

  1. In normal cases, you should not use virtualenv inside Docker Container. see https://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.

Below is code that creates a new Django project through a Docker Container

requirements.txt

Django>=3.2.4,<3.3

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

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.

sudo chown -R $USER:$USER app
  • Related