Embarking on the Docker voyage for DevOps engineers! In our earlier section, we explored the process of crafting a Dockerfile and transmitting it to a repository. Presently, let's plunge into the realm of Docker Compose, unraveling key tasks to elevate your proficiency in Docker
Docker Compose
Docker Compose is a utility crafted to facilitate the definition and sharing of multi-container applications.
Through Compose, a YAML file can be generated to outline the services, allowing for the effortless deployment or dismantling of the entire setup with a single command.
What is YAML?
YAML, a data serialization language commonly employed for crafting configuration files, has dual interpretations.
Some view YAML as "yet another markup language," while others see it as "YAML ain't markup language," emphasizing its focus on data rather than documents.
The popularity of YAML in programming stems from its human-readable and easily comprehensible nature. Files using YAML adopt either the .yml or .yaml extension.
Let's jump into the tasks to understand this concept in more detail.
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.
Environmental setup
Solution - We create a
docker-compose.yml
file to define your services, networks, and other configurations. Here's a basic example:Here we have sets up an Nginx web server and a PostgreSQL database. The
docker-compose up
command will start both containers.
Service Links
In the preceding phase, we generated a fundamental docker-compose.yml file featuring two services:
a. web server (nginx)
b. database (postgres).
To advance this configuration, we'll delve into service links and environment variables. Service links facilitate communication between services. Let's enhance our docker-compose.yml file to create a connection between the web service and the database service.
The
web
service now has alinks
section, specifying that it's linked to thedb
service. This allows theweb
service to communicate with thedb
service by using the hostnamedb
.
Environment Variables
Environment variables in Docker Compose enable you to configure services with dynamic values. Let's add an environment variable to the
web
service, specifying the welcome message.
Task-2
1. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.
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.
I hope by accomplishing these tasks, we have broadened our understanding of Docker, delving into Docker Compose to proficiently manage multi-container applications and refining our ability to interact with Docker containers.
With our forthcoming articles, we'll delve into more advanced Docker concepts!
Thanks,
Kishor Chavan