This is an old revision of the document!


Lab 04 - Docker Compose

Objectives

  • Understand what a software container is
  • Get familiar with the Docker environment
  • Learn how to build, publish, and deploy containers

Contents

Introduction

Normally, in order to run containers, we need to execute the corresponding run command (docker run) and set all the necessary parameters. This process can be difficult and repetitive when we need to start multiple containers. One way to “save” the running configuration is to create scripts. The problem with running multiple scripts is the loss of uniformity in configuration (which container connects to which network, who communicates with whom, etc.).

Docker Compose is a utility created by Docker that is used to centralise the configuration process of a container-based application in a declarative manner, using Yet Another Markup Language (YAML) configuration files.

Moreover, the format for Compose files is also used in Docker Swarm, the orchestrator created by Docker for managing Docker services, which we will discuss in lab 5.

You will notice that in this lab we use the terms service and container interchangeably. This is because Docker Swarm works with services, while Docker Compose works with containers. We refer to both terms in the same context because the configuration is 90% identical, regardless of whether Swarm or Compose is used.

Installation

For Windows and MacOS, Docker Compose is part of the Docker Desktop installation. For Linux, the installation is done according to the official guide.

Key items

YAML file format

YAML files are usually used to write declarative configurations. The format is very easy to understand and employ, as follows:

  • “key:value” elements are used
  • indented paragraphs are children properties of the previous paragraphs
  • lists are delimited by ”-”.

Docker Compose file example

# docker-compose.yml
version: "3.8"

services:
    api:
        build: . # builds the image from a Dockerfile
        image: register-image-name:version # uses an image from a registry
        environment:
            ENVIRONMENT_VARIABLE: value
        ports:
            - "5000:80"
        networks:
            - lab4-network

    postgres:
        image: postgres:12
        volumes:
            - lab4-volume:/var/lib/postgresql/ data
            - ./scripts/init-db.sql:/docker-entrypoint-init.d/init-db.sql
        networks:
            - lab4-network

volumes:
    lab4-volume:

networks:
    lab4-network:

Version

The “version” attribute describes what functionalities will be loaded when running the Docker Compose utility.

You must specify the version in any Docker Compose file.

Services

The “services” attribute describes the services/containers that will run after the configuration is started by Compose. Each service represents a container that will have the name and configuration of the service. In the example above, the containers will be named api and postgres. The most important properties of services are the following:

  • build - specifies the path where the Dockerfile the container will be built from is located
  • image - specifies the name of the image used to run the container
  • ports - a list of entries with the format “host_port:service_port”, which specifies which ports are exposed and/or mapped
  • volumes - a list of entries with the format “host_volume:service_path” where the volume mappings are specified; the same rules that apply to CLI commands are maintained here as well; “host_volume” can be a standard volume or a bind mount
  • networks - the list of networks which the service/container belongs to
  • environment - object with entries of type “service_variable_name: value” which injects the environment variables specified when running the service/container.

The build and image attributes are mutually exclusive.

ii/labs/s2/04.1652883377.txt.gz · Last modified: 2022/05/18 17:16 by radu.ciobanu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0