Day 18 Task: Docker for DevOps Engineers
Docker Compose
Docker Compose is a tool that simplifies the process of managing multi-container Docker applications. It allows you to define and run multiple Docker containers as a single service using a YAML configuration file (docker-compose.yml
). With Docker Compose, you can easily manage complex applications that require several interconnected services, such as a web server, a database, and a caching layer.
Key Features of Docker Compose:
Multi-Container Management: You can define multiple services (containers) in a single
docker-compose.yml
file. For example, a web application with a frontend, backend, and database can be managed together.Service Definition: Each service (like a web server or a database) is defined in the
docker-compose.yml
file. You specify the image, ports, volumes, environment variables, and dependencies for each service.Network Configuration: Docker Compose automatically creates a network for your containers, allowing them to communicate with each other using service names.
Volume Management: Volumes can be defined in the Compose file to persist data, ensuring that data is not lost when containers are stopped or recreated.
Dependency Management: You can define the order in which services are started, ensuring that dependent services (like a database) are up and running before others (like a web server) start.
Single Command to Start All Services: Instead of running multiple
docker run
commands for different containers, you can start all services with a single command (docker-compose up
).
What is YAML?
YAML (short for "YAML Ain't Markup Language") is a human-readable data format commonly used for configuration files and data exchange between languages with different data structures. YAML is designed to be easy to read and write, making it a popular choice for configuration files in many programming languages and applications.
Key Characteristics of YAML:
Human-Readable: YAML is designed to be easy for humans to read and write. It uses indentation to represent data structure, making it visually simple to understand.
Key-Value Pairs: YAML uses key-value pairs to represent data. Keys and values are separated by a colon and a space (key: value
).
Whitespace and Indentation: Indentation (using spaces, not tabs) is critical in YAML to define structure. Nested elements are indented relative to their parent elements.
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.
version '3'
services:
web:
image: 'nginx:latest'
ports:
- "80:80"
mysql:
image: 'mysql:5.7'
ports:
- "3306:3306"
environments:
MYSQL_ROOT_PASSWORD: 'test@123'
MYSQL_DATABASE: 'devops'
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.
docker pull mysql
While running 'docker pull mysql' getting this kind of permission denied message that means intially we don't have access to run it.
How to resolve it -
sudo usermod -aG docker $USER
After adding the permissions to the docker group reboot your terminal and again try to run 'docker pull mysql'
- 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.
Run the mysql container and see the logs related to the container.
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
Docker Logs: The docker logs
command is used to fetch and display the logs of a Docker container. This is particularly useful for debugging, monitoring, and understanding the behavior of applications running within containers.
docker logs 7e7706b0a0df
- Use the
docker stop
anddocker start
commands to stop and start the container.
To stop the docker container, first we need to run processes command for docker to check the processes.
- -a means show all the processes means exited and running containers.
docker ps -a
Docker stop: Docker stop command is used to stop the running container.
docker stop <container-id>
Docker start - Docker start command start the docker container again.
docker start <container_id>
- Use the
docker rm
command to remove the container when you're done.
docker remove <container_id>
How to Run Docker Commands Without Sudo?
Make sure Docker is installed and the system is updated (This was already completed as part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine.
Follow for more update:)
Subscribe to my newsletter
Read articles from Vibhuti Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vibhuti Jain
Vibhuti Jain
Hi, I am Vibhuti Jain. A Devops Tools enthusiastic who keens on learning Devops tools and want to contribute my knowledge to build a community and collaborate with them. I have a acquired a knowledge of Linux, Shell Scripting, Python, Git, GitHub, AWS Cloud and ready to learn a lot more new Devops skills which help me build some real life project.