Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 51e39c6

Browse files
committed
docker: build an image with proper permissions
Signed-off-by: Austin Seipp <aseipp@pobox.com> Change-Id: I3cbf2c4cdb1c15c87e95973a04ac5efa
1 parent 43c3024 commit 51e39c6

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

.github/workflows/docker.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ env:
1010

1111
jobs:
1212
build:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
runner: [ X64, arm-runner ]
1317
name: "update: build and deploy postgres server images"
14-
runs-on: ubuntu-latest
18+
runs-on: [ self-hosted, ${{ matrix.runner }} ]
1519
permissions:
1620
contents: read
1721
packages: write
1822
steps:
1923
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
2024
with:
2125
fetch-depth: 0
22-
- uses: DeterminateSystems/nix-installer-action@65d7c888b2778e8cf30a07a88422ccb23499bfb8
23-
- uses: DeterminateSystems/magic-nix-cache-action@749fc5bbc9fa49d60c2b93f6c4bc867b82e1d295
2426
- uses: actions/checkout@v3
2527

2628
- name: Build images

docker/init.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
# shellcheck shell=bash
3+
4+
sudo -u postgres /bin/initdb --locale=C -D /data
5+
sudo -u postgres ln -s /etc/postgresql.conf /data/postgresql.conf
6+
sudo -u postgres /bin/postgres -p 5432 -D /data

docs/docker.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Docker images are pushed to `ghcr.io` on every commit. Try the following:
2+
3+
```
4+
docker run --rm -it ghcr.io/supabase/nix-postgres-15:latest
5+
```
6+
7+
Every Docker image that is built on every push is given a tag that exactly
8+
corresponds to a Git commit in the repository &mdash; for example commit
9+
[d3e0c39d34e1bb4d37e058175a7bc376620f6868](https://github.com/supabase/nix-postgres/commit/d3e0c39d34e1bb4d37e058175a7bc376620f6868)
10+
in this repository has a tag in the container registry which can be used to pull
11+
exactly that version.
12+
13+
This just starts the server. Client container images are not provided; you can
14+
use `nix run` for that, as outlined [here](./start-client-server.md).

flake.nix

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,43 @@
173173

174174
# Make a Docker Image from a given PostgreSQL version and binary package.
175175
makePostgresDocker = version: binPackage:
176-
pkgs.dockerTools.buildLayeredImage {
176+
let
177+
initScript = pkgs.runCommand "docker-init.sh" {} ''
178+
mkdir -p $out/bin
179+
cp ${./docker/init.sh} $out/bin/init.sh
180+
chmod +x $out/bin/init.sh
181+
'';
182+
183+
postgresqlConfig = pkgs.runCommand "postgresql.conf" {} ''
184+
mkdir -p $out/etc/
185+
substitute ${./tests/postgresql.conf.in} $out/etc/postgresql.conf \
186+
--subst-var-by PGSODIUM_GETKEY_SCRIPT "${./tests/util/pgsodium_getkey.sh}"
187+
'';
188+
189+
in pkgs.dockerTools.buildImage {
177190
name = "postgresql-${version}";
178191
tag = "latest";
179-
contents = with pkgs; [ coreutils bash binPackage ];
192+
193+
runAsRoot = ''
194+
#!${pkgs.runtimeShell}
195+
${pkgs.dockerTools.shadowSetup}
196+
groupadd -r postgres
197+
useradd -r -g postgres postgres
198+
mkdir -p /data /run/postgresql
199+
chown postgres:postgres /data /run/postgresql
200+
'';
201+
202+
copyToRoot = pkgs.buildEnv {
203+
name = "image-root";
204+
paths = with pkgs; [
205+
initScript coreutils bash binPackage
206+
dockerTools.binSh sudo postgresqlConfig
207+
];
208+
pathsToLink = [ "/bin" "/etc" "/var" "/share" ];
209+
};
180210

181211
config = {
182-
Cmd = [ "/bin/postgres" ];
212+
Cmd = [ "/bin/init.sh" ];
183213
ExposedPorts = { "5432/tcp" = {}; };
184214
WorkingDir = "/data";
185215
Volumes = { "/data" = { }; };

tests/postgresql.conf.in

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@
5757

5858
# - Connection Settings -
5959

60-
#listen_addresses = 'localhost' # what IP address(es) to listen on;
61-
# comma-separated list of addresses;
62-
# defaults to 'localhost'; use '*' for all
63-
# (change requires restart)
60+
listen_addresses = '*' # what IP address(es) to listen on;
6461
#port = 5432 # (change requires restart)
6562
max_connections = 100 # (change requires restart)
6663
#superuser_reserved_connections = 3 # (change requires restart)

0 commit comments

Comments
 (0)