Day 18 Task: Docker for DevOps Engineers
Docker Compose
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, spin everything up or tear it all down.
Docker Compose is another best tool for docker to setup multi-container environments. Using this create a single compose file with defining all the containers with there environments. You can easily use single command to build images and run all the containers.
There is the three-step process to work with Docker Compose.
1. Define application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining with all services under application.
3. Run docker-compose up to run all services under applications
You must have Docker Engine installed on your system. If you don’t have already installed, you can install using this command.
sudo apt-get install docker.io -y
sudo apt-get install docker-compose -y
Example docker-compose.yml
file of nginx
# vim docker-compose.yml
-------------------------------------------------------------------------------
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
What is YAML?
YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, 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.
YAML is a popular programming language because it is human-readable and easy to understand.
YAML files use a
.yml
or.yaml
extension.
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.
# vim docker-compose.yml
-------------------------------------------------------------------------------
version: "3.3" # Specifies the version of the Docker Compose file format being used.
services: # Defines the services to be deployed.
web: # Service named 'web'.
image: nginx:latest # Specifies the Docker image to be used for the 'web' service (in this case, the latest version of the Nginx web server).
ports:
- "80:80" # Maps port 80 of the host to port 80 of the 'web' service container. Allows access to the web server running inside the container.
db: # Service named 'db'.
image: mysql # Specifies the Docker image to be used for the 'db' service (in this case, the latest version of MySQL).
ports:
- "3306:3306" # Maps port 3306 of the host to port 3306 of the 'db' service container. Allows access to the MySQL database running inside the container.
environment:
- "MYSQL_ROOT_PASSWORD=test@123" # Sets the environment variable MYSQL_ROOT_PASSWORD to 'test@123' for the MySQL container. This defines the root password for the MySQL database.
The
web
service uses the Nginx image and exposes port 80 of the container to port 80 of the host machine.The
db
service uses the MySQL image and exposes port 3306 of the container to port 3306 of the host machine. It also sets the root password for the MySQL database using theMYSQL_ROOT_PASSWORD
environment variable.The docker-compose build and docker-compose run commands are replaced by this one. If the images are not already present locally, it creates them and launches the containers.
docker-compose up -d
docker ps
Task 2
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 the
usermod
command to give the user permission to Docker). Make sure you reboot the instance after giving permission to 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
anddocker start
commands to stop and start the container.Use the
docker rm
command to remove the container when you're done.
sudo usermod -a -G docker $USER && sudo reboot
docker pull nginx:latest
- run the nginx container
docker run -d --name nginx -p 80:80 nginx:latest
docker ps && docker images
Inspect the container's running processes and exposed ports using thedocker inspect
command.
docker inspect <container_id>
Use the docker logs
command to view the container's log output.
docker logs <container_id>
Use thedocker stop
anddocker start
commands to stop and start the container.
docker stop <container_id>
docker start <container_id>
Use thedocker rm
anddocker kill
commands to remove the container when you're done.
docker kill <container_id>
docker rm <container_id>
How to run Docker commands without sudo?
Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine using
sudo reboot
.