Docker Compose Example: Multi-Container Application
In this example, we will set up a simple application with Docker Compose. The application will have:
-
A Web service running a Node.js Express app.
-
A Database service running MySQL.
-
We'll connect both services via a custom network.
We'll also demonstrate the structure of a docker-compose.yml
file, which makes it easier to define and manage multiple containers.
Docker Compose Example
1. Create Project Directory
Create a new project directory and navigate into it:
2. Create docker-compose.yml
File
Create a file named docker-compose.yml
and add the following content:
Explanation:
-
web
service:-
Builds the Docker image from the
./web
directory (we will create this directory next). -
Exposes the port
3000
inside the container to8080
the host machine. -
Connects to the custom network
my-network
. -
depends_on
ensures thedb
Service is ready before theweb
Service starts. -
Environment variables (
DB_HOST
,DB_USER
,DB_PASSWORD
) are passed to the Node.js app for database configuration.
-
-
db
service:-
Uses the official MySQL 5.7 image.
-
Sets up the MySQL root password and creates a default database (
myapp
). -
Uses a volume
db-data
for persistent storage of the database.
-
-
volumes
:-
Defines a named volume
db-data
for persistent database storage.
-
-
networks
:-
Defines a custom network
my-network
to allow communication between containers.
-
3. Create Web Application (Node.js/Express)
Now, create the web service (Node.js Express app).
-
Inside the
my-docker-compose-app
directory, create a new directoryweb
: -
Initialize a new Node.js project:
-
Install Express:
-
Create the
app.js
file inside theweb
directory:-
The app connects to the MySQL database
mysql2
and fetches a simple message from the database. -
The
DB_HOST
,DB_USER
, andDB_PASSWORD
Environment variables are used to configure the database connection.
-
-
Create a
Dockerfile
inside theweb
directory:This Dockerfile:
-
Uses the official Node.js 14 image.
-
Copies the project files and installs dependencies.
-
Exposes port 3000 (default port for the app).
-
Specifies the command to run the app.
-
4. Start the Application with Docker Compose
Return to the root project directory (my-docker-compose-app
), and run:
-
The The
--build
flag ensures that Docker Compose builds the images before starting the containers. -
The
-d
flag runs the containers in detached mode (in the background).
5. Verify the Services
-
Check the status of your containers:
You should see both the
web
anddb
Services listed as running. -
Access the web service:
Open your browser and go to
http://localhost:8080
. You should see:This message is coming from the database through the Node.js app.
6. View Logs
To view the logs of the web service, use:
To view the logs of the database service:
7. Stop the Application
To stop the services and remove the containers, use:
This will stop and remove all containers, networks, and volumes created by Docker Compose.
Summary of Key Concepts
-
Multi-Container Applications: Docker Compose allows you to manage multiple related containers (e.g., a web app and a database) with a single command
docker-compose.yml
file. -
Networking: Services in the
docker-compose.yml
file can communicate with each other over a custom network. -
Volumes: Volumes are used to persist data (e.g., MySQL data) across container restarts.
-
Dependencies: You can specify service dependencies
depends_on
to ensure proper startup order. -
Easy Configuration: Using a
docker-compose.yml
file, you can define and run applications with multiple containers using simple commands likedocker-compose up
.