0
0
Dockerdevops~30 mins

Docker Compose for dev environment - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Compose for dev environment
📖 Scenario: You are a developer who wants to run a simple web application and a database together on your computer. Instead of starting each service manually, you want to use Docker Compose to manage both services easily.
🎯 Goal: Build a docker-compose.yml file that defines a web service using the nginx image and a database service using the mysql image. Then start the services together with Docker Compose.
📋 What You'll Learn
Create a docker-compose.yml file with two services: web and db
The web service uses the nginx:latest image and maps port 8080 on the host to port 80 in the container
The db service uses the mysql:5.7 image with environment variables MYSQL_ROOT_PASSWORD set to example
Use Docker Compose commands to start the services
Display the running services using Docker Compose
💡 Why This Matters
🌍 Real World
Developers often need to run multiple services like web servers and databases together on their local machines. Docker Compose helps manage these services easily with one file and simple commands.
💼 Career
Knowing Docker Compose is essential for developers and DevOps engineers to create reproducible development environments and simplify running multi-container applications.
Progress0 / 4 steps
1
Create the basic Docker Compose file
Create a file named docker-compose.yml with a version set to '3.8' and define a service called web that uses the image nginx:latest.
Docker
Need a hint?

Start with the version key, then add services and under it the web service with the image set to nginx:latest.

2
Add port mapping to the web service
In the docker-compose.yml file, add a ports section under the web service that maps port 8080 on the host to port 80 in the container.
Docker
Need a hint?

Use the ports key as a list and add the string "8080:80" to map the ports.

3
Add the database service with environment variables
Add a new service called db to the docker-compose.yml file. Use the image mysql:5.7 and set the environment variable MYSQL_ROOT_PASSWORD to example under the environment section.
Docker
Need a hint?

Under services, add db with the image and an environment dictionary setting MYSQL_ROOT_PASSWORD.

4
Start services and display running containers
Run the command docker-compose up -d to start the services in detached mode. Then run docker-compose ps to display the running services.
Docker
Need a hint?

Use docker-compose up -d to start the containers in the background. Then use docker-compose ps to list the running containers.