As presented at Digital Product School.
You might need to add sudo before the docker commands.
Feel free to contact me for questions/help.
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.
- Build and run the project from this repo
- Create the django project from scratch
git clone https://github.com/anindyaspaul/demo-docker.git
cd demo-docker
docker-compose up --build
Visit localhost:8000
Create the db tables (optional)
docker-compose down
mkdir demo-docker
cd demo-docker
Create requirements.txt
with the following content.
Django
psycopg2
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker build -t demo .
docker images
docker run -v ~/codes/dps/demo-docker:/code demo django-admin startproject demo .
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.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
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,
}
}
docker run -v ~/codes/dps/demo-docker:/code -p 8000:8000 demo python manage.py runserver 0.0.0.0:8000
Visit localhost:8000
docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate
docker-compose run web python manage.py createsuperuser
docker-compose up --build
docker <command>
ps
See running containersps -a
See all containersstart
containersstop
containersrestart
containersrm
Remove containersimages
See all imagesrmi
Remove imagesimage prune
Remove intermediate layers to free up storage
You can try fiddling with it just to learn more about docker. E.g.
- automate the
makemigrations
and themigrate
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
, useuwsgi
orgunicorn
with Nginx. - add Redis database that communicates through unix socket.
All of these should work by running just the docker-compose up --build
command.