Shipping the core in an image: the Dockerfile and the host run

A service ships as an image, and an image of this project has one unusual property: the language that built the binary is not the language that runs it. GHC is needed for the build and is not needed at run time, the binary needs two C libraries that GHC does not carry, and Python needs neither GHC nor the Haskell libraries. A multi-stage build expresses exactly that.

The image was built and started on the host that holds this checkout, which has Docker, and the first run did not answer: the service listened on the loopback address inside the container, and a published port forwards to nothing there. The fix is one environment variable and one line of the Dockerfile, and the page shows both the failure and the run that works.

Built and run on the host

Not built in this container. The agent image has no Docker, and the evidence is one command that prints nothing and exits 1:

command -v docker; echo "status $?"

status 1

Run on the host, Docker, not in this container. The image was built on the host machine that holds this checkout, with Docker 28 and the alpine:3.24 base image, and the container was started from the published port 18080. Everything in this chapter is that host run; nothing in it was produced by the agent container.

docker build -t pricing-core .

naming to docker.io/library/pricing-core

The build log is long and is not reproduced here; its last line is the tag it wrote. The image it produced is 53.1 MB, which is the size the host reports for it:

docker images pricing-core --format '{{.Size}}'

53.1MB

The address that answered nobody

The container started, the log said it was listening, and the port did not answer. The call and what it printed:

curl -s -w " %{http_code}\n" -X POST --data 'PRICE tea 3' http://127.0.0.1:18080/quote

 000

000 is curl reporting that it got no HTTP status at all: the connection was refused or closed before a response. The service was running and had printed its listening line, because at that moment the code bound 127.0.0.1, and inside a container that address is the container's own loopback. The published port -p 18080:8080 forwards to the container's network interface, where nothing was listening, so no request reached the handler.

The rule behind it: an address inside a container is not an address on the host. A process that has to be reachable through a published port binds 0.0.0.0, which means every interface of the container, and the port mapping decides what the outside world sees. A process that only serves its own container, a sidecar or a test helper, binds the loopback and is unreachable from outside by design.

The fix is in two places. The service reads the address from the environment, so the same file serves both cases: host = os.environ.get("BIND", "127.0.0.1"), and it prints the address it bound. The Dockerfile sets the variable before the process starts: ENV BIND=0.0.0.0. The service page has the file in full.

The rebuilt image, started the same way, answered. Its log names the address it bound, which is the line to look for when a port does not answer:

docker run -d --rm --name pc -p 18080:8080 pricing-core
docker logs pc

listening on http://0.0.0.0:8080/quote

The two requests of the protocol, through the published port, with the status on the same line:

curl -s -w " %{http_code}\n" -X POST --data 'PRICE tea 3' http://127.0.0.1:18080/quote
curl -s -w " %{http_code}\n" -X POST --data 'PRICE gold 1' http://127.0.0.1:18080/quote

{"line": "OK 750 7.50", "total": 750, "money": "7.50"} 200
{"line": "ERR E_SKU unknown sku: gold", "error": "E_SKU", "message": "unknown sku: gold"} 400

The answers are the ones the service gave in this container on port 8765, byte for byte, which is the point of testing the layer before it is packaged. The last lines of the container log are the two requests the handler wrote:

docker logs pc

"POST /quote HTTP/1.1" 200 -
"POST /quote HTTP/1.1" 400 -

What goes into the image

Four files and one binary. The sources are the two Haskell modules of the core; the binary is what GHC makes of them; the Python files are the layer of the previous pages. Nothing else is needed, because the core imports only base.

Pricing.hs Main.hs protocol.py service.py

The binary is the one object whose dependencies are not obvious. It is linked against the C library of the machine that built it, which is musl on Alpine, and against two C libraries that the Haskell runtime and the text package use:

ldd ./core

    /lib/ld-musl-x86_64.so.1 (0x778499215000)
    libgmp.so.10 => /usr/lib/libgmp.so.10 (0x7784991ac000)
    libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x778499215000)
    libffi.so.8 => /usr/lib/libffi.so.8 (0x7784991a2000)
status 0

On Alpine those two are the packages gmp and libffi, which is why the runtime stage installs them by name instead of copying files. A glibc distribution would need its own names and its own image; the C library of the build machine and of the run machine have to be the same family, which is the reason a binary built on Alpine does not run on Debian.

The Dockerfile

# syntax=docker/dockerfile:1 FROM alpine:3.24 AS build RUN apk add --no-cache ghc WORKDIR /src COPY Pricing.hs Main.hs ./ RUN ghc -Wall -O1 -o core Main.hs Pricing.hs FROM alpine:3.24 RUN apk add --no-cache python3 gmp libffi WORKDIR /app COPY --from=build /src/core ./core COPY protocol.py service.py ./ EXPOSE 8080 ENV BIND=0.0.0.0 USER nobody CMD ["python3", "service.py", "8080"]

Two stages. The first installs ghc and compiles the core, so the compiler and its libraries exist only while the image is built. The second installs the Python interpreter and the two C libraries, copies the binary out of the first stage and the Python files from the build context, sets the bind address and runs the service as nobody.

Why two stages

The GHC package on Alpine is a large installation, and none of it is needed once the binary exists. A single-stage image would carry the compiler, its documentation and its package database into production, which is both a size and a surface: a compiler on a host is a tool for an attacker who already has a shell. The build stage keeps both out of the shipped image, and the 53.1 MB above is what the result weighs.

The second reason is reproducibility of the build versus the run. The build stage is pinned by the tag of the base image and by the GHC package of that distribution, and the run stage is pinned by the same tag plus Python. A rebuild that changes the compiler does not change the runtime, and a runtime patch does not force a rebuild of the binary.

The last lines are the ones to think about. USER nobody keeps the shipped process unprivileged: the service binds port 8080, which is above 1024 and needs no privilege, and it never writes a file. ENV BIND=0.0.0.0 is the line the host run above is about: without it the container starts, logs that it is listening, and answers nobody.

The image of the shared library

The ctypes way ships a different image, and it is harder. The .so does not contain the Haskell runtime and the libraries it uses: the build on the ctypes page names them as dependencies, so the image has to carry GHC's shared libraries too, or the loader fails at the first import with a missing symbol. That means either the GHC package in the runtime stage, which is the size the two-stage build was avoiding, or copying the few .so files and setting a loader path for them.

The same page also shows why the ABI matters: the library is linked against musl, so the image base has to be Alpine. Between the two ways, the process boundary is the one that ships more easily, and the library boundary is the one that calls more cheaply. The timing page has the numbers that decide it, and this page has what each decision costs at deployment time.

The Haskell lesson on packaging is a Cabal project, offline, and the project hub is A Haskell core in a modern project.

The image was built and run on the host with Docker 28 and Alpine 3.24; the binary it carries was built with GHC 9.10.3, and the ldd report was taken in this container, which has no Docker of its own.

Article author: Arthur Isaev