Docker for DevOps Engineers

Docker for DevOps Engineers

Day 18 :-

Docker Compose

  • Docker Compose is a tool that was developed to help define and share multi-container applications.

  • With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Docker Compose is another best tool for docker to setup multi-container environments. Using this create a single compose file with defining all the containers with there environments. You can easily use single command to build images and run all the containers.

There is the three-step process to work with Docker Compose.

1. Define application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining with all services under application.
3. Run docker-compose up to run all services under applications.

You must have Docker Engine installed on your system. If you don’t have already installed, Visit our Docker installation section of this tutorial.

Docker Compose Example File

A docker-compose.yml file required to create to start working with the docker compose. Below is a sample configuration file of version 3. This file has only one service added in it named web.

Docker Compose CLI Reference

The docker-compose command provides a number of subcommands to manage Docker containers with docker-compose. Please find below details of the subcommands. Before reading below commands remember that you passed service name as an argument (not container name)

Install Docker Compose

Visit the Docker compose official Github page and download the latest version of Docker compose tool. You can also install Docker compose 1.16.1 using the following command. Before installing the specific version, You must check the compatibility on releases page with your docker version.

What is YAML?

  • YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • YAML is a popular programming language because it is human-readable and easy to understand.

  • YAML files use a .yml or .yaml extension.

What is YAML used for?

One of the most common uses for YAML is to create configuration files. It's recommended that configuration files be written in YAML rather than JSON, even though they can be used interchangeably in most cases, because YAML has better readability and is more user-friendly.

In addition to its use in Ansible, YAML is used for Kubernetes resources and deployments.

A benefit of using YAML is that YAML files can be added to source control, such as Github, so that changes can be tracked and audited.

build –

The build option is used to build images for services for which build is defined.

$ docker-compose build             ## Build all services
$ docker-compose build web         ## Build single service

up –

Use to create docker containers with available services in docker-compose.yml file in current directory. Use -d switch to launch containers in daemon mode.

$ docker-compose up -d            ## Create all containers
$ docker-compose up -d web        ## Create single container

down –

This will stop and delete all containers, network and associated images for the services defined in a config file

$ docker-compose down           ## Restart all containers
$ docker-compose down web       ## Restart single container

ps –

This will list all containers created for the services defined in a config file with there status, port bindings and command.

$ docker-compose ps

exec –

This will execute a command to the running container. For example list files in container associated with web service.

$ docker-compose exec web ls -l

start –

This will start stopped containers of the services defined in config file

$ docker-compose start            ## Start all containers
$ docker-compose start web        ## Start single container

stop –

This will stop running containers for the services defined in config file

$ docker-compose stop             ## Stop all containers
$ docker-compose stop web         ## Stop single container

restart –

This will restart containers of the services defined in config file

$ docker-compose restart           ## Restart all containers
$ docker-compose restart web       ## Restart single container

pause –

This will pause running containers for the services defined in config file.

$ docker-compose pause            ## Pause all containers
$ docker-compose pause web        ## Pause single container

unpause –

This will start paused containers for the services defined in config file.

$ docker-compose pause            ## Start all paused containers
$ docker-compose pause web        ## Start single paused container

rm –

This will remove stopped containers for the services defined in config file.

$ docker-compose rm               ## Start all paused containers
$ docker-compose pause web        ## Start single paused container