Day 18 of #90DaysOf DevOps Challenge
Aishwarya keshri
Azure DevOps Support Engineer at Microsoft || MS Certified - AZ900 || Ex-Onclusive || NIT Raipur
?Docker Compose
Using docker commands, we can only run and manage a single container at a time, but there can be scenarios when you need to create multiple containers and manage them. It would help if you had a single command solution to create various containers, manage them and then stop them with a single command.
Docker-compose does precisely what I mentioned above. It allows docker to set up the multi-container environment.
There is a three-step process to work with Docker Compose.
1. Define the application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining all services under the application.
3. Run?the docker-compose up?command to run all services under applications.
You can run the docker-compose down command to destroy the above infrastructure.
Pre-requisites:
Docker-compose comes by default with docker installation.
Now, let's learn a little bit about YAML which is very important for a DevOps engineer because it is used in many DevOps tools, like docker, Ansible, K8s, pipelines, etc.
?YAML
YAML is a human-readable data serialization language that is often used for writing configuration files.?
YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.?
Tip: Try to use spaces instead of tabs.
?Docker-Compose Commands
Sample docker-compose file -
version : "3.3"
service
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123":s
build –
The build option is used to build images for services for which the build is defined.
$ docker-compose build # Build all services
$ docker-compose build web # Build single service
up –
Use to create docker containers with available services in the docker-compose.yml file in the current directory. Use the -d switch to launch containers in daemon mode.
$ docker-compose up -d # Create all containers
$ docker-compose up -d web # Create single container
down –
This will stop and delete all containers, network, and associated images for the services defined in a config file.
$ docker-compose down # Restart all containers
$ docker-compose down web # Restart single container
ps –
This will list all containers created for the services defined in a config file with their status, port bindings, and command.
$ docker-compose ps
exec –
This will execute a command to the running container. For example list files in a container associated with?a web?service. (Similar to the docker exec command)
$ docker-compose exec web ls -l
start –
This will start stopped containers of the services defined in the config file.
$ docker-compose start # Start all containers
$ docker-compose start web # Start single container
stop –
This will stop running containers for the services defined in the config file
$ docker-compose stop # Stop all containers
$ docker-compose stop web # Stop single container
restart –
This will restart containers of the services defined in the config file
$ docker-compose restart # Restart all containers
$ docker-compose restart web # Restart single container
pause –
This will pause running containers for the services defined in the config file.
$ docker-compose pause # Pause all containers
$ docker-compose pause web # Pause single container
unpause –
This will start paused containers for the services defined in the config file.
$ docker-compose unpause # Start all paused containers
$ docker-compose unpause web # Start single paused container
rm –
This will remove stopped containers for the services defined in the config file.
领英推荐
$ docker-compose rm # Start all paused containers
$ docker-compose rm web # Start single paused container
?Task
Task 1 -
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.
Example:
In below configuration file, we are creating two containers. One is for the Apache webpage and the other is for the database.
version: '3.3'
services:
? db:
? ? ?image: mysql
? ? ?container_name: mysql_db
? ? ?restart: always
? ? ?environment:
? ? ? MYSQL_ROOT_PASSWORD: pass
? web:
? ? image: apache
? ? build: ./webapp
? ? depends_on:
? ? - db
? ? container_name: apache_web
? ? restart: always
? ? ports:
? ? - 8080:80
In this case, I used the MySQL image so I referred to the below page -https://hub.docker.com/_/mysql
You can refer to the example on the same page.
2. To link two different containers or define the dependency, we can use the depends_on option.
Task 2 -
From user Aish, I cannot do docker ps as it is a non-root user.
So, I added this user to the group - docker.
Need to reboot the system.
Now I can run docker commands without sudo. You can see that the user is added to the docker group.
id
Above command gives details about the current user's uid, gid, and groups to which the user is added.
I can create the docker container with the name C1 and image nginx.
docker run -itd --name C1 nginx
docker inspect C1
The above command gives detailed information about the container and we can find the ExposedPorts under config.
docker top C1
The above command will show the running processes in the container.
You can also use the ps aux command.
docker exec C1 ps aux
docker logs C1
To stop the running container, run the below command -
docker stop C1
To start the stopped container, run the below command -
docker start C1
To remove a running container, run the below command -
docker rm -f C1
Or you can first stop the container and then remove it without the -f (forcefully) flag.
That's it for this Article.
Thank you for reading! ??