Docker sq
Docker and Kubernetes Container Virtualization - Other Docker Tools
  •  

Docker Compose

PRO
Outline

Another way to manage Docker services is through the Docker Compose tool. Docker Compose uses YAML-formatted configuration files named docker-compose.yaml to identify base images, environment variables, network ports, and data volumes for multiple services in a way that lets them work together right out of the box.

Anyway, back to Docker Compose itself. Again, I'm going to show you how it works by showing you how it works. Here's a docker-compose.yaml file that'll fire up two containers: one running a MariaDB database and the second running the WordPress service that'll make use of the database. This example comes from the Docker online documentation and uses an up-to-date version of MariaDB as of this recording. Feel free to check the source page for updates whenever you get to doing this yourself.

# From https://docs.docker.com/samples/wordpress/
services:
  db:
    image: mariadb:10.6.4-focal
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  wordpress:
    image: wordpress:latest
    ports:
      - 80:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
volumes:
  db_data:

First of all, the fact that this file is YAML-formatted means that the way you indent each line is really important. Lines of a particular level must be perfectly lined up with each other. And any mistakes - even a mistake of just one space - will cause the whole thing to fail. Fortunately Docker Compose will let you know something's wrong before heading off and executing only part of the environment.

The first service is called db - which obviously is so-called to remind us that this is a database. It'll pull the Docker Hub image containing specified version of MariaDB and mount a new volume at /var/lib/mysql. These names and passwords are the actual names and passwords the services will use to connect with each other. They're insanely insecure for production deployments, so make sure you change them before running this on a public network. The service will use the standard MySQL ports, 3306 and 33060.

The second service is WordPress. It'll pull the latest WordPress image from Docker Hub and listen for both internal and external traffic on port 80. Again, we're using default passwords for authentication. Make sure you update those for real deployments. Ideally, you should pass secrets to your deployments by way of environment variables. That's much better than hard coding them into your YAML files.

Finally, we need to create the database volume we referenced earlier within the db service.

We spent our time here focusing on Docker Engine, which is primarily a command line tool. I believe that's the best way to ease our way into the Docker world, since it lets us see things working from a front seat perspective. However, the Docker company itself has lately been vigorously pushing their Docker Desktop package. Here it is, for instance, plastered over the very top of the docker.com homepage.

What is Docker Desktop? It's a GUI tool that runs on macOS, Windows, and Linux desktops through which you can manage local or remote Docker resources. In the short term, there's definitely less of a learning curve for getting simple things done like inspecting a container, listing your available images, or managing volumes.

But there are also some really useful tools as part of the Settings menus - like these advanced controls that let you configure host system resources or play around with your Docker Engine defaults. There's even a Kubernetes integration control.

So if that sounds like something that'll make life easier for you, check out the installation documentation for your OS.