Lab 3: Using Docker Compose for Multi-Container
Applications
Objective
This lab demonstrates how to use Docker Compose to deploy and manage a multi-container
application consisting of a web server (Nginx) and a database (MySQL). By completing this
lab, you will:
1. Understand the structure of a docker-compose.yml file.
2. Learn to use Docker Compose commands.
3. Deploy and interact with a multi-container application.
Pre-requisites
1. Docker Desktop installed on Windows.
2. Docker Compose included with your Docker installation.
3. Basic understanding of Docker concepts such as images, containers, and networks.
Step 1: Navigate to Working Directory
1. Open a terminal or file explorer.
2. Create a new folder for your project:
mkdir multi-container-app
cd multi-container-app
Explanation: This directory will store all configuration files and data related to this lab.
Step 2: Create a docker-compose.yml File
1. Inside the multi-container-app folder, create a file named docker-compose.yml.
2. Open the file in a text editor and add the following content:
version: '3.8'
services:
web:
image: nginx
ports:
- "8080:80"
networks:
- app-network
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- app-network
networks:
app-network:
driver: bridge
3. Save and close the file (Be sure to save as a .yml file).
Explanation:
○ version: '3.8': Specifies the Docker Compose file format version.
○ services: Defines the containers to deploy (web and db).
■ web: Uses the Nginx image and maps port 80 on the container to port
8080 on the host.
■ db: Uses the MySQL 5.7 image with an environment variable to set the
root password.
○ networks: Creates a custom network (app-network) to allow communication
between the containers.
Step 3: Start the Multi-Container Application
Run the following command to start the application:
docker-compose up
1. Explanation:
○ docker-compose up: Builds and starts all containers defined in the
docker-compose.yml file.
○ Docker will pull the required images if they are not already available locally.
Expected Output:
● Docker Compose will output logs from both containers as they start.
Step 4: Verify the Web Server
1. Open a browser and navigate to http://localhost:8080.
2. You should see the default Nginx welcome page.
Explanation:
○ Port 8080 on your machine maps to port 80 on the Nginx container, allowing you
to access the web server.
Step 5: Interact with the Database (Optional)
Access the MySQL container using the following command:
docker exec -it <db-container-name> mysql -uroot -pexample
1. Replace <db-container-name> with the name of the MySQL container (you can find
it using docker ps).
Run the following MySQL command to list databases:
SHOW DATABASES;
2. Exit the MySQL shell:
EXIT;
3. Explanation:
○ docker exec: Executes a command inside a running container.
○ mysql -uroot -pexample: Connects to the MySQL instance using the root
user and the password set in the environment variable.
Step 6: Explore Additional Docker Compose Commands
List all running containers in the application:
docker-compose ps
Restart specific containers:
docker-compose restart web
Scale a service to multiple instances:
docker-compose up --scale web=3
1. Explanation:
○ docker-compose ps: Shows the status of containers managed by Docker
Compose.
--scale: Allows you to run multiple instances of a service (useful for scaling).
Step 7: Stop the Application
Stop the containers without deleting them:
docker-compose stop
1. Explanation:
○ docker-compose stop: Stops the containers gracefully while preserving their
states.
To stop and remove the containers, run:
docker-compose down
2. Explanation:
○ docker-compose down: Stops and removes all containers, networks, and
volumes created by Docker Compose.
Step 8: Clean Up
Ensure the containers and networks are removed:
docker-compose down --volumes
1. Explanation:
○ --volumes: Removes any volumes created by the application, cleaning up
storage.
Conclusion
In this lab, you:
1. Created a docker-compose.yml file for a web server and database.
2. Started and interacted with a multi-container application.
3. Learned to manage containers using Docker Compose commands.