Hopp til innhold

Docker for automation

Skip to content

This is a machine-translated text that may 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 investigate what is created automatically.

Before you begin: Docker Engine and Compose from [[Docker Level 1 - Get started|Docker Level 1]] must be working. Run docker compose version in the Linux VM. You do not need a Flask app, Dockerfile, or GitHub account. [[Terminal Git Level 3 - YAML|The YAML reference]] provides support for indentation and lists; you can read it without having created a GitHub workflow.

First complete “Task 1” and verify the web response. Updating is a lookup for later work. “Task 2” allows you to try multiple services in the same project.

What is Docker Compose?

With Docker Compose, you describe one or more services in a file. In this exercise, each service runs one container. Here, docker-compose.yml is used.

compose.yaml and docker-compose.yml are names supported by Compose. Use one of them in your project directory, and run commands from that directory. Here, docker-compose.yml is used.

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

One command sets everything up:

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 must also move necessary data and local files and set up passwords, network, and other settings. Tags such as latest may point to a new image later. If the builds are to be identical, you must also lock which image is 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 the next server docker compose up -d

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

Read the small example

services collects the services. web is the name we give to the web server. image selects a pre-built image, while ports maps port 8083 on the VM’s loopback address to port 80 inside the container. Therefore, you test from the VM itself. Building from source code comes up 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 the project’s containers and networks
docker compose logs -f Follows the logs in real time
docker compose pull Pulls images that the project’s tags point to
docker compose restart Restarts the project’s containers without changing the image
docker ps Shows running containers

When a service needs storage

Preserve data outside the container explains bind mounts and named volumes. The Nginx exercise below does not require a database or storage connection. Use the storage support before extending the project with a service that must preserve data.

Updating Services

Updating a Docker service is simple:

# Fetch latest version
docker compose pull

# Restart with new version
docker compose up -d

up -d applies the new image by recreating the container. Verify that the service works correctly after the update; a new image version may require changes to configurations or data.

Easy Task 1 - Set up Nginx with Docker Compose

Work in the VM’s console. Create a new folder so that this exercise does not modify another Compose project:

# Create a directory for composition (compose) results
mkdir compose-results
# Change into that directory
cd compose-results
# Open an editor to create or edit the Docker Compose configuration file
nano docker-compose.yml

If the folder already exists, open it and check its contents before proceeding. Write the Nginx example from “What is Docker Compose?” into the file. Save with Ctrl+O, confirm with Enter, and exit with Ctrl+X. Check and start:

The provided code block contains no comments; therefore, it is returned unchanged per instruction #2 (never modify executable code) and general integrity rules.

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

config --quiet should exit without an error message. If you fix a mistake in the file, run the check again before starting it. ps should show web as running, and curl should display the Nginx content with Welcome to nginx!.

If the port is occupied, choose another available host port in the file and use the same port in curl. If the container stops, read docker compose logs web. Do not start a new project in a random folder to try again.

Explain before continuing: Which file describes the service, and what did docker compose up -d create? Stop the project with docker compose stop, restart it with docker compose up -d, and repeat the curl test. If you are using this page as Compose support for another task, you can go back there now.

Medium Task 2 - Add a service

Extend the docker-compose.yml from task 1 with an additional service. For example:

Use a unique service name under services, at the same indentation level as web. Read the service’s official Docker example to find the required image, port, and storage. Use an available host port and restrict access to the practice environment. A latest tag is not a pinned version.

First run docker compose config --quiet. Then start it with docker compose up -d and verify that both services are running simultaneously. If you use a loopback binding like Nginx does, test the port from within the VM; access from another machine requires separate 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 configured. Explain verbally or in writing what Compose creates automatically, and what you still need to handle yourself, such as data, configuration, and backups. If one service stops, read docker compose logs before modifying the configuration. Terminate any log tailing with Ctrl+C.

Summary

  • Docker Compose allows you to define multiple services in one file
  • The Compose file is both configuration and documentation
  • Updating is done with docker compose pull + docker compose up -d
  • Docker makes it easy to move services between servers

Sources: Docker Compose: first project and Compose services.