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:
-
webservice:-
Builds the Docker image from the
./webdirectory (we will create this directory next). -
Exposes the port
3000inside the container to8080the host machine. -
Connects to the custom network
my-network. -
depends_onensures thedbService is ready before thewebService starts. -
Environment variables (
DB_HOST,DB_USER,DB_PASSWORD) are passed to the Node.js app for database configuration.
-
-
dbservice:-
Uses the official MySQL 5.7 image.
-
Sets up the MySQL root password and creates a default database (
myapp). -
Uses a volume
db-datafor persistent storage of the database.
-
-
volumes:-
Defines a named volume
db-datafor persistent database storage.
-
-
networks:-
Defines a custom network
my-networkto 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-appdirectory, create a new directoryweb: -
Initialize a new Node.js project:
-
Install Express:
-
Create the
app.jsfile inside thewebdirectory:-
The app connects to the MySQL database
mysql2and fetches a simple message from the database. -
The
DB_HOST,DB_USER, andDB_PASSWORDEnvironment variables are used to configure the database connection.
-
-
Create a
Dockerfileinside thewebdirectory: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
--buildflag ensures that Docker Compose builds the images before starting the containers. -
The
-dflag runs the containers in detached mode (in the background).
5. Verify the Services
-
Check the status of your containers:
You should see both the
webanddbServices 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.ymlfile. -
Networking: Services in the
docker-compose.ymlfile 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_onto ensure proper startup order. -
Easy Configuration: Using a
docker-compose.ymlfile, you can define and run applications with multiple containers using simple commands likedocker-compose up.
