Skip to content

Read Redis and PostgreSQL settings from environment variables - #541

Closed
professordiogodev wants to merge 3 commits into
dockersamples:mainfrom
professordiogodev:configurable-hosts
Closed

professordiogodev wants to merge 3 commits into
dockersamples:mainfrom
professordiogodev:configurable-hosts

Conversation

@professordiogodev

Copy link
Copy Markdown

Closes #370.

Why

vote, worker and result have the host names redis and db, and the postgres/postgres credentials, written into the code. That works wherever those names resolve (Compose, Swarm, the Kubernetes specs) and nowhere else. I use this app to teach a class how to run each service on its own server, and there was no way to point a service at a different Redis or PostgreSQL without editing the source.

What changes

New optional environment variables. Every default is the value that was hardcoded, so Compose, Swarm and Kubernetes behave exactly as before.

Service Variable Default
vote REDIS_HOST redis
worker REDIS_HOST redis
worker, result POSTGRES_HOST / POSTGRES_USER / POSTGRES_PASSWORD / POSTGRES_DB db / postgres / postgres / postgres

Three small commits, so any of them can be dropped:

  1. Read Redis and PostgreSQL settings from environment variables. The worker builds its connection string with NpgsqlConnectionStringBuilder, so a password containing ; or = works (a hand-built string would not).
  2. README: a short "Configuration" section with the table above.
  3. vote: stop dropping the last digit of the generated voter_id. hex() in Python 3 has no trailing L, so [2:-1] cuts a real hex digit (a Python 2 leftover). Unrelated to the rest; happy to move it to its own PR if you prefer.

How I tested it

On five separate Ubuntu 24.04 servers (vote, result, worker, Redis, PostgreSQL), with the three services running from this branch:

  • No variables set: the services find redis and db by DNS as before, a vote cast on the vote page arrives on the result page.
  • REDIS_HOST pointing at a wrong address: the vote page cannot reach Redis (the request fails). With the right address it works.
  • PostgreSQL password changed to Str0ng;p@ss=1: with POSTGRES_PASSWORD unset the worker logs "Waiting for db"; with it set on the worker and on result, votes flow end to end and the result page updates.

I did not run docker compose for this change (no Docker engine on my machine at the time), so please let CI confirm that. For the .NET build I compiled the worker against .NET 8 on those servers; the code uses only NpgsqlConnectionStringBuilder, which Npgsql 4.1.9 provides, but I did not build the net7.0 image.

vote, worker and result had the host names redis and db and the
postgres/postgres credentials written into the code, so the services could
only run where those names resolve (Compose, Swarm, Kubernetes).

Add REDIS_HOST, POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD and
POSTGRES_DB. Every default is the value that was hardcoded, so existing
deployments are unchanged. The worker builds its connection string with
NpgsqlConnectionStringBuilder so passwords containing ; or = work.
hex() in Python 3 has no trailing L, so [2:-1] cut a real hex digit
(a Python 2 leftover). The id is now the full 64 bits.
@professordiogodev

Copy link
Copy Markdown
Author

Closing this: I opened it by mistake before I had checked with the course it belongs to. Sorry for the noise.

@professordiogodev
professordiogodev deleted the configurable-hosts branch November 10, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add REDIS_HOST and POSTGRES_HOST option from env

2 participants