Wednesday, 6 March 2019

Introduction to docker-compose

docker-compose


Helps managing Docker containers. Everything is defined through a docker-compose.yml file.

$ docker-compose
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information


Its configuration file is docker-compose.yaml (or .yml). This file is also known as Compose file. It contains:
  • services - list of service names; Each service can have:
    • build
    • command
    • environment
    • image
    • ports
    • restart
    • volumes
docker-compose up will read Compose filecreate and start containers (services) start listed there.

To check the state of all services, run docker-compose ps from the working directory (which is by default the one that contains Compose file):

$ docker-compose ps

         Name                   Command           State           Ports         
--------------------------------------------------------------------------------
my-project_service1_1    /bin/sh -c yarn          Up      0.0.0.0:3001->3001/tcp
                         install && ...                                         
my-project__postgres_1   postgres.sh              Up      0.0.0.0:5432->5432/tcp                                             
my-project_service3_1    /usr/local/bin/example   Up      0.0.0.0:4567->4567/tcp                              

To list the IDs of all running containers:

$ docker-compose ps -q
1e8a22167a4ac8354a456a29550cc7fd5fa5eb80ce68f6db04db8ba9764733cf
a1404d3f917c5daf8921731175ae0aa0940eccb518e9c0fd786183036d000d20
e6aeaf2e79b10a29bc834b0e3f712add1c02216b50afa1193663248162721b86

To list all images:

$ docker-compose images
       Container       Repository   Tag       Image Id      Size 
------------------------------------------------------------------
my-project_service1_1   service1   latest   58f28f305f80   875 MB
my-project__postgres_1  postgres   latest   f97c959a7d9c   298 MB
my-project_service3_1   service3   latest   4e02459e1040   159 MB

Another way to find the association between running containers and their IDs:

$ docker ps
CONTAINER ID    IMAGE   COMMAND                     CREATED       STATUS     PORTS                    NAMES
a1403d3f917c  postgres  "postgres.s…"               29 hours ago  Up 3 hours 0.0.0.0:5432->5432/tcp   postgres
e6aecf2e79b1  service3  "/usr/local/bin/example…"   29 hours ago  Up 3 hours 0.0.0.0:4567->4567/tcp   browser-engagement_s3_1
1e8a12167a4a  service1  "/bin/sh -c 'yarn in…"      29 hours ago  Up 3 hours 0.0.0.0:3001->3001/tcp   my-project_service1_1


No comments: