@@ -4,11 +4,14 @@ import (
44 "context"
55 "log"
66 "log/slog"
7+ "os"
8+ "path/filepath"
79 "time"
810
911 "github.com/jackc/pgx/v5/pgxpool"
1012 "github.com/l3montree-dev/devguard/database"
1113 "github.com/l3montree-dev/devguard/shared"
14+ "github.com/moby/moby/api/types/container"
1215 "github.com/testcontainers/testcontainers-go"
1316 "github.com/testcontainers/testcontainers-go/modules/postgres"
1417)
@@ -34,13 +37,40 @@ func InitRawDatabaseContainer(initDBSQLPath string) (*pgxpool.Pool, func()) {
3437 dbUser := "user"
3538 dbPassword := "password"
3639
40+ // The image has a read-only Nix filesystem so docker cp (used by WithInitScripts)
41+ // cannot write into the container. Instead we bind-mount the init SQL file,
42+ // mirroring how docker-compose.yaml mounts ./initdb.sql.
43+ absInitSQL , err := filepath .Abs (initDBSQLPath )
44+ if err != nil {
45+ panic ("could not resolve initdb SQL path: " + err .Error ())
46+ }
47+ if _ , err := os .Stat (absInitSQL ); err != nil {
48+ panic ("initdb SQL file not found: " + absInitSQL )
49+ }
50+
3751 postgresC , err := postgres .Run (ctx ,
38- "ghcr.io/l3montree-dev/devguard- postgresql:v0.4.16 " ,
52+ "ghcr.io/l3montree-dev/devguard/ postgresql:v1.3.1 " ,
3953 postgres .WithDatabase (dbName ),
4054 postgres .WithUsername (dbUser ),
4155 postgres .WithPassword (dbPassword ),
42- postgres .WithInitScripts (initDBSQLPath ),
4356 postgres .BasicWaitStrategies (),
57+ testcontainers .WithLogger (log .Default ()),
58+ // The postgres module overrides CMD to "postgres -c fsync=off", which drops the
59+ // image's config_file arg and makes postgres listen only on 127.0.0.1. We restore
60+ // the config_file so listen_addresses='*' takes effect for port mapping.
61+ testcontainers .WithCmd ("postgres" ,
62+ "-c" , "config_file=/etc/postgresql/postgresql.conf" ,
63+ "-c" , "fsync=off" ,
64+ ),
65+ testcontainers .WithTmpfs (map [string ]string {
66+ "/run/postgresql" : "rw" ,
67+ }),
68+ testcontainers .WithHostConfigModifier (func (hc * container.HostConfig ) {
69+ hc .ShmSize = 1 << 30 // 1 GiB — matches shm_size in docker-compose.yaml
70+ // Bind-mount the init SQL; WithInitScripts uses docker cp which fails on the
71+ // read-only Nix filesystem of this image.
72+ hc .Binds = append (hc .Binds , absInitSQL + ":/docker-entrypoint-initdb.d/init.sql:ro" )
73+ }),
4474 )
4575
4676 terminate := func () {
@@ -49,10 +79,40 @@ func InitRawDatabaseContainer(initDBSQLPath string) (*pgxpool.Pool, func()) {
4979 }
5080 }
5181 if err != nil {
82+ if postgresC != nil {
83+ if logs , lerr := postgresC .Logs (ctx ); lerr == nil {
84+ log .Printf ("=== container logs ===" )
85+ buf := make ([]byte , 64 * 1024 )
86+ for {
87+ n , rerr := logs .Read (buf )
88+ if n > 0 {
89+ log .Printf ("%s" , buf [:n ])
90+ }
91+ if rerr != nil {
92+ break
93+ }
94+ }
95+ logs .Close ()
96+ }
97+ }
5298 slog .Info ("failed to start postgres container" , "error" , err )
5399 panic (err )
54100 }
55101
102+ if logs , lerr := postgresC .Logs (ctx ); lerr == nil {
103+ buf := make ([]byte , 64 * 1024 )
104+ for {
105+ n , rerr := logs .Read (buf )
106+ if n > 0 {
107+ log .Printf ("=== postgres startup logs ===\n %s" , buf [:n ])
108+ }
109+ if rerr != nil {
110+ break
111+ }
112+ }
113+ logs .Close ()
114+ }
115+
56116 host , _ := postgresC .Host (ctx )
57117 port , _ := postgresC .MappedPort (ctx , "5432" )
58118
0 commit comments