Docker Compose
Docker Compose is a tool provided by Docker that allows us to define and manage multi-container Docker applications.
It enables us to define the services, networks, and volumes for your application in a single, easy-to-read YAML file called docker-compose.yml
.
With Docker Compose, we can spin up your entire application stack with a single command, making it convenient for development, testing, and deployment.
What is YAML?
YAML is a data serialisation language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language.
YAML is a popular programming language because it is human-readable and easy to understand.
YAML files use a .yml or .yaml extension.
Components in a Docker Compose definition:
Here is a breakdown of the key components in a typical Docker Compose definition-
Services:
A service represents a containerized application or a component of an application.
Each service is defined in the
services
section of thedocker-compose.yml
file.It includes information such as the Docker image to use, environment variables, ports to expose, volumes to mount, etc.
Networks:
Networks define how containers communicate with each other.
By default, Docker Compose creates a bridge network for all services defined in a
docker-compose.yml
file.We can explicitly define networks and connect services to them.
Volumes:
Volumes allow data to persist beyond the lifecycle of a container.
They can be used for sharing data between containers or persisting application data.
Volumes can be defined in the
volumes
section or within individual service definitions.
Environment Variables:
Environment variables are used to configure the behavior of containers.
They can be specified within individual service definitions.
Docker Compose Commands:
Docker Compose provides commands for managing the application lifecycle. We will use below commands-
# To start the containers. docker-compose up
# To start the containers and run it in background. docker-compose up -d
# To stop and remove the containers. docker-compose down
Docker-Compose Setup:
To install the docker-compose, we will use the code given below-
sudo apt install docker-compose -y
Task 1:
In this task, we will learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
We will use the below code in our YAML file-
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: postgres:latest
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
Linking Containers:
You can link services together using the depends_on
field or by defining custom networks. For example:
codeversion: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
Task 2:
Pull an Image from DockerHub and run the container:
Here, we have pulled the "nginx" image from the docker hub. We can verify the same.
Use the command given below to pull the image from the docker hub-
# docker image pull <image-name>
docker image pull nginx
We are running this container after exposing container port 80 and mapping it with system port 80. We can verify the same.
Use the command given below to run the container-
docker container run -d --name web-app -p 80:80 nginx:latest
Use the docker stop and docker start commands to stop and start the container:
To stop a docker container, use the command given below-
# docker container stop <container-name>
docker container stop web-app
To start a docker container, use the command given below-
# docker container start <container-name>
docker container start web-app
Use the docker rm command to remove the container:
To remove the container, we first need to stop it and then remove it. Use the command given below-
# docker container stop <container-name>
docker container stop web-app
# docker container rm <container-name>
docker container rm web-app
Conclusion:
In this blog, we learned about the Docker-Compose. We used YAML to write the Docker-Compose file. We performed some tasks like writing a docker-compose.yml file, setting up the environment, configuring the services and links between different containers, and also to use environment variables. We learned how to start, stop and remove the docker containers.