Development

Docker Compose: Define and Run Multi-Container Apps

What is Docker?
Docker is a containerization platform that allows developers to package applications with all their necessary parts (libraries, dependencies, config) into lightweight, portable containers. This ensures the application runs consistently across any environment and makes it easy to scale and manage. 

What is Compose?
Compose is a tool for defining and running multi-container docker applications.

Multi-Container docker application
It is the type of application that is based on multiple that is based on multiple software/images such as redis as cache, mysql for database, django for backend etc.

Instead of managing each single container/images to run full application, we can use 
compose tool to run multiple containers/images.

With Compose, you use a YAML file to configure your applications services like Redis, MYSQL etc. 

Then, with a single command, you create and start all the services from your configuration.

Compose works in all environment including production, staging, development, testing as well as CI workflows.

Docker Desktop includes Docker Compose along with Docker Engine and Docker CLI which are Compose pre requirements.

Following are use cases of Docker Compose:
  • Development environment
  • Automated testing environments
  • Single host deployments.
Docker Compose key features
  • Have multiple isolated environments in a single host.
  • Preserves volume data when containers are created 
  • Only recreate containers that have changed 
  • Supports variables and moving a composition between environments.


commands:
docker compose --help: to see available commands
docker compose up: This command will takes the docker-compose.yml file and start
building container from defining images. It starts creating container for each images/services defining in docker-compose.yml file. This command will dowload/pull required images and create/start required containers. 

If we modify docker-compose.yml file and again run docker compose up then it will only recreate modified containers.

docker compose up -d: It is same as docker compose up but it will run in the background which means it won't occupy terminal.

docker compose ps: This command is used to list currently running service.

docker compose exec service_name mysql -u root -p: This command is used to get access the terminal of container.

docker compose stop: This command is used to stop compose that starts using docker compose up -d.

docker compose down: This command is used to remove containers entirely. It will not remove volumes. If you want to remove volumes then use --volumes flag.

Steps:

create docker-compose.yml 
docker-compose.yml:

Than, Run following commands 
docker compose config -> shows/list what services is going to run
docker compose create -> Just create containers for images defined in docker-compose.yml file.
docker ps -a -> lists all containers (stopped + running)
docker network ls -> list all docker networks
docker container ls -> list running containers only
docker exec web bash -> Opens interactive bash shell inside container named web
(use sh instead of bash if bash isn’t available)
docker compose stop -> Stops running containers(keeping them)
docker compose down -> Stops and remove containers
docker compose down --volumes -> Stops and remove containers + remove volumes(data loss)
docker compose up -> Creates and starts containers

How to use environment variables
create .env

Now, use That TAG inside docker-compose.yml


Than,
docker-compose up -d

Set environment attribute in Service Container

Use environment file in service container 
Create mysqlconfig.env

Than, use it inside docker-compose.yml

Set profile name for service

Now, when we run
docker compose up -d: Two containers from services web and db will created but the container from recache will not created because profiles: - rediscache set to it.

for running container for recache: docker compose --profile rediscache up -d
When we run,
docker compose down --volumes: It doesnot remove rediscache

To remove rediscache -> docker compose --profile rediscache down --volumes

Exporting PORT

Now, 
docker compose up -d

Now live on
localhost:8000

No comments yet. Be the first to start the conversation!