testcontainers-go
testcontainers-go copied to clipboard
Env map not getting passed to postgres 11.7 container
The Issue I'm attempting to get my project setup with testcontainers to test my database code. I'm using the postgres:11.7 image and everytime I try to run the test code, the postgres container stops instantly because the POSTGRES_PASSWORD environment variable isn't set, which the container needs to start correctly. This is the code i'm using to try and get the test container running:
var port = "5432/tcp"
req := testcontainers.ContainerRequest{
Image: "postgres:11.7",
ExposedPorts: []string{port},
BindMounts: map[string]string{"/home/sean/Projects/typer-site/seedTestDB.psql": "/docker-entrypoint-initdb.d/init.sql"},
Env: map[string]string{
"POSTGRES_PASSWORD": "password",
"POSTGRES_USER": "postgres",
"POSTGRES_DB": dbname,
},
WaitingFor: wait.ForListeningPort("5432/tcp"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return container, fmt.Errorf("failed to start container: %s", err.Error())
}
To Reproduce
To reproduce, just run go test .
in the directory with the testfile. OS is pop!_OS 20.04 on a Thinkpad T480 with go 1.16
Expected behavior The postgres container will start up with the proper environment variables passed to it.
docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 11
Server Version: 19.03.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.11.0-7614-generic
Operating System: Pop!_OS 20.04 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 23.25GiB
Name: thinkpad
ID: HTDS:UGOY:74BI:E35H:KYXP:YRXR:Y73O:YWLQ:G5N5:V5OM:K5CS:5KNF
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: **********
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
It works fine on Ununtu 20/go 1.16/PostgreSQL 11.7/testcontainers-go v0.11.0
what's in docker logs? try run the test without BindMounts
option
I ran it without the BindMounts
option but I get the same result. The error in the container logs is:
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
The error is the same with and without the BindMount
option being passed. When I inspect the running container, none of the environment variables are passed to it.
@smanierre could you show a full code?
The whole test file is located here https://pastebin.com/QBgyNZXD
I've changed the code https://pastebin.com/3iDahjbw
Run
DB_NAME="test" go test -v <file>.go
Here docker inspect
"Env": [
"POSTGRES_PASSWORD=password",
"POSTGRES_USER=postgres",
"POSTGRES_DB=test",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/11/bin",
"GOSU_VERSION=1.12",
"LANG=en_US.utf8",
"PG_MAJOR=11",
"PG_VERSION=11.7-2.pgdg90+1",
"PGDATA=/var/lib/postgresql/data"
],
Seeing this issue as well, env vars are not passed to the container
@smanierre did you check @Bablzz's pastebin https://pastebin.com/3iDahjbw?
This is the right way to pass the variables to the container, including the POSTGRES_PASSWORD
in the container request's Env:
req := testcontainers.ContainerRequest{
Image: "postgres:11.7",
ExposedPorts: []string{port},
//BindMounts: map[string]string{"/home/user/Projects/typer-site/seedTestDB.psql": "/docker-entrypoint-initdb.d/init.sql"},
Env: map[string]string{
"POSTGRES_PASSWORD": "password",
"POSTGRES_USER": "postgres",
"POSTGRES_DB": dbname,
},
WaitingFor: wait.ForListeningPort("5432/tcp"),
}
Apart from that, make sure you update the code for the BindMounts, there was a big refactor (and a breaking change) in https://github.com/testcontainers/testcontainers-go/pull/386
@smanierre @spinningarrow @Bablzz I think we can close this issue as stale.
Do you mind if I do it? Thanks!
Closing as not reproducible any more. Please let us know if you need anything else from this issue
Thanks!