Skip to Content
Last repository update 9/10/2025 🎉
DocsDockerDocker Compose

Docker Compose

compose.yaml

A default network is created for all the composed containers. that means all containers that are created will be automatically added to that network.

compose.yaml
name: apiapp services: postgres: image: postgres:14 networks: - db environment: POSTGRES_USER: sonar POSTGRES_PASSWORD: sonar volumes: - postgres_data:/var/lib/postgresql/data sonarqube: image: sonarqube:lts ports: - '9000:9000' - '9092:9092' networks: - db environment: SONAR_JDBC_URL: jdbc:postgresql://postgres:5432/sonar SONAR_JDBC_USERNAME: sonar SONAR_JDBC_PASSWORD: sonarpasswd depends_on: - postgres apiapplication: build: ./ volumes: - /app/node_modules - ./app:/app depends_on: - postgres server: build: context: ./server # system look dir to search for Dockerfile dockerfile: Dockerfile.dev networks: db: volumes: postgres_data:

Additional;

  • If you want to specify -it in compose.yaml, you will have to specify these two fields (mandatory)
    • stdin_open: true - This service needs an open input connection
    • tty: true - Attaching this terminal
    • -it = stdin_open + tty
  • If you want to override the Dockerfile entrypoint, you can specify entrypoint within compose.yaml, then it will override the entrypoint in Dockerfile that is defined.

Commands

Reference 

Use docker compose instead of docker-compose.

List containers

docker compose ps

Build or rebuild containers

It only builds containers without starting it.

docker compose build

Create and start containers

Up = Build/rebuild + start

docekr compose up docker compose up --build # rebuild containers (build images before starting containers) docker compose -d up # running in backgound docker compose -f <compose-file> up docker compose up <service> <service> # start only a few service

Execute a command in a running container

By default, it will allocate a TTY by default, so you can straight away get an interactive prompt from this command docker compose exec apiservice bash.

docker compose exec <service-name> <commands> docker compose exec -it apiservice bash # old version docker compose exec apiservice bash # new version

Display service log output

docker compose logs

Stop services

Stop running containers without removing them. The services can be started again with docker compose start.

docker compose stop

Stop and remove containers, networks

docker compose down
Last updated on