Docker Compose is a tool that was developed to help define and share multi-container applications.
With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
Suppose, you are working on a project which requires at least 10 different services in a running state to run your project. For example, let's say your project requires Java 8, Node 14, MySQL, MongoDB and many others.
In such a case, you have to pull all those images individually from DockerHub and start all of them in their containers manually as an everyday task, this is so time-wasting and hectic task also.
It would be good if it's a one-time process like you are starting all the created containers that are mentioned in a file just by only using one tool.
So, Docker Compose is a tool you can use to define and share multi-container applications. This means you can run a project with multiple containers using a single source.
For example, assume you're building a project with NodeJS and MongoDB together. You can create a single image that starts both containers as a service – you don't need to start each separately.
Not only container creation, you can also build an image by using the Docker Compose tool. For that, you should have a docker file to create an image and put some commands inside the docker-compose.yml file.
1. Docker Compose
Suppose, you are working on a project which requires at least 10 different services in a running state to run your project. For example, let's say your project requires Java 8, Node 14, MySQL, MongoDB and many others.
In such a case, you have to pull all those images individually from DockerHub and start all of them in their containers manually as an everyday task, this is so time-wasting and hectic task also.
It would be good if it's a one-time process like you are starting all the created containers that are mentioned in a file just by only using one tool.
So, Docker Compose is a tool you can use to define and share multi-container applications. This means you can run a project with multiple containers using a single source.
For example, assume you're building a project with NodeJS and MongoDB together. You can create a single image that starts both containers as a service – you don't need to start each separately.
Not only container creation, you can also build an image by using the Docker Compose tool. For that, you should have a docker file to create an image and put some commands inside the docker-compose.yml file.
2. docker-compose.yml file
The compose file is a YML file defining services, networks, and volumes for a Docker container. There are several versions of the compose file format available – 1, 2, 2.x, and 3.x.
3. What Is YAML File
YAML stands for Yet Another Markup Language.YAML is a human-readable data serialization language, just like XML and JSON. Serialization is a process where one application or service that has different data structures and is written in a different set of technologies can transfer data to another application using a standard format.
YAML is a widely used format for writing configuration files for different DevOps tools, programs, and applications because of its human-readable and intuitive syntax.
To create a YAML file, use either the
.yaml
or.yml
file extension.YAML is case sensitive
YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead.
Nearly every YAML file starts with a list.
List members are denoted by a leading hyphen (-).
Associative arrays are represented using colon ( : ) in the format of key-value pair. They are enclosed in curly braces {}.
Multiple documents with single streams are separated with 3 hyphens (---).
The most important part is, YAML follows a proper Indentation. If your Indentation is not correct it will throw an error.
Example of a Yaml File:
4. Let's Create a docker-compose.yml file as an Example:
Step 1:
Create a folder called compose and then change the directory into it.
Step 2:
Install docker-compose by using the command sudo apt install docker-compose -y and check its version.
Step3: Create a Compose yaml file vim docker-compose.yml
Here commands used:
version: '3', denotes that we are using version 3 of Docker Compose, and Docker will provide the appropriate features.
services: This section defines all the different containers we will create. In this example, we have two services, a webserver and a database.
webserver: This is the name of the service. Docker Compose will create containers with the name that provides.
image: If we don’t have a Dockerfile and want to run a service using a pre-built image, we specify the image location using the
image
clause. Compose will fork a container from that image.ports: This is used to map the container’s ports to the host machine.
environment: The clause allows us to set up an environment variable in the container [here set up a password for MySQL]. This is the same as the
-e
argument in Docker when running a container.
Step4:
Execute the command docker-compose up -d [ -d is for detached mode].
List the images by executing the docker images command, you can see nginx:alpine image has been created.
List the containers by using the docker-compose ps command,it will create two containers.
After that, if you want to stop the containers then use the docker-compose down command.
Step5:
Allow port 8001 to the security group by adding it to the inbound rule in the AWS EC2 instance to access it from outside of the internet.
This is the welcome page of Nginx [type publicip:8001]
5. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use usermod
command to give the user permission to docker). Make sure you reboot the instance after permitting the user.
Inspect the container's running processes and exposed ports using the docker inspect command.
Use the docker logs command to view the container's log output.
Use the docker stop and docker start commands to stop and start the container.
Use the docker rm command to remove the container when you're done.
- Run the container as a non-root user and give the user permission to docker, in this case, use the command sudo usermod -aG docker $USER (add docker to a user group)
Then reboot the system and after that, you can execute the docker commands as a non-root user.
Pull an Nginx image from the docker hub and run the container [do port mapping of Nginx(80) to the host machine ].
Check the status container using the docker ps command.
The container is up and running.
Inspect the container's running processes and exposed ports using the docker inspect command.
You can check the ports part in the NetworkinSettings section if we inspect that container,
Use the docker logs command to check the logs of the container.
Use the docker stop and docker start commands to stop and start the container.
Use the docker rm command to remove the container.
To remove a container use the docker rm command and check the status by the docker ps -a command [it will show all stop and running containers]