Docker Compose
Docker Compose is a tool that simplifies the management of multi-container Docker applications. With Compose, you define a multi-container environment in a YAML file (docker-compose.yml
) and then use simple commands to build, start, and manage the entire stack. This is particularly useful for applications with multiple components, like a web server, database, cache, and more.
Key Concepts of Docker Compose
-
Service: A service is a container that you define in your
docker-compose.yml
file. Each service represents a part of your application, such as a web server or a database. -
Network: Docker Compose automatically creates a network for all the services, enabling them to communicate with each other by service names.
-
Volume: Volumes in Docker Compose allow data persistence across container restarts.
Basic Structure of a docker-compose.yml
File
A the docker-compose.yml
file is used to define all the services and their configurations. Here’s a simple structure:
Explanation:
-
version: Specifies the Compose file format version.
-
services: Defines the different containers (services) that make up the application.
-
web: An Nginx container exposed on port 8080.
-
db: A MySQL container with an environment variable for the root password and a volume for persistent data storage.
-
-
volumes: Defines named volumes (like
db-data
) for persistent storage.
Docker Compose for a Web App
Let’s create a simple multi-container application with Docker Compose. This application will consist of:
-
A web service running on an Nginx server.
-
A database service running MySQL.
1. Create a Docker Compose File (docker-compose.yml
)
Create a directory for your project, and inside it create a docker-compose.yml
file:
Add the following content to docker-compose.yml
:
Explanation of the Compose File:
-
web:
-
Runs the official Nginx image.
-
Exposes port 8080 on the host to port 80 on the container (Nginx default port).
-
Connects to the
my-network
network.
-
-
db:
-
Runs the official MySQL 5.7 image.
-
Sets the root password for MySQL.
-
Attaches to the same
my-network
network. -
Uses a named volume
db-data
for persistent MySQL data.
-
-
volumes:
-
Defines
db-data
for persistent MySQL data storage.
-
-
networks:
-
Defines a custom bridge network (
my-network
) to allow containers to communicate with each other.
-
2. Start the Application with Docker Compose
In the same directory as the docker-compose.yml
file, run the following command to start the containers:
-
The
-d
flag runs the containers in detached mode (in the background).
This will download the required images (nginx
and mysql:5.7
), create the services, and start the containers.
You can check the running containers with:
3. Access the Web Service
Once the containers are running, you can access the Nginx web server by opening your browser and navigating to:
This will show the default Nginx welcome page.
4. Access the MySQL Database
If you want to access the MySQL container to check its logs or manage the database, you can use:
This opens a bash shell inside the MySQL container. You can then log into MySQL:
Enter the password (rootpassword
), and you’ll be inside the MySQL command-line interface.
5. Stopping the Services
To stop the services, you can use:
This will stop and remove all the containers, networks, and volumes created by docker-compose up
.
6. Rebuilding the Services
If you make changes to the docker-compose.yml
file (e.g., update the service configuration or add a new service), you can rebuild the services with:
This will rebuild the services and apply any changes.
Additional Docker Compose Commands
-
List Running Containers:
-
View Logs for Services:
To view logs for a specific service:
-
Scale Services: You can scale services (e.g., running multiple instances of a service):
-
Run a One-Off Command in a Service: For example, running a shell inside the web service:
Use Cases for Docker Compose
-
Multi-Container Applications: Compose simplifies defining and running applications with multiple services (e.g., a web server, database, caching, etc.).
-
Development Environment: Developers use Docker Compose to quickly set up and tear down environments without needing to manually configure each container.
-
Testing & Continuous Integration: Compose is ideal for spinning up testing environments for unit tests, integration tests, and CI/CD pipelines.
Example: Adding a Simple Web App to Docker Compose
Let’s extend the current docker-compose.yml
file by adding a simple web application to the web
service. For example, using a simple Node.js app with Express.
-
Create a
Dockerfile
for the Node.js App:In the same directory, create a
Dockerfile
For the Node.js app: -
Create
app.js
(a simple Express app): -
Update
docker-compose.yml
to build the app service from the Dockerfile: -
Start the Application:
Now, run the application with:
Visit
http://localhost:8080
In your browser, and you should seeHello, Docker Compose!
.
Summary
-
Docker Compose makes managing multi-container applications easy by defining all services, networks, and volumes in a single
docker-compose.yml
file. -
You can start, stop, and manage the entire stack with simple commands like
docker-compose up
anddocker-compose down
. -
It's highly useful for development, testing, and CI/CD setups.