Docker - Compose



Table of Contents

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

KeyDescription
imageThis is the tag or image ID
buildThis is the path to a directory containing a Dockerfile
commandThis key overrides the default command
linksThis key links to containers in another service
external_linksThis key links to containers started either by some other docker-compose.yml
portsThis key exposes ports and specifies both the ports HOST_port:CONTAINER_port
exposeThis key exposes ports without publishing them to the host machine
volumesThis key mounts paths as volumes
volumes_fromThis key mounts all of the volumes from another container
environmentThis adds environment variables and uses either an array or a dictionary
KeyDescription
env_fileThis adds environment variables to a file
extendsThis extends another service defined in the same or different configuration file
netThis is the networking mode, which has the same values as the Docker client --net option
pidThis enables the PID space sharing between the host and the containers
dnsThis sets custom DNS servers
cap_addThis adds a capability to the container
cap_dropThis drops a capability of the container
dns_searchThis sets custom DNS search servers
working_dirThis changes the working directory inside the container
entrypointThis overrides the default entrypoint
KeyDescription
userThis sets the default user
hostnameThis sets a container's host name
domainnameThis sets the domain name
mem_limitThis limits the memory
privilegedThis gives extended privileges
restartThis sets the restart policy of the container
stdin_openThis enables the standard input facility
ttyThis enables text based control such as a terminal
cpu_sharesThis 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
CommandDescription
buildbuilds or rebuilds services
killkills containers
logsdisplays the output from the continers
portprints the public port for s port binding
pslist the containers
pullpulls the service images
rmremoves the stopped containers
runruns a one-off command
scalesets a number of containers for a service
startstarts services
stopstops services
upthis creates and starts containers
downstop 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