📖 Scenario: You are setting up a simple web application using Docker Compose. This app has a web server and a database. You want to learn how advanced Docker Compose features help manage complex setups easily.
🎯 Goal: Build a Docker Compose file step-by-step that uses advanced features like environment variables, volumes, and depends_on to manage a web app and database.
📋 What You'll Learn
Create a basic Docker Compose file with two services: web and db
Add environment variables to the db service
Use a volume to persist database data
Use depends_on to control service startup order
💡 Why This Matters
🌍 Real World
Docker Compose helps developers run multi-container apps easily on their computers or servers. Advanced features make managing complex apps simpler and more reliable.
💼 Career
Understanding Docker Compose and its advanced features is essential for DevOps roles, as it helps automate and manage containerized applications efficiently.
Progress0 / 4 steps
1
Create basic Docker Compose services
Create a Docker Compose file named docker-compose.yml with two services: web using image nginx:latest and db using image mysql:5.7.
Docker
Hint
Use the services key to list your containers. Each service needs a name and an image.
2
Add environment variables to the database service
Add environment variables MYSQL_ROOT_PASSWORD: rootpass and MYSQL_DATABASE: mydb under the db service in docker-compose.yml.
Docker
Hint
Use the environment key under the db service to set variables.
3
Add a volume to persist database data
Add a volume named db_data to the db service that maps to /var/lib/mysql inside the container. Also, declare the volume db_data at the bottom of the file.
Docker
Hint
Use the volumes key under db to map the volume. Declare the volume at the bottom with the same name.
4
Add depends_on to control service startup order
Add depends_on to the web service so it starts after the db service in docker-compose.yml.
Docker
Hint
Use depends_on under web to list db.
Practice
(1/5)
1. What is the main benefit of using advanced features in Docker Compose for multi-container applications?
easy
A. They reduce the size of container images.
B. They make containers run faster individually.
C. They simplify managing multiple containers together.
D. They automatically update Docker itself.
Solution
Step 1: Understand Docker Compose purpose
Docker Compose is designed to manage multiple containers as one app.
Step 2: Identify advanced features role
Advanced features help organize, scale, and control these containers easily.
Final Answer:
They simplify managing multiple containers together. -> Option C
Quick Check:
Advanced Compose features = Simplify multi-container management [OK]
Hint: Think: Compose = managing many containers easily [OK]
Common Mistakes:
Confusing image size with Compose features
Thinking Compose speeds up single containers
Assuming Compose updates Docker engine
2. Which of the following is the correct syntax to scale a service named web to 3 instances using Docker Compose CLI?
easy
A. docker-compose scale web 3
B. docker-compose start web -n 3
C. docker-compose run --scale web 3
D. docker-compose up --scale web=3
Solution
Step 1: Recall Docker Compose scale syntax
The correct command to scale is: docker-compose up --scale servicename=number
Step 2: Match options with correct syntax
docker-compose up --scale web=3 matches this syntax exactly.
Final Answer:
docker-compose up --scale web=3 -> Option D
Quick Check:
Scaling uses 'up --scale' syntax [OK]
Hint: Use 'docker-compose up --scale service=num' to scale [OK]
Common Mistakes:
Using 'scale' as a separate command
Confusing 'run' with 'up' for scaling
Using 'start' which doesn't support scaling
3. Given this snippet in a docker-compose.yml file:
When running docker-compose up, only one db container starts. Why?
medium
A. The 'deploy' section is ignored by 'docker-compose up'.
B. You must specify replicas in 'docker-compose up' command.
C. Postgres image does not support multiple replicas.
D. Port mapping conflicts prevent multiple containers.
Solution
Step 1: Understand 'deploy' section scope
The 'deploy' key is only used by Docker Swarm, not by 'docker-compose up'.
Step 2: Explain why only one container starts
'docker-compose up' ignores 'deploy', so it starts one container by default.
Final Answer:
The 'deploy' section is ignored by 'docker-compose up'. -> Option A
Quick Check:
'deploy' ignored by 'docker-compose up' = one container [OK]
Hint: 'deploy' works only with Swarm, not 'docker-compose up' [OK]
Common Mistakes:
Thinking 'docker-compose up' can scale with 'deploy'
Assuming port conflicts cause single container
Believing image limits replicas
5. You want to deploy a web app with a database using Docker Compose. You need to: - Scale the web service to 4 instances - Limit each web container to 1 CPU and 1GB RAM - Ensure the database runs only once with persistent storage
Which Compose features help you achieve this setup?
hard
A. Use 'deploy.replicas' and 'deploy.resources.limits' for web; use volumes for DB persistence.
B. Use 'scale' in 'docker-compose up' for DB; use 'deploy' for web resource limits.
C. Use environment variables to limit CPU and RAM; scale DB with 'deploy.replicas'.
D. Use 'depends_on' to scale web; use 'ports' to limit DB resources.
Solution
Step 1: Identify scaling and resource control for web
Use 'deploy.replicas' to scale web to 4 and 'deploy.resources.limits' to limit CPU and RAM.
Step 2: Ensure single DB instance with persistence
Set DB replicas to 1 and use volumes to keep data persistent.
Final Answer:
Use 'deploy.replicas' and 'deploy.resources.limits' for web; use volumes for DB persistence. -> Option A
Quick Check:
Scaling + resource limits + volumes = Use 'deploy.replicas' and 'deploy.resources.limits' for web; use volumes for DB persistence. [OK]
Hint: 'deploy' controls scale and limits; volumes keep DB data safe [OK]
Common Mistakes:
Trying to scale DB instead of web
Using environment variables for resource limits
Confusing 'depends_on' with scaling or resource control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.