📖 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
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
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
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
Hint
Use docker-compose up -d to start the containers in the background. Then use docker-compose ps to list the running containers.
Practice
(1/5)
1. What is the main purpose of using docker-compose.yml in a development environment?
easy
A. To build Docker images only
B. To define and run multiple containers with one command
C. To replace Docker CLI commands completely
D. To monitor container resource usage
Solution
Step 1: Understand Docker Compose role
Docker Compose uses a YAML file to define multiple containers and their settings.
Step 2: Identify main benefit in dev
It allows running all containers together with a single command, simplifying setup.
Final Answer:
To define and run multiple containers with one command -> Option B
Quick Check:
Compose = multiple containers, one command [OK]
Hint: Compose runs many containers with one file and command [OK]
Common Mistakes:
Thinking Compose only builds images
Believing Compose replaces all Docker CLI commands
Confusing Compose with monitoring tools
2. Which of the following is the correct syntax to expose port 8080 on the host mapped to port 80 in a docker-compose.yml service?
easy
A. ports:
- "80:8080"
B. expose:
- "80:8080"
C. expose:
- "8080:80"
D. ports:
- "8080:80"
Solution
Step 1: Understand ports syntax in Compose
The format is "host_port:container_port" to map host port to container port.
Step 2: Match ports for host 8080 to container 80
To expose container port 80 on host port 8080, use "8080:80" under ports.
Final Answer:
ports:
- "8080:80" -> Option D
Quick Check:
HostPort:ContainerPort = 8080:80 [OK]
Hint: Ports use host:container format [OK]
Common Mistakes:
Reversing host and container ports
Using expose instead of ports for host mapping
Incorrect YAML indentation
3. Given this docker-compose.yml snippet, what will be the effect of the volume configuration?
A. The environment variable for db should use list syntax or key-value mapping consistently
B. The volume pgdata is not defined properly under volumes
C. The command for web service is invalid
D. The environment variable for db uses wrong syntax, should be a list
Solution
Step 1: Inspect YAML syntax in command field
The command: nginx -g 'daemon off;' lacks quotes around the full string, causing YAML to parse it as separate unquoted scalars, leading to a syntax error.
5. You want to set up a development environment with a web app and a database using Docker Compose. The web app needs live code updates from your host machine, and the database should persist data between restarts. Which docker-compose.yml configuration correctly achieves this?