SECCTRL-298: how-to for building and publishing container images

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
babic_aandClaude Fable 5.2 committed 2026-11-25 14:03:34 +01:00
1 parent 188d1afd06
commit 4a1973cc19
2 files changed
+355

No files matched your search

@@ -8,3 +8,232 @@ tags:
---
# How-To: Use Containers
To run your service in a container on Controls infrastructure, you package it as an image, publish the image to a registry, and a **container host** pulls it and runs it with Podman under systemd. This page is mostly about the first two steps; running the image is the short last section.
![From the source repository to a container host: podman build turns the Containerfile and code into an image, podman tag gives it a version, podman push or Gitea Actions on a version tag publishes it to gitea.psi.ch, and the container host pulls it](images/09_containers_path.svg)
## What is in place and what is planned
| | Today | Planned |
|---|---|---|
| Build and registry | Podman on your machine or Gitea Actions; the Gitea registry `gitea.psi.ch` | |
| Container hosts | 12 hosts for web applications; containers run as root systemd units with `podman run` | Rootless Podman, Quadlet units, deployment through the catalogue |
| Soft IOCs | On soft IOC hosts, not in containers | Rootless Podman for soft IOCs that suit it |
## 1. Package your application as an image
### What goes into the image
An image holds your code and everything it needs to run: the Python version, the packages from `requirements.txt`, system libraries. It does **not** hold passwords, tokens, host-specific settings or data. Those come from the host when the container starts (environment variables, mounted files and directories), so the same image runs unchanged on a test host and on the production host.
One image runs one process. If your application needs a database or a cache, that is a separate container from its own image.
A small project looks like this:
myapp/
Containerfile
.containerignore
requirements.txt
app.py
Podman reads `Containerfile` or `Dockerfile`, and `.containerignore` or `.dockerignore`; use one of each.
### The Containerfile
A Containerfile for a Python service:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
ENV PYTHONUNBUFFERED=1
RUN useradd --create-home --uid 10001 appuser
USER appuser
EXPOSE 8000
CMD ["python", "app.py"]
Line by line:
- `FROM python:3.11-slim` is the base image: Debian slim with Python 3.11. Name the Python version in the tag, never `python:latest`, so a rebuild next year gives the same Python. The `-slim` variant keeps the image small.
- `WORKDIR /app` is the directory the following lines and the program work in.
- `COPY requirements.txt .` and `RUN pip install --no-cache-dir -r requirements.txt` install the dependencies before the code is copied. Podman reuses this layer while `requirements.txt` is unchanged, so a code change rebuilds in seconds. `--no-cache-dir` keeps pip's download cache out of the image. Pin versions in `requirements.txt`.
- `COPY app.py .` copies the code. Copy only what the program needs at run time.
- `ENV PYTHONUNBUFFERED=1` makes Python write its output immediately. Without it, print output waits in a buffer and reaches the journal late or not at all when the process dies.
- `RUN useradd --create-home --uid 10001 appuser` and `USER appuser` run the program as an ordinary user with a fixed uid. A fixed uid lets you give a mounted data directory the right owner.
- `EXPOSE 8000` documents the port the program listens on; it opens nothing by itself.
- `CMD ["python", "app.py"]` starts the program. Use this list (exec) form: Python is then the main process of the container and receives the stop signal directly. The plain string form starts a shell in between, and the program is killed after the stop timeout instead of ending cleanly.
Settings that change between hosts are read from environment variables in `app.py` (for example `os.environ["DATABASE_URL"]`), not written into the Containerfile.
### The ignore file
`.containerignore` keeps files out of the build. Everything not listed is sent to the build and may end up in the image through a `COPY`. From the [gfa-monitoring](https://gitea.psi.ch/controls_it/gfa_monitoring) project:
.git
.venv
venv
__pycache__
*.pyc
*.log
tests
.env
`.env` is in the list because it holds secrets on a developer's machine.
### Build and test locally
1. Build the image. The container hosts are x86_64 machines; on a Mac with Apple Silicon Podman builds for arm64 unless you name the platform, and that image does not start on the host:
podman build --platform linux/amd64 -t myapp:dev .
2. Run it in the foreground; `Ctrl-C` stops it:
podman run --rm --name myapp -p 8000:8000 myapp:dev
3. In a second terminal, look at it while it runs:
podman ps
podman logs -f myapp
4. Check that it stops cleanly. `podman stop` sends the stop signal and kills the process after 10 seconds:
time podman stop myapp
If this takes 10 seconds, the program does not end on the stop signal: check the `CMD` form and that your program handles `SIGTERM`.
5. Check the size in the SIZE column:
podman image ls myapp
Check that it worked: the application answers on `http://localhost:8000` (or prints its output), its messages appear in `podman logs`, and `podman stop` returns at once.
## 2. Publish the image to the registry
Controls images are published to the container registry built into Gitea, `gitea.psi.ch`.
### Name and tags
A published image is named after the Gitea organisation and repository it belongs to, in lower case:
gitea.psi.ch/<org>/<repository>:<tag>
One image gets several tags. The gfa-monitoring release workflow gives each release:
| Tag | Example | Meaning |
|---|---|---|
| full version | `1.4.0` | This exact release. Never moves. Use it to deploy. |
| minor version | `1.4` | Moves to the newest `1.4.x`. |
| `latest` | `latest` | Moves to the newest release. |
| commit | `sha-3f2a9c1` | The commit the image was built from. |
Raise the version for every image you publish: the last number for a fix, the middle one for a new function, the first one for a change that needs a new configuration or breaks something for its users. Do not push a published full version again with different content.
### Log in
1. In Gitea, open **Settings → Applications → Access Tokens** and create a token. Scope `write:package` to push (it includes reading), `read:package` for a machine that only pulls.
2. Log in and give the token when asked for the password:
podman login gitea.psi.ch -u <your Gitea user>
In a script, pass the token on standard input with `--password-stdin` so it does not appear in the command line.
### Push by hand
podman tag myapp:dev gitea.psi.ch/<org>/myapp:1.0.0
podman push gitea.psi.ch/<org>/myapp:1.0.0
Push further tags of the same image the same way (`1.0`, `latest`).
### Publish automatically on a version tag
The gfa-monitoring repository builds and publishes its image in Gitea Actions whenever a version tag is pushed (`.gitea/workflows/release.yml`). It works in two jobs:
1. **guard** checks that the tag points at a commit on `master`, because a tag can be pushed from any commit. A tag elsewhere is refused.
2. **publish** logs in to `gitea.psi.ch` with a token from the repository secret `REGISTRY_TOKEN`, derives the four tags from the version tag, builds for `linux/amd64` and pushes.
The built-in token of Gitea Actions has no package scope, so the push needs a token of its own: create one with `write:package` and store it in the repository under **Settings → Actions → Secrets** as `REGISTRY_TOKEN`. The publish job, trimmed to what a Python service needs:
on:
push:
tags: ["v*"]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: gitea.psi.ch
username: ${{ gitea.actor }}
password: ${{ secrets.REGISTRY_TOKEN }}
- id: meta
uses: docker/metadata-action@v5
with:
images: gitea.psi.ch/${{ gitea.repository }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-,format=short
type=raw,value=latest
- uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
Copy the guard job from the gfa-monitoring workflow as well if releases must come from `master` only. A release is then:
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0
### Check the published image
1. In Gitea, open the organisation and its **Packages** tab (`https://gitea.psi.ch/<org>/-/packages`). The image is listed with its tags.
2. On another machine, pull it and start it once:
podman pull gitea.psi.ch/<org>/myapp:1.0.0
podman run --rm gitea.psi.ch/<org>/myapp:1.0.0
### When build or push fails
- **`unauthorized` on push or pull**: not logged in, or the token lacks `write:package` / `read:package`. In Gitea Actions: `REGISTRY_TOKEN` is not set; the built-in token cannot push.
- **`exec format error` when the container starts on the host**: the image was built for arm64. Build with `--platform linux/amd64`.
- **`invalid reference format`**: the image name contains upper-case letters. Use lower case.
- **Image too large, slow push and pull**: use a `-slim` base, `--no-cache-dir`, and a `.containerignore` that keeps `.git`, virtual environments and data out.
- **`(already exists)` when you push the Git tag `v1.0.0`**: that version has been released already. Raise the version.
## 3. Run it on a container host
A container host is a Controls server that runs containers; you get one by [requesting a server](../../2._Services_Portfolio/Services/02_computingandboot.md#how-to-request-a-machine-or-a-change) for your service. The pattern in use today:
- Each container is a systemd unit that pulls the image and starts it with `podman run`, so it starts at boot and restarts when it stops.
- The unit file is kept in Git; Puppet installs it in `/etc/systemd/system/` through a [change to the machine](../../2._Services_Portfolio/Services/02_computingandboot.md#request-a-change-to-an-existing-machine). Nothing is started by hand.
- Ports with `-p`, data that must survive in a host directory with `-v`, secrets in an environment file on the host (`--env-file`), never in Git or the image.
- The unit names a full version tag. Updating is a new tag in the unit and a restart; rolling back is the previous tag.
- The output goes to the journal and from there to Elastic: read it with `journalctl -u myapp` on the host and in Kibana ([Logging](../../2._Services_Portfolio/Services/03_devops.md#logging)).
A compact example unit:
[Unit]
Description=myapp
After=network-online.target
[Service]
ExecStartPre=-/usr/bin/podman rm -f myapp
ExecStartPre=/usr/bin/podman pull gitea.psi.ch/<org>/myapp:1.0.0
ExecStart=/usr/bin/podman run --rm --name myapp --init -p 8000:8000 \
-v /data/myapp:/app/data --env-file /etc/myapp/myapp.env \
gitea.psi.ch/<org>/myapp:1.0.0
ExecStop=/usr/bin/podman stop myapp
Restart=always
[Install]
WantedBy=multi-user.target
Today these units run as root. Planned: rootless Podman under a service account, Quadlet unit files, and deployment through the catalogue (`cas container deploy`, not implemented yet). Controls IT owns the container host, your group owns the image and what runs in it ([who is responsible for what](../../2._Services_Portfolio/Services/02_computingandboot.md#who-is-responsible-for-what)).
Soft IOCs are planned to run as rootless Podman containers under systemd where they suit it, once Channel Access in containers has been validated. Until then they run on [soft IOC hosts](../../2._Services_Portfolio/Services/02_computingandboot.md#soft-ioc-hosts-and-hardware-bound-ioc-hosts).
@@ -0,0 +1,126 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 960 420" role="img" aria-labelledby="t d">
<title id="t">Build and publish a container image</title>
<desc id="d">Step 1, source repository: Containerfile, .containerignore, requirements.txt and code in Git. Step 2, build: podman build --platform linux/amd64 makes the image. Step 3, tag: the image gets the name gitea.psi.ch/org/myapp with the tags 1.0.0, 1.0, latest and sha-commit. Step 4, push to the registry gitea.psi.ch, by hand with podman push or by Gitea Actions on a version tag v1.0.0. Last, a container host pulls the image and runs it with Podman under systemd.</desc>
<style>
text { font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; fill: #212121; }
.mono { font-family: "JetBrains Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
.h { font-size: 17px; font-weight: 600; }
.t { font-size: 14px; }
.cap { font-size: 13px; fill: #5f6368; }
.code { font-size: 12px; fill: #303fa1; }
.card { fill: #f7f8fc; stroke: #e1e4f2; stroke-width: 1.5; }
.card2 { fill: #ffffff; stroke: #e0e0e0; stroke-width: 1.5; stroke-dasharray: 5 4; }
.tile { fill: #e8eaf6; }
.inner { fill: #ffffff; stroke: #c5cae9; stroke-width: 1.5; }
.ic { fill: none; stroke: #4051b5; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icm { fill: none; stroke: #757575; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.track { fill: none; stroke: #4051b5; stroke-width: 2.5; }
.wire { fill: none; stroke: #9fa8da; stroke-width: 2; stroke-dasharray: 5 4; }
</style>
<defs>
<symbol id="i-git" viewBox="0 0 24 24"><circle cx="6" cy="6" r="2.5"/><circle cx="6" cy="18" r="2.5"/><circle cx="18" cy="9" r="2.5"/><path d="M6 8.5v7M18 11.5c0 3-4 3.5-10 5.5"/></symbol>
<symbol id="i-box" viewBox="0 0 24 24"><path d="M3 7.5 12 3l9 4.5v9L12 21l-9-4.5z"/><path d="M3 7.5 12 12l9-4.5M12 12v9"/></symbol>
<symbol id="i-tag" viewBox="0 0 24 24"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9z"/><circle cx="7.5" cy="7.5" r="1.3"/></symbol>
<symbol id="i-db" viewBox="0 0 24 24"><ellipse cx="12" cy="5.5" rx="8" ry="3"/><path d="M4 5.5v13c0 1.7 3.6 3 8 3s8-1.3 8-3v-13M4 12c0 1.7 3.6 3 8 3s8-1.3 8-3"/></symbol>
<symbol id="i-server" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="7.5" rx="2"/><rect x="3" y="13.5" width="18" height="7.5" rx="2"/><path d="M7 6.8h.01M7 17.2h.01"/></symbol>
<marker id="ah" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M1 1l8 4-8 4z" fill="#4051b5"/></marker>
<marker id="ahl" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M1 1l8 4-8 4z" fill="#9fa8da"/></marker>
</defs>
<rect x="1" y="1" width="958" height="418" rx="10" fill="#ffffff" stroke="#e0e0e0" stroke-width="1.5"/>
<text class="cap" x="24" y="30" font-weight="600" style="fill:#4051b5">BUILD AND PUBLISH</text>
<text class="cap" x="778" y="30" font-weight="600">RUN</text>
<!-- track -->
<path class="track" d="M115 58H262" marker-end="url(#ah)"/>
<path class="track" d="M291 58H441" marker-end="url(#ah)"/>
<path class="track" d="M470 58H636" marker-end="url(#ah)"/>
<path class="wire" d="M665 58H836" marker-end="url(#ahl)"/>
<g font-size="15" font-weight="600">
<circle cx="100" cy="58" r="14" fill="#4051b5"/><text x="100" y="63" text-anchor="middle" style="fill:#fff">1</text>
<circle cx="276" cy="58" r="14" fill="#4051b5"/><text x="276" y="63" text-anchor="middle" style="fill:#fff">2</text>
<circle cx="455" cy="58" r="14" fill="#4051b5"/><text x="455" y="63" text-anchor="middle" style="fill:#fff">3</text>
<circle cx="650" cy="58" r="14" fill="#4051b5"/><text x="650" y="63" text-anchor="middle" style="fill:#fff">4</text>
</g>
<!-- 1 repo -->
<g transform="translate(24 88)">
<rect class="card" width="152" height="306" rx="8"/>
<rect class="tile" x="14" y="14" width="36" height="36" rx="8"/>
<use href="#i-git" class="ic" x="20" y="20" width="24" height="24"/>
<text class="h" x="14" y="80">Source</text>
<text class="cap" x="14" y="100">Git repository</text>
<rect class="inner" x="8" y="114" width="136" height="94" rx="6"/>
<text class="mono code" x="15" y="136">Containerfile</text>
<text class="mono code" x="15" y="156">.containerignore</text>
<text class="mono code" x="15" y="176">requirements.txt</text>
<text class="mono code" x="15" y="196">app.py</text>
<text class="cap" x="14" y="236">No secrets,</text>
<text class="cap" x="14" y="254">no host settings</text>
</g>
<!-- 2 build -->
<g transform="translate(188 88)">
<rect class="card" width="176" height="306" rx="8"/>
<rect class="tile" x="14" y="14" width="36" height="36" rx="8"/>
<use href="#i-box" class="ic" x="20" y="20" width="24" height="24"/>
<text class="h" x="14" y="80">Build</text>
<text class="cap" x="14" y="100">Code + dependencies</text>
<text class="cap" x="14" y="118">become an image</text>
<rect class="inner" x="10" y="132" width="156" height="74" rx="6"/>
<text class="mono code" x="20" y="154">podman build</text>
<text class="mono code" x="20" y="174">--platform</text>
<text class="mono code" x="20" y="194">linux/amd64</text>
<text class="t" x="14" y="230">Test locally:</text>
<text class="mono code" x="14" y="252">podman run</text>
<text class="mono code" x="14" y="272">podman logs</text>
<text class="mono code" x="14" y="292">podman stop</text>
</g>
<!-- 3 tag -->
<g transform="translate(376 88)">
<rect class="card" width="158" height="306" rx="8"/>
<rect class="tile" x="14" y="14" width="36" height="36" rx="8"/>
<use href="#i-tag" class="ic" x="20" y="20" width="24" height="24"/>
<text class="h" x="14" y="80">Tag</text>
<text class="cap" x="14" y="100">Name and versions</text>
<text class="mono code" x="14" y="128">gitea.psi.ch/</text>
<text class="mono code" x="14" y="146">&lt;org&gt;/myapp</text>
<rect class="inner" x="12" y="162" width="134" height="96" rx="6"/>
<text class="mono code" x="22" y="184">:1.0.0</text>
<text class="mono code" x="22" y="204">:1.0</text>
<text class="mono code" x="22" y="224">:latest</text>
<text class="mono code" x="22" y="244">:sha-&lt;commit&gt;</text>
</g>
<!-- 4 push + registry -->
<g transform="translate(546 88)">
<rect class="card" width="210" height="306" rx="8"/>
<rect class="tile" x="14" y="14" width="36" height="36" rx="8"/>
<use href="#i-db" class="ic" x="20" y="20" width="24" height="24"/>
<text class="h" x="14" y="80">Push</text>
<text class="cap" x="14" y="100">Token: write:package</text>
<rect class="inner" x="12" y="114" width="186" height="50" rx="6"/>
<text class="t" x="22" y="134">By hand</text>
<text class="mono code" x="22" y="153">podman push</text>
<rect class="inner" x="12" y="172" width="186" height="50" rx="6"/>
<text class="t" x="22" y="192">Gitea Actions</text>
<text class="mono code" x="22" y="211">on git tag v1.0.0</text>
<rect x="12" y="236" width="186" height="54" rx="6" fill="#4051b5"/>
<text class="t" x="24" y="258" style="fill:#fff" font-weight="600">Registry</text>
<text class="mono code" x="24" y="278" style="fill:#fff">gitea.psi.ch</text>
</g>
<!-- run -->
<g transform="translate(772 88)">
<rect class="card2" width="164" height="306" rx="8"/>
<rect x="14" y="14" width="36" height="36" rx="8" fill="#f5f5f5"/>
<use href="#i-server" class="icm" x="20" y="20" width="24" height="24"/>
<text class="h" x="14" y="80">Container host</text>
<text class="mono code" x="14" y="110">podman pull</text>
<text class="mono code" x="14" y="130">podman run</text>
<text class="cap" x="14" y="160">in a systemd unit</text>
<text class="cap" x="14" y="186">Output goes to</text>
<text class="cap" x="14" y="204">the journal</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 7.9 KiB