0
0
Dockerdevops~15 mins

docker-compose.yml structure - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding docker-compose.yml Structure
📖 Scenario: You are setting up a simple web application using Docker Compose. This app has a web server and a database. You will create a docker-compose.yml file step-by-step to define these services.
🎯 Goal: Build a docker-compose.yml file that defines two services: web and db. The web service uses the nginx:latest image and the db service uses the mysql:5.7 image.
📋 What You'll Learn
Create a version key with value '3.8' in the YAML file
Define a services section with two services: web and db
Set the image for web to nginx:latest
Set the image for db to mysql:5.7
Add environment variables MYSQL_ROOT_PASSWORD: example to the db service
💡 Why This Matters
🌍 Real World
Docker Compose is used to run multi-container Docker applications easily on your local machine or servers.
💼 Career
Understanding <code>docker-compose.yml</code> files is essential for DevOps roles to manage containerized applications efficiently.
Progress0 / 4 steps
1
Create the YAML version key
Create a docker-compose.yml file and add the version key with the value '3.8' at the top.
Docker
Need a hint?

The version key tells Docker Compose which file format version you are using.

2
Add the services section with web and db
Add a services section below version. Inside services, define two services: web and db.
Docker
Need a hint?

The services section groups all your app's containers.

3
Set images for web and db services
Under the web service, add image: nginx:latest. Under the db service, add image: mysql:5.7.
Docker
Need a hint?

The image key tells Docker which container image to use for each service.

4
Add environment variable to db service
Add an environment section under the db service with MYSQL_ROOT_PASSWORD: example.
Docker
Need a hint?

Environment variables configure the container. Here, it sets the MySQL root password.