📖 Scenario: You are working as a DevOps engineer for a small web application team. They want to run their web app using multiple containers to handle more users. You will help them set up a simple container orchestration using Docker Compose to run a web server and a database together.
🎯 Goal: Build a Docker Compose setup that runs a web server container and a database container together. You will create the initial Docker Compose file, add configuration for environment variables, define the services, and finally run the containers to see them working together.
📋 What You'll Learn
Create a Docker Compose YAML file named docker-compose.yml
Define two services: web and db
Set environment variables for the db service
Use official Docker images: nginx:latest for web and mysql:8.0 for db
Expose port 80 on the web service to the host
Run the Docker Compose setup and verify both containers are running
💡 Why This Matters
🌍 Real World
Docker Compose is widely used to run multi-container applications locally or in small production environments. It helps developers and DevOps teams manage related containers easily.
💼 Career
Understanding container orchestration basics with Docker Compose is essential for DevOps roles. It prepares you for more advanced orchestration tools like Kubernetes.
Progress0 / 4 steps
1
Create the initial Docker Compose file
Create a file named docker-compose.yml with the version set to '3.8' and define two services: web and db. For now, set the image for web to nginx:latest and for db to mysql:8.0.
Docker
Hint
Use the version key at the top, then define services with web and db each having an image.
2
Add environment variables for the database
In the docker-compose.yml file, add environment variables under the db service: MYSQL_ROOT_PASSWORD set to rootpass and MYSQL_DATABASE set to myappdb.
Docker
Hint
Use the environment key under db and list the variables with their exact names and values.
3
Expose port 80 on the web service
In the docker-compose.yml file, under the web service, add a ports section that maps port 80 of the container to port 80 on the host machine.
Docker
Hint
Use the ports key under web and map the container port 80 to host port 80 using the format "80:80".
4
Run the Docker Compose setup and verify containers
Run the command docker-compose up -d in the terminal to start the containers in detached mode. Then run docker-compose ps to list running containers. Write a print statement that outputs the exact text: Containers web and db are running.
Docker
Hint
Use print("Containers web and db are running") to show the final confirmation message.
Practice
(1/5)
1. What is the main purpose of container orchestration in production environments?
easy
A. To manage and automate deployment, scaling, and operation of multiple containers
B. To write code inside containers
C. To create container images manually
D. To monitor CPU usage on a single container
Solution
Step 1: Understand container orchestration role
Container orchestration automates managing many containers, including deployment and scaling.
Step 2: Compare options
Only To manage and automate deployment, scaling, and operation of multiple containers describes managing and automating container groups, which is orchestration's goal.
Final Answer:
To manage and automate deployment, scaling, and operation of multiple containers -> Option A
Quick Check:
Orchestration = Manage & automate containers [OK]
Hint: Orchestration means managing many containers automatically [OK]
Common Mistakes:
Confusing orchestration with coding inside containers
Thinking orchestration is about creating images manually
Believing orchestration only monitors single container stats
2. Which Docker Swarm command is used to create a new service in a cluster?
easy
A. docker service create
B. docker container run
C. docker swarm init
D. docker network create
Solution
Step 1: Identify command for creating services
The command docker service create is used to start a new service in Docker Swarm.
Step 2: Eliminate other commands
docker container run runs containers but not as a managed service; docker swarm init initializes the swarm; docker network create creates networks.
Final Answer:
docker service create -> Option A
Quick Check:
Service creation = docker service create [OK]
Hint: Service creation uses 'docker service create' command [OK]
Common Mistakes:
Using 'docker container run' to create services
Confusing swarm initialization with service creation
Mixing network commands with service commands
3. What will be the output of the command docker service ls in a Docker Swarm cluster?
medium
A. List of all running containers on the host
B. List of all services running in the swarm with their replicas
C. List of all Docker images available locally
D. List of all networks created in Docker
Solution
Step 1: Understand the command purpose
docker service ls shows all services in the swarm, including how many replicas each has.
Step 2: Compare with other options
It lists services and replicas, not individual containers (<code>docker ps</code>), images (<code>docker images</code>), or networks (<code>docker network ls</code>).
Final Answer:
List of all services running in the swarm with their replicas -> Option B
Quick Check:
docker service ls = list swarm services [OK]
Hint: 'docker service ls' lists swarm services, not containers [OK]
Common Mistakes:
Thinking it lists individual containers
Confusing services with images or networks
Expecting detailed container logs from this command
4. You run docker service scale myservice=5 but only 3 containers start. What is the likely cause?
medium
A. The command syntax is wrong
B. The service name is incorrect
C. Docker daemon is not running
D. Not enough resources on swarm nodes to run 5 replicas
Solution
Step 1: Analyze the scaling command
The command syntax is correct and service name is assumed valid since some containers started.
Step 2: Identify resource constraints
If only 3 replicas run instead of 5, likely the swarm nodes lack enough CPU/memory to start all replicas.
Final Answer:
Not enough resources on swarm nodes to run 5 replicas -> Option D
Quick Check:
Scaling fails due to resource limits [OK]
Hint: Scaling fails if swarm nodes lack resources [OK]
Common Mistakes:
Assuming command syntax is wrong
Thinking service name is invalid if some replicas run
Ignoring resource limits on nodes
5. In a production Docker Swarm, how can you ensure your service stays running even if a node fails?
hard
A. Use 'docker container run' repeatedly on the same node
B. Run the service with a single replica on one node only
C. Set the service replicas to more than one so tasks run on multiple nodes
D. Disable swarm mode to avoid node failures
Solution
Step 1: Understand high availability in swarm
Running multiple replicas spreads tasks across nodes, so if one node fails, others keep running the service.
Step 2: Evaluate options
Single replica or running containers manually does not provide failover. Disabling swarm removes orchestration benefits.
Final Answer:
Set the service replicas to more than one so tasks run on multiple nodes -> Option C
Quick Check:
Multiple replicas = high availability [OK]
Hint: Use multiple replicas for failover in swarm [OK]
Common Mistakes:
Running only one replica expecting failover
Manually running containers without orchestration
Disabling swarm mode to fix node failures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.