docker-compose is an easy to use container orchestration manager that sits on top of Docker nicely. It ships with Docker.

docker-compose and docker compose

The command used to be docker-compose but recently changed to docker compose. I like to alias docker-compose="docker compose" in my shell so that I don’t have to care.

# Check if docker-compose command exists
if command -v docker-compose &> /dev/null
then
	DC="docker-compose"
else
	DC="docker compose"
fi

Using Host Network

For some use cases it may be useful to bind the container’s network stack to the host so that calls to ports on localhost go to the right place. To do this add the following to your docker-compose service:

 servicename:
    image: some_image:latest
    network_mode: "host"

Extensions and Fragments

Docker-compose yaml defines mechanisms for reusing parts of the configuration so that it is possible to minimise repetition.

The worked examples illustrate the point of this functionality:

x-function: &function
 labels:
   function: "true"
 depends_on:
   - gateway
 networks:
   - functions
 deploy:
   placement:
     constraints:
       - 'node.platform.os == linux'
services:
 # Node.js gives OS info about the node (Host)
 nodeinfo:
   <<: *function
   image: functions/nodeinfo:latest
   environment:
     no_proxy: "gateway"
     https_proxy: $https_proxy
 # Uses `cat` to echo back response, fastest function to execute.
 echoit:
   <<: *function
   image: functions/alpine:health
   environment:
     fprocess: "cat"
     no_proxy: "gateway"
     https_proxy: $https_proxy
  • We use the prefix x- to denote configurations that are template and docker-compose ignores them.
  • We use &varname to set up a reference to the yaml data structure that sits underneath it
  • We use <<: *varname to inject those variables into a later service.