Hopp til innhold

Docker for automatin', partner

Skip to content

This here’s a machine-translated text that might contain errors!

Docker as Automation

With Compose you describe your desired setup in a file and let Docker create the containers. Here you start with an Nginx web server from a ready-made image. Then you can add a service and inspect what gets created automatically.

Before you begin: Docker Engine and Compose from [[Docker Level 1 - Get started|Docker Level 1]] must be workin’. Run docker compose version in the Linux VM. You don’t need no Flask app, Dockerfile or GitHub account. [[Git in Terminal Level 3 - YAML|The YAML reference]] gives support for indentation and lists; you can read it without havin’ made a GitHub workflow.

Do “Task 1” first and check the web response. Upgrading is a lookup for later work. “Task 2” lets you try multiple services in the same project.

What is Docker Compose?

With Docker Compose, ya describe one or more services in a file. In this here drill, each service runs just one container. We’re usin’ docker-compose.yml round here.

compose.yaml an’ docker-compose.yml are names that Compose supports. Pick one of ‘em for yer project folder, an’ run the commands from that spot. We go with docker-compose.yml on this end.

services:
  web:
    image: nginx:latest
    ports:
      - "127.0.0.1:8083:80"

One command sets up ever’thin’:

docker compose up -d
# Starter alle tjenester i bakgrunnen.
# Starts all services in the background.

The compose file can be used on another server, but you gotta move over any needed data and local files too, plus set up passwords, network stuff, an’ other settings. Tags like latest might point at a different image down the road. If y’all want ‘em to build out just alike, then ya better lock in which one gets used.

Why is this automation?

Think about 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 steps Describe desired setup in the Compose file
Repeat everything on next server docker compose up -d

The Compose file is part of the technical documentation and describes the desired setup. Check actual state with docker compose ps and logs. Rationale, test results, and backup routines must be documented additionally.

Read the little example

services gathers the services together. web is the name we give to the web server. image picks a ready-made image, while ports hooks up port 8083 on the VM’s loopback address to port 80 inside the container. That’s why you test from the VM itself. Buildin’ it from source code comes later in Docker Level 2.

Useful Docker Commands

Command What it does
docker compose up -d Starts all services in the background
docker compose down Stops and removes project containers and networks
docker compose logs -f Follows logs in real-time
docker compose pull Fetches images that the project’s tags point to
docker compose restart Restarts project containers without changing the image
docker ps Shows running containers

When a Service Needs Storage

[[Container usage level 2 - Preserve Data Outside Container | Preserve Data Outside Container]] explains bind mounts and named volumes. The Nginx exercise below requires no database or storage connection. Use the storage support before extending your project with services that require preserving data.

Updatin’ Services

Updatin’ a Docker service is plumb simple:

# Fetch the latest version
docker compose pull

# Restart with the new version
docker compose up -d

up -d gets that new image rollin’ by startin’ up a fresh container. Make sure the service’s runnin’ smooth after the update; sometimes a new image version means you gotta tweak your settings or data.

Easy Task 1 - Set Up Nginx with Docker Compose

Workin’ on that VM console here now. Go ahead an’ make yerself a new folder so this here drill don’t go messin’ up some other Compose project:

mkdir compose-overthere
cd compose-overthere
nano docker-compose.yml

If that directory’s already there, open it up and check its contents before movin’ on. Write down the Nginx example from “What is Docker Compose?” into the file. Save with Ctrl+O, confirm with Enter and quit out with Ctrl+X. Check ‘em over and get started:

docker compose config --quiet
docker compose up -d
docker compose ps
curl http://127.0.0.1:8083

config --quiet oughta quit without throwin’ no errors. If you fix a mistake in the file, run that check again before bootin’ it up. ps should show web as bein’ alive and well, an’ curl oughta spit out them Nginx bits with Welcome to nginx!.

If the port’s already taken, pick yourself another open port on the host side of things in the file, an’ use that same one when you fire off your curl. Should the container shut down, give docker compose logs web a read-through. Don’t go spinnin’ up some brand-new project in whatever folder just ‘cause ya wanna try again.

Spit it out before movin’ on: Which file lays out how this service works, an’ what did docker compose up -d actually build for us? Kill the whole rig with docker compose stop, get her back up with docker compose up -d, an’ run that curl test once more. If y’all been usin’ this page as backup support fer somethin’ else, feel free to head back over there now.

Medium Task 2 - Add a service

Expand the docker-compose.yml from task 1 with an extra service. For example:

Use your own service name under services, at the same indentation level as web. Read the service’s official Docker-example to get the necessary image, port, and storage. Use a free host port and restrict access to the practice environment. A latest tag is not a locked version.

First run docker compose config --quiet. Then start it up with docker compose up -d and see that both services are running side-by-side. If you use reverse proxy binding like we did for Nginx, test the port from the VM; access from another machine requires its own clarification of address, port, and network.

Show docker compose ps and an expected response from each service on the actual address and port you have set up. Explain verbally or in writing what Compose creates automatically, and what you still need to handle yourself, such as data, settings, and backups. If one service stops, read docker compose logs before changing the configuration. End any log tailing with Ctrl+C.

Summary

  • Docker Compose lets ya define several services in one file
  • The Compose file is both configuration an’ documentation
  • Updatin’s done with docker compose pull + docker compose up -d
  • Docker makes it easy to move services between servers

Sources: Docker Compose: first project an’ Compose-services.