Checking how many agents are working

How this works
Kontakt
← Blog
Four matching steel shipping crates stacked in a dark workshop, a thin amber seam of light along one lid, and the AMARBARO mark painted on the side of the nearest crate.
✎Post blogowy

Containers explained: why each site lives in its own box

The short version

A container makes an app portable and contained, but it does not make the app correct, and a running container can still serve the wrong thing.

What this is about

A container is a sealed box that holds one program and everything that program needs to run. You can start it on your laptop or on a server, and it behaves the same way. Our sites use containers to keep the database, the app and the web server apart. This article explains the idea, shows the box layout of our base stack, and tells a small story about a setting we forgot.

Why it matters

Before containers, moving a site from one machine to another was a long list of installs and tweaks, and the new machine never quite matched the old one. A container replaces that list with one file. It also limits the damage when something goes wrong, because one box cannot easily touch another. If you plan to host anything yourself, containers will be one of the first tools you meet.

How it works

The box and the recipe

Two words are worth keeping apart. An image is the recipe plus the ingredients, frozen into a file. A container is a running copy made from that image. You can start many containers from one image. Stop a container and it is gone, unless you told it to keep some data somewhere outside.

That last part is the key. A container is meant to be disposable. The things you must keep, such as the database files, live in volumes, which are folders kept outside the container's own short life.

Our box layout

The production file for our base stack, called compose.prod.yaml, lists four services.

  1. db: the PostgreSQL database. It has a health check that asks every 5 seconds whether the database accepts connections, and it keeps its files in a volume.
  2. app: the program that builds our pages. It starts only after the database says it is healthy.
  3. hq: the operator panel, which is a separate program with its own source folder.
  4. caddy: the web server that receives visitors and hands them to the app. It keeps its certificates in a volume so they survive a restart.

The compose file names four volumes in all: one for the database files, one for uploaded photos, and two for the web server. When we rebuild the app image, the photos stay. That is the whole point of keeping them outside the box.

A tool called Docker Compose reads the file and starts all four in the right order. One command brings the stack up, and one command takes it down.

Why a separate box for each

If the database lives in its own box, we can upgrade the app without touching the data. If the web server lives in its own box, we can restart it without dropping the database. A crash in one box does not take down the others, and each box can be told to restart by itself.

A story: the setting that was not passed in

The file for the app has a comment worth reading. It explains that a variable called NICHE tells the app which site it should serve. The comment also says that an environment file only makes a variable available for the file itself, and that a container sees nothing unless it is named in that block. When the line was missing, the app served a plain placeholder site. The container was healthy and the home page returned a 200. It was, in the comment's words, the worst possible way to be wrong.

The lesson is simple. "The container is running" and "the right site is showing" are different facts. We now keep that comment next to the line, and our checks look at what the page actually contains.

Containers also limit what code can do

Containers can be locked down. In the design notes for dynoshell, our agent terminal project, there is a sandbox for running code that we do not fully trust. The settings are plain: the program runs as a normal user, not as root, all special permissions are dropped, the main file system is read only, a small temporary folder of 64 MB cannot run programs, and the number of processes is capped at 128. The notes also say that there is no source scan. The box, not a list of bad words, is the boundary.

What containers do not do

A container is not a virtual machine, and it shares the machine's kernel. It is not a guarantee of safety either. A container with too many permissions is as risky as the program outside. And it does not fix a bad recipe: if the app inside has a bug, the bug travels with the image.

Words you may not know

  • Container: a running, sealed copy of an app with what it needs.
  • Image: the frozen file a container is made from.
  • Volume: a folder kept outside the container so data survives.
  • Docker: the most common tool for building and running containers.
  • Compose: a tool that starts several containers from one file.
  • Health check: a small test the system runs to see if a container is working.
  • Root: the all powerful user on a Linux machine.

Try it

  1. Read our terminal project at dynoshell.com, the place where we run our agents.
  2. Install Docker, then run docker run --rm hello-world. That is the smallest container you can start.
  3. Write a two service Compose file with a database and a tiny app, and use a volume for the data. Stop it, start it again and see that the data stayed.
  4. See the list of the sites we run at amarbaro.com/web. Each one is a set of boxes like the ones above.

Komentarze

Brak komentarzy.

Zaloguj się aby skomentować.