Docker, as a most wondrous automation,

Skip to content

This doth be a machine-wrought text which may contain errors!

Docker doth simplify the deployment of applications. Rather than installing and configuring software manually upon a server, thou dost define all within configuration files. The result is reproducible, portable, and swift to set up.

What doth be Docker Compose?

With Docker Compose, dost thou define sundry services within a single file (docker-compose.yml). Each service is a container with its own configuration.

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"

One command doth set up all:

docker compose up -d
# Starter tjenestene i bakgrunnen.
# Doth start the services in the background.

Doth thou require to move the service to another server? Copy the file and execute the selfsame command. All is identical.

Wherefore Doth This Constitute Automation?

Ponder the difference:

Manual Setup With Docker Compose
Install Nginx manually image: nginx:latest
Configure ports ports: "80:80"
Install Grafana manually image: grafana/grafana:latest
Document all the steps All is documented in the .yml file
Repeat all upon the next server docker compose up -d

The Docker Compose file is the documentation. It doth describe precisely which services do run, which ports they employ, and how they are configured.

Useful Docker Commands

Command What it doth
docker compose up -d Doth start all services in the background
docker compose down Doth stop and remove all containers
docker compose logs -f Doth follow the logs in real time
docker compose pull Doth fetch the latest version of all images
docker compose restart Doth restart all services
docker ps Doth show running containers

Volumes: To Save Data Without the Container

Containers be fleeting, good sir. Shouldst thou erase a container, all within doth vanish. To preserve data, we employ volumes:

services:
  database:
    image: postgres:16
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: hemmelig # Hark! A password most secret, 'tis!
    volumes:
      - db-data:/var/lib/postgresql/data # Prithee, store the database's lore within this volume.

volumes:
  db-data: # Lo, a volume to hold the database's treasures.

Here are the database files stored within a volume hight db-data. E’en shouldst thou delete and recreate the container, the data shall remain.

Update of Services

To update a Docker service doth prove most simple:

# Fetch the latest version
docker compose pull

# Restart with the new version
docker compose up -d

Compare this with the updating of software installed by hand, where perchance one must download, configure, and hope that naught doth break.

Easy Task the First - To Set Up Nginx with Docker Compose

Create ye a docker-compose.yml which doth run an Nginx webserver:

  1. Forge a new folder and create therein the file docker-compose.yml
  2. Define a service with image: nginx:latest upon port 80
  3. Execute docker compose up -d
  4. Visit http://localhost within thy browser

Medium Task the Second - Add a Service

Extend the docker-compose.yml from Task the First with an additional service. For example:

Start all with docker compose up -d and behold that both services do run concurrently.

Summary

  • Docker Compose doth permit thee to define sundry services within a single scroll.
  • The Compose file is both configuration and record for posterity.
  • Volumes do preserve data without the container, so ‘twill survive restarts.
  • Updating is docker compose pull + docker compose up -d.
  • Docker doth make it facile to move services ‘twixt servers.