Django app dev in Docker

django, docker, programming

Start with Docker’s doc

The link https://docs.docker.com/compose/django/ for the most part is valid in setting up a development environment for working with Django in Docker.

Update to use Pipfile

The document still uses the example of a requirements.txt to handle app dependencies. Django now has at least two newer alternatives:

  • Setup.py
  • Pipfile (pipenv)

One update to use pipenv is to modify the Dockerfile as such:

FROM python:3
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code

COPY . /code/
RUN pip install pipenv && pipenv install
RUN pipenv run python manage.py migrate

That last command is optional; it can be deferred in the docker-compose.yml if that works better.

Speaking of docker-compose.yml:

version: '3'

services:
  web:
    build: .
    command: pipenv run python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

(I removed the “db” service to keep this minimalistic.)

Some Docker Notes

docker

Some Docker notes of commands for reference

Images

Listing images

docker images

Running images

docker run <image name>
docker run <repo name>/<image name>

Building images

Create a Dockerfile file with the recipe therein.

docker build -t <image name> <parent subdir of Dockerfile>

 

Tagging images

Kinda like renaming; can be used to prepare for pushing

docker tag <image ID> <repo name>/<image name>:<version label or tag>

One version label is “latest.”

Pushing images

docker login --username=<user name> --email=<email>
docker push <image name>

Deleting images

docker rmi <repo name>
docker rmi <image ID>

The “-f” option is required if there are containers (even finished ones) referencing the image.

Containers

Kinda like processes.

Listing containers

docker ps -a

Deleting containers

docker rm <container ID>