Docker
Last updated at 2023-10-22

Docker Compose Multiple Service Minimal Example

ClickUp
Note
AI Status
75%
Last Edit By
Last edited time
Oct 22, 2023 02:17 PM
Metatag
Slug
docker-compose-example
Writer
Published
Published
Date
Oct 22, 2023
Category
Docker

Introduction

In this article, we will explore a minimal example of using Docker Compose to manage and deploy multiple services in a multi-container application.
Docker Compose is a tool that allows you to define and run multi-container Docker applications.

Prerequisites

Before we begin, make sure you have the following installed on your system:

Example Scenario

Let's consider a scenario where we have two services: a web server and a database.
Our web server will be a simple Node.js application, and we will use PostgreSQL as our database.

Project Structure

Create a new directory for our project and navigate into it:
mkdir docker-compose-example cd docker-compose-example
Inside the project directory, create the following files and directories:
docker-compose.yml web/ - Dockerfile - index.js db/ - Dockerfile

Docker Compose Configuration

Open the docker-compose.yml file and add the following configuration:
version: '3' services: web: build: ./web ports: - 8080:8080 depends_on: - db db: build: ./db environment: - POSTGRES_PASSWORD=mysecretpassword
In this configuration, we define two services: web and db.
The web service is built using the Dockerfile located in the web directory.
We expose port 8080 on the host machine, and the service depends on the db service.
The db service is built using the Dockerfile located in the db directory. We also set an environment variable POSTGRES_PASSWORD for the PostgreSQL container.

Web Service Configuration

Open the web/Dockerfile file and add the following configuration:
FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD [ "node", "index.js" ]
In this configuration, we use the official Node.js Docker image as our base image.
We set the working directory to /app and copy the package.json and package-lock.json files.
We run npm install to install the dependencies, and then copy the rest of the application files.
Finally, we start the web server by running node index.js.

Database Service Configuration

Open the db/Dockerfile file and add the following configuration:
FROM postgres:12 ENV POSTGRES_USER=myuser ENV POSTGRES_DB=mydb COPY init.sql /docker-entrypoint-initdb.d/
In this configuration, we use the official PostgreSQL Docker image as our base image.
We set the environment variables POSTGRES_USER and POSTGRES_DB to configure the PostgreSQL container.
We also copy an init.sql file to the /docker-entrypoint-initdb.d/ directory.
This file will be executed when the container is started and can be used to initialize the database with any necessary data.

Web Server Code

Open the web/index.js file and add the following code:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, Docker Compose!'); }); app.listen(8080, () => { console.log('Server listening on port 8080'); });
This is a simple Node.js web server that listens on port 8080 and responds with a "Hello, Docker Compose!" message when a GET request is made to the root URL.

Running the Application

To run the application, open a terminal and navigate to the project directory. Run the following command:
docker-compose up
This command will build the Docker images for the web and db services (if they haven't been built already) and start the containers.
You should see the logs from both services in the terminal.
Open your web browser and navigate to http://localhost:8080. You should see the "Hello, Docker Compose!" message.

Conclusion

In this article, we explored a minimal example of using Docker Compose to manage and deploy multiple services in a multi-container application.
We saw how to define the services in the docker-compose.yml file, configure the Dockerfiles for each service, and run the application using docker-compose up.
This is just a basic example, but Docker Compose allows for much more complex configurations and orchestration of services.
Refer to the Docker Compose documentation for more advanced usage: https://docs.docker.com/compose/
I hope you found this article helpful.
Happy coding!

References

Discussion (0)

Related Posts