testcontainers / testcontainers-go

Testcontainers for Go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers that should be run as part of a test and clean up those resources when the test is done.
https://golang.testcontainers.org
MIT License
3.7k stars 507 forks source link

Postgres container not working #250

Closed sazzer closed 2 years ago

sazzer commented 4 years ago

Describe the bug Using Testcontainers to start a Postgres container seems to not work at all. The container fails to start correctly.

To Reproduce

func TestDatabase(t *testing.T) {
    ctx := context.Background()

    req := testcontainers.ContainerRequest{
        Image:        "postgres",
        ExposedPorts: []string{"5432/tcp"},
        WaitingFor:   wait.ForListeningPort("5432/tcp"),
    }

    pgC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: req,
        Started:          true,
    })
    if err != nil {
        t.Fatal(err)
        }
}

Output from this is:

2020/09/29 10:25:20 Starting container id: aec37729692a image: quay.io/testcontainers/ryuk:0.2.3
2020/09/29 10:25:21 Waiting for container id aec37729692a image: quay.io/testcontainers/ryuk:0.2.3
2020/09/29 10:25:21 Container is ready id: aec37729692a image: quay.io/testcontainers/ryuk:0.2.3
2020/09/29 10:25:36 Starting container id: e152c14538d1 image: postgres
2020/09/29 10:25:37 Waiting for container id e152c14538d1 image: postgres
    database.go:37: failed to start container: /bin/sh command not executable

Expected behavior The container to start correctly.

docker info

-> % docker info
Client:
 Debug Mode: false
 Plugins:
  app: Docker Application (Docker Inc., v0.8.0)
  buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
  scan: Docker Scan (Docker Inc., v0.3.3)

Server:
 Containers: 149
  Running: 11
  Paused: 0
  Stopped: 138
 Images: 356
 Server Version: 19.03.13-beta2
 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:
  seccomp
   Profile: default
 Kernel Version: 4.19.76-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 1.944GiB
 Name: docker-desktop
 ID: 5JTZ:4FAH:4V47:ZBQE:DE4F:MAUI:JPMA:R33M:TKFO:CFKJ:MLPK:HBTK
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 76
  Goroutines: 77
  System Time: 2020-09-29T09:13:29.094983073Z
  EventsListeners: 4
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: true
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Additional context Other containers seem to work fine. I've tested the exact code above with nginx and redis successfully.

This container itself also works fine when running normally, via docker run or docker-compose. And I've done this recently with the Rust Testcontainers with no problems.

I've had this exact error with all of the postgres containers I've tried - different versions, alpine or buster, etc.

This is running on go 1.15.2, but also reproduces on 1.14.7.

sazzer commented 4 years ago

Just to rule it out, I've just deleted all containers and images and tried again, with the same results.

sazzer commented 4 years ago

It turns out it's a configuration error, but the output is very unhelpful.

The problem was that I need to provide some environment params for the container to start, and without them it fails. This works:

    req := testcontainers.ContainerRequest{
        Image: "postgres:12.4-alpine",
        Env: map[string]string{
            "POSTGRES_DB":       "test",
            "POSTGRES_USER":     "test",
            "POSTGRES_PASSWORD": "test",
        },
        ExposedPorts: []string{"5432/tcp"},
        WaitingFor:   wait.ForListeningPort("5432/tcp"),
    }

However, I only worked that out by actually putting a sleep after the startup and then looking at the container logs with docker logs -f. The output from testcontainers wasn't helpful at all :(

cburgmer commented 4 years ago

See #252 .

I did not find this issue as I was searching for the error message.

mdelapenya commented 2 years ago

Hi, I'm checking the postgres docs for their Docker image and they mention the need of explicitly setting the POSTGRES_PASSWORD variable to make it:

POSTGRES_PASSWORD

This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.

I'm closing this issue as it's not related to testcontainers-go. Thanks!