Docker-Compse & Django Tutorial
Enter Container CLI: docker exec -it <running container ID> bash
See All Images: docker image ls
See Live Containers: docker container ls
(add --all
for offline)
###Directory Structure
- New Directory
- Dockerfile
- requirements.txt
- docker-compose.yaml
- manage.py
- Django Project Dir
- Django Project Files/Sub Dirs
This file describes the setup of the image. The example below uses the base Python3 image to create the new image. It makes a directory in the image called ‘code’. It also installs the required dependencies using the given requirements.txt file.
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
This file is used to start several containers at once. If the containers do not exist they use the associated images to create the container or if they do and have been changed they will recreate the containers from their original state. The example below uses Python3 and starts two services: db and web. DB, is an official Postgres image that is downloaded from DockerHub. Web, builds an image from the current directory and includes several other configurations. The example below will be different depending on if you’re building the image, what machine you are one, and for several other reasons.
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
The project is created in the same directory and needs to be modified to work with the Postgres image that was imported with the ‘docker-compose.yml’ (the default db settings are listed below). Other settings may need to be changed depending on what other images are imported.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
To run a particular docker-compose.yml you must be in the same directory as the file. The commands to run in verbose and detached mode. Docker-compose will recreate the containers if there has been any change to them you can prevent this by using the appropriate flag while starting.
Start in Verbose: docker-compose up
Start in Verbose & Prevent Recreating Containers: docker-compose up -- no-recreate
Start in Detached: docker-compose up -d
Stop in Verbose: ctrl + C
Stop either: docker-compose down
-
Need to first login to DockerHub (if you don't have an account you must first create one here using the command below).
docker login
-
Get the image id using the command below.
docker image ls
-
Add a tag to your image using the command below.
docker tag <image ID> <dockerHubUserName/<imageName>:<versionControl>
-
Push image to DockerHub using the command below.
docker push <dockerHubUserName/<imageName>
-
From another machine use the pull command using the command below.
docker pull <dockerHubUserName/<imageName>
As mentioned above docker-compose.yml needs to be changed for each variable (i.e. different machine) to run the proper set of containers. Example yml for the containers that were built in the earlier section using the docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
image: joswar84/djdock:latest
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- db