Skip to content

Instantly share code, notes, and snippets.

@cse031sust02
Last active June 2, 2024 09:39
Show Gist options
  • Save cse031sust02/f149d809d50116e7890691d73922d379 to your computer and use it in GitHub Desktop.
Save cse031sust02/f149d809d50116e7890691d73922d379 to your computer and use it in GitHub Desktop.
Basic Dockerfile for Django using Pipenv
# My Django Project
# Version: 1.0
# FROM - Image to start building on.
FROM python:3
# PROJECT SETUP
# ----------------
# sets the working directory
WORKDIR /usr/src/django-docker
# copy these two files from <src> to <dest>
# <src> = current directory on host machine
# <dest> = filesystem of the container
COPY Pipfile Pipfile.lock ./
# install pipenv on the container
RUN pip install -U pipenv
# install project dependencies
RUN pipenv install --system
# copy all files and directories from <src> to <dest>
COPY . .
# RUN SERVER
# ------------
# expose the port
EXPOSE 8000
# Command to run
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
@cse031sust02
Copy link
Author

cse031sust02 commented Oct 25, 2020

Running Example

  • Build Image
$ docker build --tag my-django .
  • Show Images
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-django      	   latest              c0ac7c872818        2 minutes ago       976MB
  • Run Container
$ docker run --name my-django-docker -p 8000:8000 my-django
  • Show Running Containers
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
580c48dc36d7        my-docker-test      "python manage.py ru…"   3 minutes ago       Up 3 minutes        0.0.0.0:8000->8000/tcp     my-django-docke

@tadasajon
Copy link

Isn't the whole point of a docker container to have a virtual environment? If so, then why is there any need for Pipenv? Why can't we just run Django globally in the container?

@tarikwaleed
Copy link

@tadasajon
The adding value of using pipenv is to manage the versioning of your dependencies, separating development dependencies from actual project dependencies. And here, in this example, we're not installing dependencies in a virtual environment but globally on the container.

@tarikwaleed
Copy link

@cse031sust02
Thanks for sharing this gist ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment