Lab 3_ Using Docker Compose for Multi-Container Applications
Lab 3_ Using Docker Compose for Multi-Container Applications
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:
Pre-requisites
cd multi-container-app
Explanation: This directory will store all configuration files and data related to this lab.
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.
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.
Explanation:
○ Port 8080 on your machine maps to port 80 on the Nginx container, allowing you
to access the web server.
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;
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.
docker-compose ps
1. Explanation:
--scale: Allows you to run multiple instances of a service (useful for scaling).
1. Explanation:
○ docker-compose stop: Stops the containers gracefully while preserving their
states.
docker-compose down
2. Explanation:
Step 8: Clean Up
1. Explanation:
○ --volumes: Removes any volumes created by the application, cleaning up
storage.
Conclusion