"How
Cheap Dedicated Server

How to Use Docker Compose on a Linux Server to Deploy Multiple Services

Managing multiple containerized applications can get complicated—especially on a production Linux server. Docker Compose simplifies this by letting you define and manage multi-container applications using a single YAML file. Whether you’re deploying a web app with a database or orchestrating microservices, Docker Compose is a powerful tool for managing your stack.

What is Docker Compose?

Docker Compose is a tool that allows you to define and run multi-container Docker applications. With it, you describe the services, networks, and volumes your application uses in a docker-compose.yml file.


Step-by-Step Guide to Deploying Multiple Services

1. Install Docker and Docker Compose

If Docker isn’t already installed on your server:

Install Docker

sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker --now

Install Docker Compose:
Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

2. Create a docker-compose.yml File

Here’s a simple example to deploy a web application with Nginx and a PostgreSQL database:
Create a docker-compose File

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html

  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: app_db
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

3. Start the Services

From the directory where your docker-compose.yml is located:

docker-compose up -d

This command will pull the images, create containers, and start them in detached mode.


4. View and Manage Services

Check running containers:

docker-compose ps

Stop the stack:

docker-compose down

View logs:

docker-compose logs -f

Benefits of Using Docker Compose on a Linux Server

  • Simplified deployment: Manage all your services from one file.
  • Repeatability: Easy to recreate environments.
  • Isolation: Containers are kept isolated but networked together.
  • Scalability: Add more services or replicas easily.

 

How to Use Docker Compose on a Linux Server to Deploy Multiple Services (F.A.Q)

Can I use Docker Compose in production?

 

Yes. While Docker Swarm or Kubernetes are better suited for large-scale production environments, Compose works well for small to medium apps.

 

How do I persist data across restarts?

Use named volumes or bind mounts in your docker-compose.yml to retain data.

 

Can services communicate with each other?

Yes, Docker Compose automatically sets up a shared network so services can use service names to communicate.

 

How do I update a service?

Modify the docker-compose.yml file or pull a new image, then run docker-compose up -d again. Compose will recreate the affected services.