Fix a few issues in devcontainers.#2516
Conversation
1f55cbe to
45533ef
Compare
|
This was a curious one. At first I thought it might just have been my environment (and I'm happy to accept that it might be still). I also didn't I think it would be more than a single issue. So, if is helpful to vet, then I'm happy to provide debugging steps/details that led me to each issue and solution. |
While working on jackc#2510 I encountered some issues with devcontainer that were introduced by `9b5e030e`: * Unsupported client encoding for CRDB. * SSL command-line flags cause the temp server to fail before initdb has copied the certs. * `:whoami` user creation, when the user already exists. * Postgres instances sharing `/var/run/postgresql` race on the default socket port. These changes should resolve each.
45533ef to
bb72444
Compare
|
Thanks! I had just noticed my own devcontainer setup was broken after a reset and this change seems to resolve it. Presumably, my initial SSL update only worked if the containers and volumes had already been set up by the original non-SSL testing devcontainer script. |
|
Yeah, forgetting to clean up the volumes between setups was a 'gotcha' for me as well. But, I felt like it was the upstream docker image behaviors that presented the real head aches/scratchers. An interesting exercise nonetheless. 🙂 |
While working on #2510 I encountered some issues with devcontainers that were
mostly introduced by
9b5e030e:the certs.
:whoamiuser creation, when the user already exists./var/run/postgresqlrace on the default socketport.
The client encoding issue was discovered through some failing tests.
Specifically, with error:
no database or schema specified. Digging in, itbecame clear that the issue was rooted in the
init_crdbfunction intest.shbut was being silenced as
stderris redirected to/dev/null.On startup
psqlattempts to determine from the environment[1] which clientencoding to use, falling back to
sqlascii[2] when not set. Unfortunately,CRDB does not support this encoding[3], therefore attempts to setup CRDB
silently fails. We resolve this by simply setting
PGCLIENTENCODINGtoutf8.Removing the redirect demonstrates this:
The SSL flag is a bit of a chicken-egg issue. The Docker entrypoint starts a
temporary server, using it to run initialization scripts, passing it the same
command-line arguments as the final server[4,5]. So, the problem is that because
-c ssl=onis set, the temporary server looks for the cert files that it istrying to create. The seemingly simplest solution to this is to just move the
SSL configuration out of the command-line arguments and into
postgresql.confdirectly.
The
:whoamiuser creation was an interesting one. The simple explanation isthat when the setup script is run by the container, the user is
postgres,which obviously already exists. And further, since
ON_ERROR_STOPis set[6],this causes the execution of the setup to halt. So, the solution is an attempt
to simulate what I believe
CREATE USER ... IF NOT EXISTSwould do if it wereactually supported.
For the pg-sockets volume issue, all instances share the same
/var/run/postgresql. Each container must have a unique PGPORT set, because the
entrypoint always appends
-p ${PGPORT:-5432}[7] when starting the temporaryserver, overriding any
-c port=from the user command. Without it, alltemporary servers race to bind port 5432 on the shared socket volume.
[1] https://github.com/postgres/postgres/blob/03facc1211b0ff1550f41bcd4da09329080c30f9/src/bin/psql/startup.c#L157
[2] https://github.com/postgres/postgres/blob/03facc1211b0ff1550f41bcd4da09329080c30f9/src/interfaces/libpq/fe-misc.c#L1281-L1298
[3] https://go.crdb.dev/issue-v/35882/v25.4
[4] https://github.com/docker-library/postgres/blob/6edb0a8c4def40c371514b34aef9037ec82d9110/docker-entrypoint.sh#L355
[5] https://github.com/docker-library/postgres/blob/6edb0a8c4def40c371514b34aef9037ec82d9110/docker-entrypoint.sh#L377
[6] https://github.com/docker-library/postgres/blob/6edb0a8c4def40c371514b34aef9037ec82d9110/docker-entrypoint.sh#L207
[7] https://github.com/docker-library/postgres/blob/6edb0a8c4def40c371514b34aef9037ec82d9110/docker-entrypoint.sh#L292