$ cat job-board-api/part-28.md
Building a Job Board API, Part 28: One Image, Two Clouds

Where we are
Hi, I’m Reza. In my newsletter, Root Access, I am sharing one of my projects from the very beginning: a Job Board API built with Django, Postgres, Docker, AWS and Azure. I am a junior engineer looking for my first SRE, DevOps or Cloud job, and that is why I build my projects in public. You can see how I think, what breaks, and how I fix it.
In the last parts, I built the pieces: a CLOUD_PROVIDER switch, Blob Storage, Service Bus, and tests for the Azure path. But all of that ran in my dev setup, with my code folder mounted into the container and Django’s runserver. That is not how a real deployment runs.
So in this part, I prove the “Done when” line under production conditions:
- One image, built from a committed state and tagged with the commit hash.
- No bind mount, so the image runs its own code, under gunicorn.
- Four containers: an API and a worker for each cloud. Their environments differ by exactly one line

On both clouds, the same path must work: readiness, register, apply with a resume, download it through the cloud’s own link, change a status, and the worker sends the email.
I did not change any app code for the switch itself. It already existed. But the run found two other problems, and one of them taught me something important about queues.
Step 1: Build one image from a committed state
git status --short # should be empty
docker compose build api
SHA=$(git rev-parse --short HEAD); IMG=jobboard:$SHA
docker tag 00-job-board-api-api "$IMG"
echo "image: $IMG $(docker image inspect -f '{{.Id}}' "$IMG" | cut -c1-19)"
docker image inspect -f '{{json .Config.Cmd}}' "$IMG"
Line by line:
- git status –short must be empty first. The image is built from my working folder (COPY . .). If uncommitted changes went into it, the tag would name a commit whose code is not what is in the image.
- docker compose build api builds the runtime stage. That is the production image from Phase 4: non-root user, gunicorn, no dev dependencies.
- git rev-parse –short HEAD gives the short commit hash, 557de36. When I tag the image with it, the image name alone answers “which code is running?”. Later, my CI/CD pipeline will do this automatically.
- docker tag adds a second name to the same image. Nothing is copied.
- {{.Id}} is the image’s content hash. Two names with the same ID are one image.
- .Config.Cmd shows the default command: [“gunicorn”,”config.wsgi:application”,”–config”,”gunicorn.conf.py”]. So this is the production server, not runserver.
Result:
image: jobboard:557de36 sha256:e2e3fcc124dc
Step 2: Four containers from one image
NET=00-job-board-api_default
docker run -d --name jb6-aws-api --network $NET --env-file .env -e CLOUD_PROVIDER=aws -p 127.0.0.1:8106:8006 "$IMG"
docker run -d --name jb6-aws-worker --network $NET --env-file .env -e CLOUD_PROVIDER=aws "$IMG" python -m worker.run_worker
docker run -d --name jb6-az-api --network $NET --env-file .env -e CLOUD_PROVIDER=azure -p 127.0.0.1:8117:8006 "$IMG"
docker run -d --name jb6-az-worker --network $NET --env-file .env -e CLOUD_PROVIDER=azure "$IMG" python -m worker.run_worker
- docker run, not docker compose run. Compose would add the service’s bind mount (.:/app) and its runserver command. Plain docker run uses only the image, like a real deployment.
- –network $NET joins the Compose network, so the containers can reach db, redis, localstack, azurite and servicebus by name.
- –env-file .env is the same settings file for all four. It holds both clouds’ settings, and each container reads only its own branch.
- -e CLOUD_PROVIDER=… overrides the file’s value. This is the only difference.
- Two host ports, 8106 and 8117, on localhost only. Inside, gunicorn listens on 8006.
- “$IMG” python -m worker.run_worker. Anything after the image name replaces its default command. Same image, a different entry point: an API or a worker.
Proof 1: all four share one image, with no mounts
docker inspect -f '{{.Name}} image={{.Image}} mounts={{len .Mounts}}' jb6-aws-api jb6-aws-worker jb6-az-api jb6-az-worker | cut -c1-60
docker inspect -f '{{.Image}}' jb6-aws-api jb6-aws-worker jb6-az-api jb6-az-worker | sort -u | wc -l
The first command printed four lines, all starting with image=sha256:e2e3fcc124dc…. The second counts the unique image IDs, and it printed 1.
Proof 2: the environments differ by exactly one line
This was the part I wanted to show, not just claim. But the environments hold secrets, so I can’t just print them. diff solves this: it prints only the lines that differ. Both containers load the same .env, so every secret is the same, and it never gets printed.
diff <(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' jb6-aws-api | sort) \
<(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' jb6-az-api | sort)
- {{range .Config.Env}}{{println .}}{{end}} prints each variable on its own line.
- <( … ) is process substitution. It gives each command’s output to diff as if it were a file.
- sort makes the order not matter.
Result:
15c15
< CLOUD_PROVIDER=aws
---
> CLOUD_PROVIDER=azure
Exactly one line.
That is the “env vars only” part of the Done-when line, shown with evidence.
Each worker also logs its queue at startup:
- AWS: http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/job-board-status
- Azure: sb://servicebus/job-board-status (with no key in the URL)
Problem 1: The Azure worker was silent
What I saw
- First run: only one Emailed line, for the AWS event. The Azure worker printed nothing in 8 seconds.
- Second run, waiting 60 seconds and keeping the logs: the API logged Queued application.status_changed for application 3, so the event reached Service Bus. But the worker logged only Worker started, then nothing. It was still running, with restarts=0.
So the message was sent, and the worker was alive. Where did the message go? I checked three things, in order.
A. Is the message still in the queue?
with ServiceBusClient.from_connection_string(CS) as c:
for label, sub in (('main', None), ('dead-letter', ServiceBusSubQueue.DEAD_LETTER)):
with c.get_queue_receiver('job-board-status', sub_queue=sub) as r:
msgs = r.peek_messages(max_message_count=20)
print('QUEUE', label, len(msgs), [(m.message_id, m.delivery_count) for m in msgs])
A peek shows messages without locking or removing them, even locked ones. The result was main 0 [] and dead-letter 0 []. So the message was not waiting, and it had not failed. Something had received and completed it.
B. Is my worker stuck?
I started a diagnostic worker from the same image, with a stack dump every 30 seconds:
python -c "import faulthandler, runpy, sys; faulthandler.dump_traceback_later(30, repeat=True, file=sys.stderr); runpy.run_module('worker.run_worker', run_name='__main__')"
- dump_traceback_later(30, repeat=True) prints every thread’s stack every 30 seconds, and the process keeps running. In Step 2, I used exit=True to find a hang. Here I wanted the worker to keep working.
- runpy.run_module(…, run_name=’__main__’) runs the module exactly like python -m worker.run_worker.
The main thread was in receive_messages, inside the normal 20-second long poll, waiting for data. A second thread was in the SDK’s _keep_alive. Healthy and idle. The worker was not stuck. It simply had nothing to receive.
C. Who else is listening?
for c in $(docker ps -q); do
docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$c" | grep -qx 'CLOUD_PROVIDER=azure' \
&& docker inspect -f 'azure container: {{.Name}} (up since {{.State.StartedAt}})' "$c"
done
This finds every running container whose environment has exactly CLOUD_PROVIDER=azure. grep -q is silent and only sets the exit code, and -x needs the whole line to match. It prints only names and start times, never the secret values.
The result showed four containers. Two were expected (jb6-az-api and my diagnostic worker). And two were old: jb-azure and jb-azure-worker.
The old worker’s own log confirmed it. It had Emailed both of my Step 4 Azure events.
The cause: competing consumers
A queue gives each message to one receiver. Two Azure workers were listening on job-board-status, and the old one won both times.
Where did it come from? When I re-ran some checks for Part 26, my script started jb-azure and jb-azure-worker again, and it did not remove them at the end. The cleanup command came later, and they kept running for about 2.5 hours.
The fix was simple: I removed the old containers and started a clean jb6-az-worker. Result: worker emailed after ~4s.
What this teaches
- A silent consumer may be healthy and simply losing the race. My order now: first check where the message went (peek), then what the worker is doing (stack dump), then who else is consuming (list the consumers).
- The system behaved correctly. Each message was handled exactly once, by a worker. Many workers on one queue is how you scale. The bug was in my test setup.
- In real environments, each environment needs its own queue, so dev, staging and a test run can never steal each other’s messages. In a later project, that will be a queue name per environment in Terraform.
Problem 2: gunicorn and a home folder that does not exist
The Azure API logged this once at startup:
Control server error: [Errno 13] Permission denied: '/home/app'
Requests were not affected. To understand it, I read the source code of the installed gunicorn, instead of guessing:
- gunicorn 26 added a control socket. It is a Unix socket for runtime management with a new tool, gunicornc.
- Its default path: if XDG_RUNTIME_DIR is not set (and it isn’t in my container), gunicorn uses $HOME/.gunicorn/gunicorn.ctl. That is /home/app/.gunicorn/gunicorn.ctl.
- My non-root user app has no home folder. My Dockerfile from Phase 4 creates it with useradd –no-create-home. So the folder does not exist, and app can’t create anything under /home.
- Why I never saw it before: in development, Compose runs runserver. Only the image’s own command, gunicorn, triggers it. This step was the first time since gunicorn 26 that I ran the image’s real command.
The fix, in gunicorn.conf.py:
# gunicorn 26+ opens a control socket (for the gunicornc tool) in a default folder under $HOME.
# Our "app" user has no home folder, and the process is managed by Docker/Kubernetes signals,
# not gunicornc, so the socket is turned off.
control_socket_disable = True
Why turn it off, and not give app a home folder?
- Nothing uses gunicornc.
- Docker, and later Kubernetes, control the process with signals: SIGTERM means a graceful shutdown.
- An unused management socket is one more thing that can fail or be misused.
- A home folder would only hide the error and keep a feature I don’t need.
How I verified it:
- The line is in the image, not just in my file: docker run –rm –entrypoint grep … printed 40:control_socket_disable = True.
- gunicorn –print-config shows control_socket_disable = True. This proves the setting was loaded, not just written.
- Running the image: up after 2 seconds, healthz 200, and control server errors: 0.
One honest note: the proof ran on image 557de36, and this fix came after, in its own commit. The error did not affect the proof. Every request worked, and the error happened once at startup.
Problem 3: docker run requires at least 1 argument
A small one. I ran the commands in a new terminal, where IMG and NET were not set. So “$IMG” became an empty string, and Docker got no image name. Shell variables only live in the shell that set them. The fix: set NET=… and IMG=… at the top of the block, so it runs anywhere.
What I learned
- Build once, configure with the environment. One image, tagged with its commit, and behaviour changed by env vars only.
- Show it with evidence. docker inspect gave me the image IDs and the mount count, and diff compared the environments without printing any secret.
- Competing consumers are a feature, until an unexpected one is listening. Before debugging a silent worker, check who else is on the queue.
- faulthandler with repeat=True gives regular stack dumps of a process that keeps running.
- Read the library’s source when a new feature surprises you. It took a few grep and sed commands, and I knew exactly what the control socket does.
- Turn off what you don’t use in production servers.
- Clean up after every check. A test container left running for 2.5 hours broke a later test.
What is next
Phase 6’s “Done when” line is met: one image ID, no mounts, one different env line, and the full upload, download, publish and email path working on both clouds. This completes item 4 of my Phase 6 checklist.