Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

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

Describe several containers in one compose.yaml file and start them with one command. Install Compose, write a first file, and run it safely on a server.

3 min readBy Daniel

Reviewed by the AmazingRDP technical team

Deploying services with Docker Compose

Docker needs a server of your own. A Linux VPS gives you root access. See Linux VPS from $6.99

A real application is rarely one container. A website needs a web server, an application, a database and perhaps a cache. Docker Compose lets you write all of them down in one file and start, stop and update them together.

Install

Compose is a plugin of Docker and comes with it when Docker is installed from Docker's own repository. The convenience script does that on a fresh server:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
docker compose version

The command is docker compose, with a space. The older separate program docker-compose, with a hyphen, is no longer developed.

A first file

Make a folder for the project and a file named compose.yaml in it:

mkdir ~/site && cd ~/site
nano compose.yaml
services:
  web:
    image: nginx:stable
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    depends_on:
      - app
    restart: unless-stopped
  app:
    image: ghcr.io/example/app:1.4
    env_file: .env
    depends_on:
      - db
    restart: unless-stopped
  db:
    image: postgres:17
    env_file: .env
    volumes:
      - dbdata:/var/lib/postgresql/data
    restart: unless-stopped
volumes:
  dbdata:

Three services. Only web publishes a port to the outside. The services reach each other by name: the application connects to the database at the host name db, on a private network that Compose creates. The application image is a placeholder; use your own.

Keep secrets out of the file

Passwords go into a file named .env next to it, which you do not put into version control:

POSTGRES_PASSWORD=change-this-long-password
POSTGRES_DB=app
chmod 600 .env

Run it

CommandDoes
docker compose up -dCreates and starts everything in the background
docker compose psShows what is running
docker compose logs -f appFollows one service's log
docker compose exec db psql -U postgresRuns a command inside a service
docker compose pull && docker compose up -dUpdates images and restarts what changed
docker compose downStops and removes the containers; data in volumes stays
docker compose down -vAlso deletes the volumes. This destroys the database

Things a server needs that a laptop does not

  • Pin versions. Write postgres:17, not postgres:latest; otherwise an update may bring a new major version unasked.
  • Publish as little as possible. A database needs no ports line. For a port only the server itself should reach, write "127.0.0.1:5432:5432".
  • Know that Docker bypasses ufw. A published port is open to the internet even when the Ubuntu firewall says deny, because Docker writes its own rules. Bind to 127.0.0.1 or use the provider's firewall.
  • Back up the volumes. For a database, take a dump on a schedule: see scheduling tasks with cron.
  • Limit the logs so that they cannot fill the disk, with a logging section setting max-size.
  • Add a health check to services others depend on; depends_on alone only waits for a container to start, not for it to be ready.

Several projects on one server

Give each project its own folder. Compose names containers and networks after the folder, so projects do not collide. Only one service on the server can own port 80; put a reverse proxy in front when several sites share it.

Where it stops

Compose manages one server. It does not spread services over several machines or move them when a machine fails; that is the job of Kubernetes and similar systems, which are worth their complexity only at a larger scale. For a graphical view of the same containers, see Portainer.

Docker needs root access and a kernel of your own, so it runs on a VPS or dedicated server and not on a shared remote desktop. See our Linux VPS plans.

Questions

What is Docker Compose?

A tool that describes several containers, their networks and their storage in one YAML file and manages them together.

What is the difference between docker-compose and docker compose?

docker compose is the current plugin built into Docker. docker-compose is the old separate program.

How do I start all services?

Run docker compose up -d in the folder with the compose file.

Does docker compose down delete my data?

Not data in named volumes. Adding -v deletes the volumes too.