/demo-docker

Demo presentation codes presented at DPS workshop

Primary LanguagePython

Demo Docker

As presented at Digital Product School.

You might need to add sudo before the docker commands.

Feel free to contact me for questions/help.

Prerequisites

Install docker and docker-compose so that you can see the version names from the following commands.

docker -v
docker-compose -v

Now you can do either of the following.

  1. Build and run the project from this repo
  2. Create the django project from scratch

1. Build and run the project from the repo

Clone and get into the project

git clone https://github.com/anindyaspaul/demo-docker.git
cd demo-docker

Run the services

docker-compose up --build

Visit localhost:8000

Create the db tables (optional)

Create a superuser (optional)

Take down the services

docker-compose down

2. Create project from scratch

Create project directory

mkdir demo-docker
cd demo-docker

List django dependencies

Create requirements.txt with the following content.

Django
psycopg2

Create Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Build docker image

docker build -t demo .

List docker images

docker images

Create django project using django-admin

docker run -v ~/codes/dps/demo-docker:/code demo django-admin startproject demo .

Run the project

docker run -v ~/codes/dps/demo-docker:/code -p 8000:8000 demo python manage.py runserver 0.0.0.0:8000

Visit localhost:8000

Create docker compose file to run multiple services (for the db)

Create docker-compose.yml with the following content.

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Add database configuration in the settings file

Replace the DATABASES entry in demo/settings.py with the following.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

Run the server again

docker run -v ~/codes/dps/demo-docker:/code -p 8000:8000 demo python manage.py runserver 0.0.0.0:8000

Visit localhost:8000

Run django migrations to create db tables

docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate

Create a superuser for the admin

docker-compose run web python manage.py createsuperuser

Run the services using docker-compose

docker-compose up --build

Some useful commands

docker <command>
  • ps See running containers
  • ps -a See all containers
  • start containers
  • stop containers
  • restart containers
  • rm Remove containers
  • images See all images
  • rmi Remove images
  • image prune Remove intermediate layers to free up storage

Things you might do from here

You can try fiddling with it just to learn more about docker. E.g.

  • automate the makemigrations and the migrate commands.
  • automate the collectstatic command of django.
  • automate the createsuperuser command to pre-populate the database.
  • setup Nginx server that routes requests to the django server.
  • run django server in production mode, i.e., don't use runserver, use uwsgi or gunicorn with Nginx.
  • add Redis database that communicates through unix socket.

All of these should work by running just the docker-compose up --build command.