Table of contents
This is the Day18 of the #90DaysofDevOps challenge in which we are going to discuss what is docker-compose, how to create a docker-compose file and what are the commands to run the docker-compose file.
Docker-Compose :
As we all learned what is Dockerfile and how to create Dockerfile in my previous blog. So, docker-compose is a tool that is used to define the application's services in a yaml file, and with the help of docker-compose, we can create multi-container applications too.
In a single command of docker-compose, we can start and stop all the application services in just a moment.
Before seeing the installation of docker-compose first let us know what is YAML because all docker-compose files we are going to write in YAML files. (.yaml) or (.yml)
What is YAML?
YAML is a data-serialization language that stores information. It was launched in 2001 and in that year it stands for (Yet Another Markup Language). As it has been clarified that YAML stores data, not documents, the YAML full form changed to (YAML Ain't Markup Language).
It has a clean syntax means it does not contain braces, closing tags, etc. It is used to write configuration files.
YAML relies on space indentation so for writing the YAML file we use VS Code Editor to get a space proper indentation.
YAML uses Key: Value to write data.
Example: Create a YAML file that contains the record of the Graduated student.
---
Name : Amit
Course : BCA
DevOps Tools :
- Jenkins
- Kubernetes
- Docker
- Maven
- SonarQube
- AWS Cloud
Installation of docker-compose :
$ sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
This command is used to download the current release of the docker-compose file.
$ sudo chmod +x /usr/local/bin/docker-compose
This command is used to give the executable permission to the docker-compose.
$ docker-compose --version
To check the version of the docker-compose.
So, let's understand the docker-compose with the simple example where we are deploying the nginx web server on port 80.
version: "3.9"
services:
web:
image: nginx
ports:
- 8080:80
database:
image: mysql
In this sample docker-compose.yml
let's understand the keywords of this file.
version is to be used to define the docker version.
services are used to define the containers or the application's services. Under services, the web is used to define the website image and ports and under the database, we are giving the database image name.
Under docker-compose.yml we can define many things like volumes, environments for containers, etc.
To run this docker-compose.yml
file execute docker-compose up -d
(-d is used to run in detached mode) to pull the image and start the container. This single command can do both tasks and to delete this container just execute docker-compose down
To validate that the docker-compose.yml file indentation is properly correct and the docker-compose file has everything defined or not execute docker-compose configure
.
To see that the container is running or not execute docker ps
. You can see that the container is deployed on 8080 port number.
And here it is nginx server is currently running on an 8080 port.
Now to stop the container we have to execute docker-compose down
. Now the nginx will not be running as the container is stopped.
So, this is all about the docker-compose. We will see another detailed example of docker-compose. So, stay tuned for the next blog.
Now, I am taking another example where we are not using sudo
the command as a normal user. So for this, we have to assign permission to the docker sudo usermod -a -G docker $USER
and then do sudo reboot
to reboot the Ubuntu machine.
Let's pull the image from the DockerHub by docker pull
command. We are executing this with sudo command as a normal user.
$ docker pull nginx
We successfully pulled the image from the DockerHub. Now, we have to inspect the running container and see the exposed ports. To inspect the running container execute docker inspect <Container ID/Name>
. But before that we had to run the container.
docker run -d --name nginx-webserver -p 8080:80 nginx:latest
To check running containers execute docker ps
and now to inspect the running container we execute :
docker inspect <ContainerID/Name>
Here, you can see the ports by inspecting the container.
By executing docker start/stop
command we can start and stop the containers.
By executing docker logs <ContainerId/Name>
command we can view the container's log and by doing docker rm <ContainerId/Name>
we can remove the containers.
So, that's it for today's task !! Stay Tuned for the next blog in which we are making our own docker-compose file with our own image.
Follow me on Hashnode for more Linux and DevOps Blogs !!
You can connect with me on Twitter !!
THANK YOU :)
HAPPY LEARNING :)