Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GoLang postgres testcontainers init script doesn't work

I want to start postgres container with init script.

request := testcontainers.ContainerRequest{
        Image:      "postgres:14.1-alpine",
        Entrypoint: nil,
        Env: map[string]string{
            "POSTGRES_DB":       "postgres",
            "PGUSER":            "postgres",
            "POSTGRES_PASSWORD": "postgres",
            //"PGDATA":            "postgres",
        },
        ExposedPorts: []string{"5432"},
        BindMounts: map[string]string{
            "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
        },
        Name:       "postgres",
        User:       "postgres",
        WaitingFor: wait.ForLog("database system is ready to accept connections"),
        AutoRemove: true,
    }
container, err = testcontainers.GenericContainer(
        test.CTX,
        testcontainers.GenericContainerRequest{
            ContainerRequest: request,
            Started:          true,
        },
    )
    if err != nil {
        log.Panicln(err)
    }

I got the following panic messages in log.Panicln(err):

failed to create container
Error response from daemon
invalid mount config for type "bind": bind source path does not exist: /docker-entrypoint-initdb.d

The point is that it perfectly works from docker-compose.yml.
How to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looking at the source, it appears that TestContainers wants container_path: host_path in BindMounts. What happens if you try:

        BindMounts: map[string]string{
             "/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
        },

It looks like more recent versions of TestContainers have removed BindMounts completely and replaced it with a more generic Mounts field.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading