Docker as a means of 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 be Docker Compose?

With Docker Compose, thou dost define manifold services within a single file (docker-compose.yml). Each service is a container, bearing 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.

Usefull Docker Commands

Command That which it doth perform
docker compose up -d Starteth all services in the background
docker compose down Stoppeth and removeth all containers
docker compose logs -f Followeth the logs in real time
docker compose pull Fetcheth the latest version of all images
docker compose restart Restarteth all services
docker ps Showeth the 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
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

Herein are stored the database files within a volume named db-data. Though thou dost delete and recreate the container anew, the data shall yet 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.