Dockerize Python Apps
Docker enables developers to pack applications into containers that run as instances of a docker image and contain their own operating system as well as all the necessary dependencies. This makes it easier, simpler and safer to build, deploy and manage your apps.
docker compose
docker compose enables running & management of a multi-container application in a simple way using the YAML file (docker-compose.yml) defining all the required services.
For out example we will use 2 services:
- django application
- postgres database
basic project setup
.env
setting.py
requirements.txt
In the dockerfile we will use the requirements.txt file to install all the necessary packages.
Before dockerizing, make sure you have the following packages installed:
Next create the requirements.txt file:
pip freeze>requirements.txt
Dockerfile
Dockerfile for our python/django app:
docker-compose.yml
The docker-compose file including two services:
web(django web app) and db (postgres db)
Commands to execute
Now run the containers:
docker-compose up -d
Stop the containers:
docker-compose down
View the logs of a container:
docker-compose logs <service_name>
List the running containers:
docker ps
Get inside the web container to execute other commands (i.e. createsuperuser)
docker exec -it <container_id> /bin/sh