Docker Compose builds, runs, and manages a local application made of multiple containers. It replaces repeated per-container Docker commands with one configuration file and commands for the whole application. Use it for development and testing; Kubernetes is the production choice here. section 4.3.1 section 4.3.6
A Docker Compose file, usually docker-compose.yaml, sits at the application root. Its services define the containers to run. A service can build an image from its own Dockerfile or use a published image; configuration can include ports, environment variables, volumes, and restart behavior. Start with one service, then add others without changing how you boot the application. section 4.3.2 section 4.5.2
Listing 4.1 Docker Compose file for our microservice
version: '3' #1
services: #2
video-streaming: #3
image: video-streaming #4
build: #5
context: ./video-streaming #6
dockerfile: Dockerfile #7
container_name: video-streaming #8
ports: #9
- "4000:80" #10
environment: #11
- PORT=80 #12
restart: "no" #13
The first configuration builds a video-streaming image from a Dockerfile in its service directory, maps host port 4000 to container port 80, and sets PORT=80. section 4.3.2
Run these commands from the directory containing docker-compose.yaml:
- Use `docker compose up --build` to build images and start the application. Keep `--build` when testing code changes: without it, a restart can reuse an older image. section 4.3.3
- Use `docker compose ps` to list the application's running containers, or `docker compose stop` to stop them while retaining them for inspection. section 4.6
- Use `docker compose down` to stop and remove the application. After Ctrl-C, run `down` rather than relying on Ctrl-C alone for cleanup. section 4.3.5 section 4.6
Grow a local application
Add a storage microservice as another service, then add a `db` service using the published `mongo:7.0.0` image. Set the streaming service's database host through an environment variable such as `DBHOST=mongodb://db:27017`. The database container uses port 27017 for connections from other containers; a host-port mapping can also expose it on the development computer. section 4.4.3 section 4.5.2
For faster iteration, configure a development Dockerfile and volumes that share source code between the host and containers. Edits can then reach a running service without rebuilding the whole application for every change. A separate volume can retain the npm cache across container recreation. section 5.5 section 5.5.4
Listing 5.5 Updating the Docker Compose file for live reload
version: '3'
services:
--snip--
history: #1
image: history
build:
context: ./history
dockerfile: Dockerfile-dev #2
container_name: history
volumes: #3
- /tmp/history/npm-cache:/root/.npm:z #4
- ./history/src:/usr/src/app/src:z #5
ports:
- "4002:80"
environment:
- PORT=80
- NODE_ENV=development
restart: "no"
This service configuration shows `Dockerfile-dev`, a source-code volume, and an npm-cache volume working together for development. section 5.5.4
Compose keeps the local setup quick to start, stop, and switch between projects. It is not the production deployment mechanism in this setup: running it on a cloud VM would be cumbersome to automate, while Kubernetes handles production containers. section 4.3.6 section 6.8.8














