Your First Compose File
A first compose file has one service: an image, a port mapping, and a name. docker compose up starts it, and docker compose down stops it and removes its containers.
One service
A web service from the nginx image, on host port 8111.
services:
web:
image: nginx:1.27-alpine
ports:
- "8111:80"Start it
This command starts the service in the background.
docker compose -p g6proj up -dExample
docker compose -p g6proj up -d >/dev/null
docker compose -p g6proj ps --format "{{.Name}} {{.Image}}"
curl -s http://localhost:8111/ | head -3
docker compose -p g6proj downKey points
- services: the block that lists every service in the file.
- image: the image the service runs from.
- ports: the host-to-container port mapping, in quotes.
- Use docker compose up -d to start in the background.
- Use docker compose down to stop and remove the containers.
- Use docker compose ps to list the services and their state.
In this example, Compose starts the web service, lists it by name and image, and curl fetches its welcome page. docker compose down then stops and removes the containers. The page proves the one-service file works end to end.
Note
Compose names containers from the project and service names, like g6proj-web-1.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does docker compose down do?