This repository contains a Django Todo App that can be easily containerized using Docker Compose. The Docker Compose configuration allows you to run both the Django Todo App and a MySQL database within separate containers.
Before getting started, ensure that you have the following prerequisites installed on your machine:
- Docker
- Docker Compose
Steps to Dockerize the Todo App using Docker Compose:
- Clone this repository to your local machine:
git clone <repository_url>
- Create the
docker-compose.yml
file in the root directory and add configuration. - Build and run the containers using Docker Compose:
docker-compose up
Make sure you have correct indentation as indentation and spaces matters in yml file. Below configurations are added in this To Do applications docker-compose.yml, Let's understand each line by line.
version: "3.9"
services:
django_todo_app:
container_name: "django-todo-app"
build : .
ports:
- 8000:8000
volumes:
- django_todo_volume:/app
mysql_db:
container_name: "django-mysql-db"
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "test@123"
volumes:
django_todo_volume:
In version, we have to mention the version of language which you are using in your application. As django todo app
is a python application I have mentioned python version 3.9
Under services we specify the services which we have to run (there can be one or multiple services)
-
django_todo_app:
This is todo application where I have code base. Inside this I have specified its related commands & specifications. Let's look into it.container_name:
Give name to your container. Here I have given container name as django-todo-appbuild:
This builds or rebuild image & add all configs for the services to create a container.ports:
Specify on which port you want to run service. It binds local port to remote server port. I am running my django todo service on 8000 and binding same to run on server.volumes:
Bind volume for the container of service. -
mysql_db:
Adding a service for mySQL database.container_name:
Specify container name for service.image:
It will pull MySQL image with 5.7 tag from dockerhub.ports:
By default, mySQL run on port 3306, hence binding same to the server.environment:
Here we have to pass environment variable like mySQL password.MYSQL_ROOT_PASSWORD: "your_password"
Declare volume which will get bind to the service.
Now, with the below command, you are now ready to build and execute your docker-compose.yml, It creates containers for mentioned services.
docker-compose up
You can run in detached mode by adding -d
flag.
docker-compose up -d
After all, You can check all your running containers with
docker ps