Search code examples
pythondjangodockeralpine-linux

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.

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


Solution

  • 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"

    and you might run into some django-user issue USER django-user so you can use this one i pasted.

    Everything else looks correct.