Table of contents
- What is Docker Compose?
- Why docker-compose?
- The Docker-compose file:
- Practical Example of Docker-compose file:
- Common Docker Compose Commands:
- Use .env files to store environment variables that you don't want to expose
- Use volumes to persist data across container restarts or share data between containers
- Use networks to isolate and connect your services
- Use labels to add metadata to your containers, networks, and volumes
- Use .gitignore files to exclude files or directories that you don’t want to commit
- Use health checks to monitor the status of your services
- Conclusion:-
If you do know about Docker, read on, and I’ll attempt to show you the power and benefits of a single docker-compose.yml
can offer over the original Dockerfile
.
Docker Compose
What is Docker Compose?
Docker is the most popular containerization tool in the DevOps world. But, What is Docker Compose? Docker Compose is used to run applications that have multiple containers using a YAML file. There can be several cases where the docker application must run numerous containers for different technology stacks. Now building, running, and connecting separate docker files for each container can be difficult; this is where docker-compose helps you out.
Why docker-compose?
Most of our applications nowadays do not play alone. They need a database, storage service, security API, and more services around them, each of which needs to become a container.
Docker-compose simplifies our life, performs it with a single command, helps build images, and orchestrates our containers.
In short, Docker-compose takes care of the application lifecycle and allows us to manage the container actions like start, stop, and simplify to get the status, publish and communicate between networks.
The Docker-compose command comes with three essential flags.
docker-compose build:
Build containers defined into the docker-compose.yml.docker-compose up:
Start the containers and networks.docker-compose down:
Stop containers and networks.
All magic happens using the docker-compose.yml file behind, so let us talk about it.
The Docker-compose file:
Like Dockerfile, we use the docker-compose.yml, which has two primary vital areas the version it defines, and the services that represent the container's definitions.
The services are the place to define our containers; it supports additional properties like image, builds, environment, volumes, port, networks, and more.
In YML files, the indentation matters and always use space.
Practical Example of Docker-compose file:
Here is an example of docker-compose.yml
a file that defines a simple web application that consists of two services: web
and db
version: 3.8
services:
web:
image: nginx
ports:
- 80:80
environment:
- DB_HOST=db
depends_on:
- db
networks:
- app-network
db:
image: postgres
environment:
- POSTGRES_PASSWORD=secret
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-network
networks:
app-network:
driver: bridge
ipam:
config:
- subnet: 172.16.0.0/24
volumes:
db-data:
To run your application with Docker Compose, you need to navigate to the directory where your YAML file is located and execute the following command:
docker-compose up
This command will pull the images from the Docker Hub or a private registry if they are not already present on your machine, create and start the containers according to the configuration in the YAML file, and attach them to their logs. You can also use the -d
flag to run the containers in detached mode, which means they will run in the background without blocking your terminal.
To stop your application with Docker Compose, you need to execute the following command:
docker-compose down
This command will stop and remove the containers created by docker-compose up. You can also use the -v
flag to remove any volumes associated with the containers.
Common Docker Compose Commands:
| Command | Description |
| ------- | ----------------------------------------------------------- |
| build | Builds or rebuilds services |
| config | Validates and views the Compose file |
| create | Creates services |
| down | Stops and removes containers, networks, images, and volumes |
| events | Receives real time events from containers |
| exec | Executes a command in a running container |
| help | Gets help on a command |
| images | Lists images |
| kill | Kills containers |
| logs | View output from containers |
| pause | Pauses services |
| port | Prints the public port for a port binding |
| ps/ls | Lists containers |
| pull | Pulls service images |
| push | Pushes service images |
| restart | Restarts services |
| rm | Removes stopped containers |
| run | Runs a one-off command on a service |
| scale | Sets the number of containers for a service |
| start | Starts services |
| stop | Stops services |
| top | Displays the running processes for a service |
| unpause | Unpauses services |
| up | Creates and starts containers |
| version | Shows the Docker-Compose version information |
Use environment variables in your YAML file to make it more dynamic and flexible
Description: Use ${PORT}
to refer to the port number that is set in an environment variable named PORT. You can also use .env
files to store your environment variables in a separate file.
Use .env
files to store environment variables that you don't want to expose
Description: Create a .env
file in the same directory as your docker-compose.yml
file and write POSTGRES_PASSWORD=secret
in it. Then, you can reference this variable in your docker-compose.yml
file as ${POSTGRES_PASSWORD}.
Use volumes to persist data across container restarts or share data between containers
Description: Use volumes to store the database data or the web server configuration. You can define named volumes in the top-level volumes section of your YAML file and then reference them in your services. You can also use bind mounts to mount a directory or a file from your host system into your containers.
Use networks to isolate and connect your services
Description: Use networks to restrict the communication between your services or to enable communication with external services. You can define custom networks in the top-level networks section of your YAML file and then reference them in your services. You can also use the default network that is created by Docker Compose for your application.
Use labels to add metadata to your containers, networks, and volumes
Description: Use labels to identify the owner, purpose, or version of your resources. You can define labels in the labels section of your YAML file and then use them to filter or group your resources.
Use .gitignore files to exclude files or directories that you don’t want to commit
Description: Create a .gitignore
file in the same directory as your docker-compose.yml
file and write .env
and db-data/*
in it. Then, these files or directories will not be tracked by Git.
Use health checks to monitor the status of your services
Description: Use health checks to determine if a service is ready, healthy, or unhealthy. You can define health checks in the health check section of your YAML file and then use them to control the startup order, restart policy, or scaling behavior of your services.
Conclusion:-
Well, we learned how to use docker-compose to help orchestrate your containers and declare ports, volumes, variables, and networks into docker-compose files. Docker-compose simplifies the development, deployment, and management of complex applications that consist of multiple services.