home
Docker Compose
Tool for running multi-container Docker applications
print

docker-compose version: 1.27.0 - Date: June 2022

Info

Show the docker-compose version
docker-compose version

Display help and usage instructions for a command docker-compose help

Manage containers

Build, (re)create and start containers docker-compose up [options] [SERVICE]

  • Start all containers in background: docker-compose up -d

Stop containers and remove containers, networks, volumes, and images created. docker-compose down [options]

Restart all stopped and running services docker-compose restart [options] [SERVICE]

Create containers (without starting them) docker-compose create [options] [SERVICE]

  • Build images before creating containers: docker-compose create --build

Removes stopped service containers docker-compose rm [options] [SERVICE]

  • Stop the containers, if required, before removing: docker-compose rm --stop

Set the number of containers to run for an existing service docker-compose scale SERVICE=NUM

  • Scale the service web with 3 containers: docker-compose scale web=3

Start existing containers for a service docker-compose start SERVICE

Stop running containers (without removing them) docker-compose stop SERVICE

Pause running containers of a service docker-compose pause SERVICE

Unpause paused containers of a service docker-compose unpause SERVICE

Run a one-time command against a service docker-compose run [options] SERVICE [COMMAND]

  • Start the web service and runs bash as its command: docker-compose run web bash

Force running containers to stop by sending a SIGKILL signal docker-compose kill [options] [SERVICE]

  • Kill all containers with SIGINT signal: docker-compose kill -s SIGINT

Manage Images

Build images referenced in Compose file (without starting the containers) docker-compose build [options] [SERVICE]

Pull images referenced in Compose file docker-compose pull [options] [SERVICE]

Push images for services to their respective registry/repository docker-compose push [options] [SERVICE]

Tools

Validate and view the Compose file docker-compose config

List containers docker-compose ps [options] [SERVICE]

Display log output from services docker-compose logs [options] [SERVICE]

Display the running processes docker-compose top [SERVICE]

Stream container events for every container docker-compose events [options] [SERVICE]

List images used by the created containers docker-compose images

Run arbitrary commands in the containers of a service docker-compose exec [options] SERVICE COMMAND

Print the public port for a port binding docker-compose port [options] SERVICE PRIVATE_PORT

Compose File Example

Deploying Wordpress with docker-compose :

docker-compose.yml

version: '3'

services:
  
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       
volumes:
    db_data:

Using :

  • version 3 of compose file
  • 2 services (db and wordpress)
  • restart policies for containers
  • ports to access web interface
  • environment variables to initialize the database and allow wordpress to connect
  • volume to store data
  • name of service as DNS entry for WORDPRESS_DB_HOST