Compose
Docker Compose is allowing you to define and run multi-container Docker applications using a simple YAML file. It is used to manage applications that consist of multiple containers, making it easier to define, run, and scale them.
What is Docker Compose?
Docker Compose is a tool that lets you define and run multi-container applications using a simple YAML file called docker-compose.yml. This file describes the services, networks, and volumes needed for your application.
With Docker Compose, you can start all your containers with a single command, making it easier to manage complex applications.
Services
In a docker-compose.yml file, you define each container as a service. Each service can have its own configuration, such as the image to use, environment variables, ports to expose, and volumes to mount.
Here’s a simple example of a docker-compose.yml file
Networks
Networks allow containers to communicate with each other. You can define custom networks in your Compose file to control how services interact.
Volumes
Volumes are used to persist data across container restarts. You can define volumes in your Compose file to ensure that data is not lost when containers are recreated.
Example of a docker-compose.yml file:
Breakdown of the Example:
- version: Specifies the version of the Compose file format.
- services: Defines the containers that make up your application.
- web: Runs an Nginx server, exposing port 80.
- app: Runs your application, connecting to the database service.
- db: Runs a MySQL database with a specified root password and mounts a volume for data persistence.
- networks: Defines a custom network called
my_networkusing the bridge driver. - volumes: Defines a named volume
db_datafor the MySQL database.
Running Docker Compose
To run your application defined in the docker-compose.yml file, you can use the following command:
Start
Stop
Note:
In the service means we can simply run the image like for Nginx we can simply use docker run nginx:latest and for MySQL we can use docker run mysql:latest but the main benifit of using compose is that we can run multiple containers at once and also we can define the network and volume in a single file.
Custom Compose File
Like we have NodeJs application and it is developing phase so we want to create a docker file. but there is one catch how we can define that NodeJs application in docker-compose.yml file.
- Push the docker image to the docker hub then define the service in the
docker-compose.ymlfile. but there is one issue here as we know that theNodeJsapplication is in developing phase so we need to rebuild the image every time we make changes in the code. - So we can use the
buildoption in thedocker-compose.ymlfile to build the image from the Dockerfile in the current directory. - We can use the docker hub image in docker compose file if your project is ready to deploy and you want to use the image from the docker hub.
Code
In this example, the node_app service is built from the Dockerfile in the current directory. The db service uses a PostgreSQL image from Docker Hub. Both services are connected to a custom network and use a named volume for data persistence.