skip to Main Content

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

3

Answers


  1. Try pip3 install instead of pip install

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

    Login or Signup to reply.
    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
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search