Docker Compose Cookbook
What is Docker Compose?​
- Docker Compose is a tool for defining and running multi-container Docker applications.
- Managing multiple containers while using docker
- Without docker compose, user should manually create and manage each container, including networking and volume management. A network bridge must be created to allow containers to communicate, and data volumes must be managed individually for each container.
- With Compose, users can use a simple YAML file to configure their application's services, networks, and volumes, making it easy to manage complex applications with multiple interconnected components.
How multi container application managed without docker compose?​
-
Create network for the application
docker network create <network_name>- Check if the network is created
docker network ls -
Run the image in the created network
docker run -d \
-p 5000:5000 \
-e <env_var_name>=<env_var_value> \
--network <network_name> \
--name my-python-app \
my-python-app:0.0.1- Similar steps are followed for each additional container, ensuring they are all connected to the same network.
Managing multiple containers using Docker Compose​
-
Create a
docker-compose.ymlfile in the root of your project directory. The name of the file can be changed, but it is recommended to keep it asdocker-compose.ymlfor consistency. -
Define the services, networks, and volumes in the YAML file. For example:
-
Typical docker compose used in development.
services:
app_service:
build: .\<service_path>
ports:
- "8000:8000"
environment:
- ENV_VAR_NAME=${ENV_VAR_VALUE}
ui_service:
build: .\<ui_service_path>
ports:
- "3000:3000"
environment:
- ENV_VAR_NAME=${ENV_VAR_VALUE} -
Docker compose for production.
services:
app_service:
image: my-python-app:latest
ports:
- "8000:8000"
environment:
- ENV_VAR_NAME=${ENV_VAR_VALUE}
ui_service:
image: my-ui-app:latest
ports:
- "3000:3000"
environment:
- ENV_VAR_NAME=${ENV_VAR_VALUE}
depends_on:
- app_service -
Best Practices or Considerations while generating docker compose.
- The docker compose file shall be maintained in source control.
- The docker compose should not contain any sensitive information, such as passwords or API keys. Use environment
variables or Docker secrets to manage sensitive data. Example:
ENV_VAR_NAME=${ENV_VAR_VALUE}, the${ENV_VAR_VALUE}part should be defined in a.envfile or in the environment where the Docker Compose command is run. - Use
imagetags to specify the exact version of the image to be used,buildcontext should be used for development. - Networks need not be explicitly defined in the Compose file if not required, as Docker will create a default network for the application.
- Use
depends_onto specify the order of service startup.
-
Image naming in docker compose
-
Docker compose usually prefix the image names with the project name, which is derived from the directory name containing the
docker-compose.ymlfile and suffix the image name with instance number. -
User can override the project name using the
--project-nameor-pflag when runningdocker-composecommands.docker-compose -d \
-f <docker_compose_file> \
-p <project_name> \
up
-
-
-
Start the application using the
docker-compose upcommand:docker-compose up -d \
-f <docker_compose_file> \- The
-dflag runs the containers in detached mode (in the background). - The
-fflag allows you to specify the Compose file.
- The
-
Stop the application using the
docker-compose downcommand:docker-compose down- This command stops and removes all containers defined in the
docker-compose.ymlfile.
- This command stops and removes all containers defined in the
-
Stop and retain the containers using the
docker-compose stopcommand:docker-compose stop- This command stops the containers without removing them, allowing you to start them again later
with
docker-compose start.
- This command stops the containers without removing them, allowing you to start them again later
with
-
Start the containers using the
docker-compose startcommand:docker-compose start- This command starts the containers that were stopped with
docker-compose stop.
- This command starts the containers that were stopped with