STATUS: CONNECTING
LATENCY: --

One Containerfile, Four Stages: Dev, Test, Staging, and Production with Podman


From Development to Production with Podman describes the path one real application takes, including the stage I have not built. This post is the idea behind it, with every stage present. To keep the idea visible, the example is deliberately small: a PHP service called notes that stores short notes in PostgreSQL.

The pattern fits in one sentence. Build one image per commit from one Containerfile, and let the stages differ only in what is mounted into that image and how it is configured.

The Idea in One Diagram

  flowchart LR
    commit["Commit"]

    subgraph local["Laptop and CI"]
        direction TB
        dev["Development<br/>podman compose<br/>source bind-mounted"]
        test["Test<br/>shipping image<br/>disposable database"]
        dev --> test
    end

    subgraph hosts["Hosts"]
        direction TB
        staging["Staging<br/>Quadlet unit<br/>pre-release, real-shaped data"]
        prod["Production<br/>Quadlet unit<br/>stable release"]
        staging --> prod
    end

    commit --> dev
    test -- "tag" --> staging

Podman runs all four stages. Compose drives the first two and systemd drives the last two.

Each stage answers a different question. Development asks whether the change works. Test asks whether it broke anything else. Staging asks whether it survives real data and real use. Production is where the answer finally matters.

One Containerfile, Several Targets

Everything starts from a single multi-stage Containerfile.

# Containerfile
FROM docker.io/library/php:8.4-apache AS base
RUN apt-get update \
    && apt-get install -y --no-install-recommends libpq-dev unzip \
    && docker-php-ext-install pdo_pgsql \
    && rm -rf /var/lib/apt/lists/*
COPY --from=docker.io/library/composer:2 /usr/bin/composer /usr/bin/composer
ENV APACHE_DOCUMENT_ROOT=/app/public
RUN sed -ri 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \
    && sed -ri 's!/var/www/!/app/!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
WORKDIR /app

# Development: no source in the image. compose.yml bind-mounts it.
FROM base AS dev
RUN cp "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

# Production: source and runtime dependencies only.
FROM base AS prod
RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-interaction --no-progress
COPY . .

# Test: the production image plus the test tools.
FROM prod AS test
RUN composer install --no-interaction --no-progress
CMD ["vendor/bin/phpunit", "tests"]

The base target holds everything the stages share: the PHP version, the extensions, the web server configuration. dev and prod both start from it, so the runtime my code meets in development is the runtime it meets in production. test starts from prod, so the tests run on top of the exact files that will ship.

Two details are specific to Podman. Image names are fully qualified, because Podman does not assume Docker Hub for a bare php:8.4-apache. The file is named Containerfile, which Podman and Buildah look for first.

Development: Bind Mount and Compose

In development the image holds no source at all. A compose file mounts the working tree into it, puts a database beside it, and puts a proxy in front.

# compose.yml
services:
  app:
    build:
      context: .
      dockerfile: Containerfile
      target: dev
    environment:
      DATABASE_URL: postgres://notes:notes@postgres:5432/notes
    volumes:
      - ./:/app
    depends_on:
      postgres:
        condition: service_healthy

  proxy:
    image: docker.io/library/caddy:2
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
    ports:
      - "127.0.0.1:8443:443"
    depends_on:
      - app

  postgres:
    image: docker.io/library/postgres:18
    environment:
      POSTGRES_USER: notes
      POSTGRES_PASSWORD: notes
      POSTGRES_DB: notes
    volumes:
      - pgdata:/var/lib/postgresql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U notes -d notes"]
      interval: 1s
      retries: 30

volumes:
  pgdata:
  caddy-data:
podman compose up --build -d
podman compose exec app composer install
podman compose exec app php bin/migrate.php
curl -k https://notes.localhost:8443/health

PHP makes this stage easy. There is no compile step, so the bind mount is the hot reload: I save a file and the next request runs the new code, a second or two later once the opcode cache notices the change. A compiled language needs a file watcher inside the container to do the same job, which is what the companion post describes for Go.

The migration is a command I run, not something the service does when it starts. That choice looks minor here and matters at every later stage, because it lets a deploy decide exactly when the schema changes. Database Migrations makes the full case.

Names and HTTPS

Two things in that compose file work without any configuration of mine, and both are worth understanding.

Podman runs a DNS server for the stack. The application reaches its database at the hostname postgres, and the proxy reaches the application at app. Nobody wrote those names into a hosts file. Compose creates a network for the project, and Podman’s network stack starts a small resolver, aardvark-dns, on that network’s gateway. Every container on the network gets it as its nameserver, and it answers for each container’s name and each compose service name.

podman compose exec app getent hosts postgres
# 10.89.19.2      postgres.dns.podman

The names only exist inside that network. Two projects can each have a postgres without colliding, and nothing on the host can resolve either one. The same mechanism is what lets the test run below find its database as notes-test-db.

The outside name needs no DNS either. Browsers and most HTTP clients resolve anything ending in .localhost to the loopback address, so notes.localhost reaches the published port with no hosts entry.

HTTPS comes from the proxy. Caddy needs three lines.

# Caddyfile
notes.localhost {
    tls internal
    reverse_proxy app:80
}

tls internal makes Caddy create its own certificate authority and issue a certificate for notes.localhost. The application container publishes no port at all. Only the proxy does, on a high port, because rootless Podman cannot bind port 443 without extra configuration.

The certificate is real TLS, but nothing trusts Caddy’s private authority until I tell it to. curl -k skips the check, and importing Caddy’s root certificate into the system or browser trust store removes the warning for good. When a client refuses private authorities entirely, the alternative is a publicly trusted wildcard certificate for a development subdomain whose DNS record points at 127.0.0.1. The companion post uses that approach.

Serving HTTPS in development is not decoration. Secure cookies, OAuth redirects, and browser features that require a secure context all behave differently over plain HTTP. If production has TLS in front, development should too, so those differences show up on my laptop and not after a release.

Test: The Same Image, a Disposable Database

The test stage rests on two rules.

Test the image that ships. The test target adds the test tools on top of prod. It does not rebuild the application a different way. If a file is missing from the production image, the tests fail for the same reason production would.

Give every run its own database. The database is created for the run, kept in memory, and removed afterwards. No run can pass because of data an earlier run left behind.

# bin/test.sh (trimmed)
podman build --target test --tag notes-test .
podman network create notes-test

podman run --detach --name notes-test-db --network notes-test \
    --tmpfs /var/lib/postgresql \
    --env POSTGRES_USER=notes --env POSTGRES_PASSWORD=notes --env POSTGRES_DB=notes \
    docker.io/library/postgres:18
until podman exec notes-test-db pg_isready -U notes -d notes; do sleep 1; done

podman run --rm --network notes-test \
    --env DATABASE_URL=postgres://notes:notes@notes-test-db:5432/notes \
    notes-test

podman rm -f notes-test-db && podman network rm notes-test

The script needs nothing but Podman, so the same command runs on a laptop and on a CI runner. When a test fails in CI, I can reproduce it locally with the identical image and an identical empty database.

Staging: Production’s Shape, Pre-Release Code

Tests prove the code does what I expected. Staging exists for what I did not expect.

A staging instance is production in every respect except two: it runs a pre-release instead of a stable release, and nobody depends on it. It uses the same image build, the same unit file, the same proxy in front, and the same migration step. Its data should resemble production’s as closely as policy allows, ideally a recent copy.

That combination catches a class of problem nothing earlier can:

  • Migrations that pass but hurt. A schema change that takes milliseconds on fixtures can lock a table for minutes on real volumes.
  • Content nobody wrote a fixture for. Fixtures hold the cases I thought of. Real data holds the rest.
  • Design flaws. Using a feature through a whole real workflow, before it is released, is the cheapest moment to discover that the design is wrong.
  • The deploy itself. Every staging deploy is a rehearsal of the production deploy, using the same steps.

Staging is also the stage most often skipped, including by me. It costs a second instance and the discipline to use it. The companion post explains why I have gone without one so far and what stands in for it.

Production: A Quadlet Unit

On a host, compose gives way to systemd. Podman’s Quadlet turns a short declarative file into a real service.

# ~/.config/containers/systemd/notes.container
[Unit]
Description=notes service
After=network-online.target

[Container]
Image=localhost/notes:3f9c2ab
Pull=never
PublishPort=127.0.0.1:8080:80
Secret=notes-database-url,type=env,target=DATABASE_URL

[Service]
Restart=always
RestartSec=5

[Install]
WantedBy=default.target

The unit publishes its port on loopback only. A proxy on the host terminates TLS and forwards to it, which is the same arrangement as the development stack with a public certificate in place of the private one.

The unit lives in the home directory of a dedicated service user and runs rootless. After a systemctl --user daemon-reload it is an ordinary service named notes.service: it starts at boot, restarts on failure, and logs to the journal.

Staging uses this same file. The two instances run under different users, so each has its own image store and its own secret named notes-database-url. Only the image tag and the published port differ, and a deploy tool can fill those in from two variables.

The image tag is a commit, not latest, and Pull=never means the unit will not go looking for anything else. What runs is exactly what was deployed, until the next deploy changes the file.

A deploy to either stage follows one order: build or fetch the new image, back up the database, stop the service, run the migration from the new image, start the service. Everything that can fail safely comes before the stop.

What Stays the Same and What Changes

DevelopmentTestStagingProduction
Image targetdevtest (on prod)prodprod
Source codeBind-mountedCopied inCopied inCopied in
VersionWorking treeCommitPre-release tagStable tag
DatabaseNamed volumeIn memory, per runCopy of productionProduction
SecretsPlain values in composePlain values in the scriptPodman secretsPodman secrets
Reached athttps://notes.localhostNetwork-internal nameStaging hostname, TLS at the proxyPublic hostname, TLS at the proxy
Started bypodman composepodman runsystemd (Quadlet)systemd (Quadlet)
MigrationsRun by handRun by the testsDeploy stepDeploy step

Reading across a row shows where a stage is allowed to differ. Reading down the last two columns shows the point of staging: apart from the version and the data, there is nothing to tell them apart.

The base image, the PHP version, the extensions, and the way dependencies are installed appear nowhere in the table, because they never change. For what --no-dev leaves out and when to commit dependencies instead, see The Vendor Pattern with Composer.

Registry or Build on the Host

One decision remains: how the image reaches a host.

With several hosts, build once in CI, push to a registry, and promote by digest. Staging pulls the digest, and production later pulls the same digest. Nothing is rebuilt between them, so what was tested is what ships.

With one host, a registry can be more machinery than the problem needs. The host can build the image itself from a tagged commit, and the tag in Git is what gets promoted. That is the route I took, and the companion post covers the reasons and the trade-offs.

Either way the rule is the same: an image is identified by something immutable, and promotion moves that identifier, not a rebuilt copy.

Where to Start

The stages do not have to arrive together. This is the order I would adopt them in:

  1. The Containerfile, with a prod target that runs the application.
  2. The compose file, so development uses the same base as production.
  3. The test run, against the shipping image and a throwaway database.
  4. The Quadlet unit, replacing however production is started today.
  5. Staging, once a release has surprised you often enough to justify it.

Each step is useful without the next one. The thread that connects them is small: one file that describes the image, and the habit of changing only what is around it.


Copyright 2026 Jared Rickert. All rights reserved. ยท