Skip to content

Compose

The docker-compose tool is a very simple, yet powerful tool and has been conceived and concretized to facilitate the running of a group of Docker containers. In other words, docker-compose is an orchestration framework that lets you define and control a multi-container service. It enables you to create a fast and isolated development environment as well as the ability to orchestrate multiple Docker containers in production. The docker-compose tool internally leverages the Docker engine for * pulling images * building the images * starting the containers in a correct sequence, and making the right connectivity/linking among the containers/services based on the definition given in the docker-compose.yml file.

Compose is used for defining and running Docker applications for multiple containers. With Compose you use a Compose file to configure the services of your application. Then, with a single command, you create and launch all the services from your configuration. For more information about all Compose features, see the list of features.

Using Compose is essentially a three-step process:

  • Define the environment of your app with a docker file so you can play it anywhere.
  • Define the services that make up your app in docker-compose.yml so they can run together in an isolated environment.
  • Finally run docker-compose up and Compose will start and run your entire app.

The docker-compose tool orchestrates containers using the docker-compose.yml file, in which you can define the services that need to be crafted, the relationships between these services, and their runtime properties.

The default docker-compose file is docker-compose.yml, which can be changed using the -f option of the docker-compose tool. The following is the format of the docker-compose.yml file:

version: "3.7"
services:
   <service>:   
      <key>: <value>   
      <key>:       
         - <value>       
         - <value> 
   <service>:
      ...
volumes:
   ...
networks:
   ...
Services refer to containers' configuration. You can have more than one service definition in a single docker-compose.yml file. The service name should be followed by one or more key-values.

The following example shows a docker-compose.yml file:

version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

keys supported in the docker-compose

Key Description
image This is the tag or image ID
build This is the path to a directory containing a Dockerfile
command This key overrides the default command
links This key links to containers in another service
external_links This key links to containers started either by some other docker-compose.yml
ports This key exposes ports and specifies both the ports HOST_port:CONTAINER_port
expose This key exposes ports without publishing them to the host machine
volumes This key mounts paths as volumes
volumes_from This key mounts all of the volumes from another container
environment This adds environment variables and uses either an array or a dictionary
Key Description
env_file This adds environment variables to a file
extends This extends another service defined in the same or different configuration file
net This is the networking mode, which has the same values as the Docker client --net option
pid This enables the PID space sharing between the host and the containers
dns This sets custom DNS servers
cap_add This adds a capability to the container
cap_drop This drops a capability of the container
dns_search This sets custom DNS search servers
working_dir This changes the working directory inside the container
entrypoint This overrides the default entrypoint
Key Description
user This sets the default user
hostname This sets a container's host name
domainname This sets the domain name
mem_limit This limits the memory
privileged This gives extended privileges
restart This sets the restart policy of the container
stdin_open This enables the standard input facility
tty This enables text based control such as a terminal
cpu_shares This sets the CPU shares (relative weight)

docker-compose command

All the docker-compose commands use the docker-compose.yml file as the base to orchestrate one or more services.
the syntax of the docker-compose command:

docker-compose [<options>] <command> [<args>...]

  • --verbose: This shows more output
  • --version: This prints the version and exits
  • -f, --file <file>: This specifies an alternate file for docker-compose
    • default is the docker-compose.yml file
  • -p, --project-name <name>: This specifies an alternate project name
    • default is the directory name
Command Description
build builds or rebuilds services
kill kills containers
logs displays the output from the continers
port prints the public port for s port binding
ps list the containers
pull pulls the service images
rm removes the stopped containers
run runs a one-off command
scale sets a number of containers for a service
start starts services
stop stops services
up this creates and starts containers
down stop and remove containers, networks, images and volumes

Exercise

consider the follwoing compose file:

services:
   db:
      image: mysql:5.7
      environment:
         MYSQL_ROOT_PASSWORD: somewordpress
         MYSQL_DATABASE: wordpress
         MYSQL_USER: wordpress
         MYSQL_PASSWORD: wordpress
   wordpress:
      image: wordpress
      links:
         - db:mysql
      ports:
         - 8080:80
      environment:
         WORDPRESS_DB_USER: wordpress
         WORDPRESS_DB_PASSWORD: wordpress
         WORDPRESS_DB_NAME: wordpress  

deploy the compose file:

$ docker-compose up -d
$ docker-compose down