Skip to Content
Start Two Services With docker compose up
Beginner
0 likes
Tutorials

Start Two Services With docker compose up

Write a compose.yaml for a web server and a checker, start both, read the result and remove them. After this, you can run a two-service project from one file.

Docker Compose starts several containers from one file, with one command. This page runs two services: a web server, and a small checker that calls it by name to prove the two can talk. It ends with the three commands you need to look at it and to tear it down.

The Compose file

Make a folder and save this as compose.yaml in it. Use spaces for the indentation. YAML does not allow tabs.

name: two-svc
services:
  web:
    image: nginx:1.30-alpine
    ports:
      - "8086:80"
  checker:
    image: alpine:3.24
    command: ["sh", "-c", "sleep 2; wget -qO- http://web | grep title"]
    depends_on:
      - web

What each part does:

  1. name is the project name. It prefixes every container, network and volume, which keeps this project apart from others.
  2. Under services, web and checker are the two services. The service name is also its hostname on the project's network.
  3. ports publishes container port 80 on your port 8086. The left number is your side. The right one is the container.
  4. command replaces the image's default command. Here the checker waits two seconds, fetches the web page from http://web and prints the title line.
  5. depends_on starts web first. It waits for the container to start, not for nginx inside it to be ready. That is why the checker sleeps for two seconds first.

Check the file before you run it

docker compose config reads the file and prints the result. If it says nothing is wrong, the file is valid.

docker compose config --services
web
checker

Start both services

docker compose pull --quiet

-d runs the services in the background. Without it, the logs take over your terminal until you press Ctrl+C.

docker compose up -d
Network two-svc_default
Container two-svc-web-1
Container two-svc-checker-1

Compose created a network first, then the containers. Your output may also show "Created", "Started" or "Healthy" on those lines, and it varies by version. Give the checker a second to run, then look at the services.

docker compose ps --all --format "table {{.Service}}\t{{.State}}"
SERVICE   STATE
checker   exited
web       running

web keeps running. checker has finished its one job, so it is exited, which is correct here. A service that should stay up and shows exited is the problem to look at. Read the checker's log, and call the web page from your own browser at http://localhost:8086.

docker compose logs --no-log-prefix checker
(curl.exe -s http://localhost:8086) -match "<title>"
<title>Welcome to nginx!</title>
<title>Welcome to nginx!</title>

The first line came from inside the project, by the name web. The second came from your computer, through the published port. Two routes, one server.

Error: the file is not valid

A wrong indent is the usual mistake. This copy has the image line moved out of its service.

services:
  web:
  image: nginx:1.30-alpine
docker compose -f compose.bad.yaml config --quiet
services.image must be a mapping

The message names the key it is unhappy about: services.image. Compose read image as a service of its own, because it sits at the same level as web, and a service has to contain settings. Compare the spaces with the working file. Indentation decides which service a setting belongs to.

Stop and remove it

docker compose down
Container two-svc-checker-1
Container two-svc-web-1
Network two-svc_default

down stops and removes the containers and the network that up created. It leaves images and named volumes alone. To remove volumes too, add -v, and only when you are sure you do not need the data.

⚠ Warning: A port can only belong to one program. If docker compose up fails with "port is already allocated", another container or program already uses 8086. Change the left number, for example to 8087:80, and run it again.

💡 Tip: On Linux the file and the commands are identical. docker compose (with a space) is the current command. The old docker-compose with a hyphen is the retired version 1.

Related course: Docker for Beginners.

About

Write a compose.yaml for a web server and a checker, start both, read the result and remove them. After this, you can run a two-service project from one file.

Responsible
John G
Last Update
08/01/2026
Reading Time
4 min
Was this helpful?
​ 0 ​ 0

Comments (0)

Sign in to comment

No comments yet.