Docker Compose

A tool for configuring multiple container applications.

One process - one container

  • easier configuration,
  • better scaling,
  • greater security.

Docker Compose

Configuring multiple Docker objects at once:

  • services,
  • containers,
  • networks,
  • named volumes,
  • configurations.

When do I use Compose

  • during development,
  • when the application needs only one computing node,
  • during automatic tests of CI CD containers.

Disadvantages of Compose

  • Harder scaling - usually runs on only one node.
  • File system mapping is limited.
  • Automatic allocation of funds is not possible.

Docker swarm mode

  • One or more computing nodes
  • They are connected by a network
  • Manager Node
  • Worker Node

Docker swarm mode

         The Internet

    +----------------+ +--------+ +---------+
    | Leader Manager | | Manager | | Manager |
    +----------------+ +--------+ +---------+

    +--------+ +--------+ +--------+
    | Worker | | Worker | | Worker |
    +--------+ +--------+ +--------+

    +-----+ +-----+
    | NAS | | SAN |
    +-----+ +-----+

Docker swarm mode

  • Resistance of the service to the outage of the computing node.
  • Scaling - adding nodes as needed.
  • Upgrade or update computing nodes without service interruption.

Manager Node

  • Maintains cluster state
  • Allocates resources
  • Maintains service information and allocates DNS names.
  • RAFT algorithm for selecting the "leading" node.

Worker Node

  • Starts and stops containers

Swarm mode disadvantages

  • Docker API is limited.
  • Virtual networks are not very flexible - publishing is not possible using the Docker API.
  • More difficult management of several services at once (multi-tenant).
  • More difficult monitoring of running services - an external solution is needed.
  • Access rights and namespaces within the cluster are not supported.
  • More difficult publication of the service.

Example: Deploying a web application using Swarm Mode Docker

A typical web application consists of several components. Each component has different requirements.

For example

  1. SQL Database, available only from the internal network for application needs
  2. Backend: Implements the REST interface and application logic.
  3. Frontend: JS Single Page application, communicates with the BACKEND through a REST interface.
+----------+
| Frontend |
+----------+
       |
+----------+ +----------+
| Backend |---| Storage |
+----------+ +----------+
       |
+----------+
| Database |
+----------+

Microservices

The application is divided into components that communicate with each other

Better scaling

Easier development

Web application architecture

Separate frontend and backend

Frontend

Interaction with the user

  • reverse proxy server (ingress)
  • Web Client
  • Mobile app

Serialization and communication protocols:

  • REST
  • Websockets

Backend

Application logic and state

  • relational database
  • cache server, workqueue (redis, memcached)
  • object storage

compose.yaml

  • different notation of docker arguments
  • one configuration for the entire web application
  • uses the YAML format

Example compose.yaml

Compose & Wordpress:

  • Php

  • Database

  • https://docs.docker.com/samples/wordpress/

  • https://hub.docker.com/_/wordpress

Docker Service

  • One DNS name
  • one or more containers

Docker Named Volume

Folder with container state

Docker Network

  • A group of containers that communicate with each other
  • Containers cannot communicate with each other using local ports.
  • Compose automatically creates a default virtual network for all services.

Application configuration

The same application will run in different environments

  • local development
  • deployment outside the container
  • deployment using container and Docker Compose
  • deployment in Kubernetes
  • deployment in another cloud service (Azure App)

It is not good to have configuration data in source codes

  • names and access passwords to the database in source codes are a security risk.
  • to change the configuration, it is necessary to reassemble the application
  • it is confusing - it is not clear at first glance how to configure the application.

Separation of configuration variables from application code

Application configuration:

  • environment variables
  • configuration files
  • command line arguments

How to implement the configuration in your application:

  • global variables, place them in one file.
  • define environment variables, load them into global variables.
  • load command line arguments.
  • use a specialized library to configure the application.

We separate:

  • configuration (where our configuration files are located that do not change)
  • status (where are the files that change and are important)
  • application (everything else)

Real configuration is complex

Wordpress with HTTPS:

  • Certbot
  • Web Server (on HTTPS certificates)

https://zactyh.medium.com/hosting-wordpress-in-docker-with-ssl-2020-fa9391881f3

Complex configuration

  • Publishing: Services using a local port
  • Scaling: one service can consist of several processes
  • Limitation of resources: we can limit the RAM or CPU of the container
Reload?