skip to Main Content

I’m trying to run a container using testcontainers. Here is my code:

func createContainer(ctx context.Context) (testcontainers.Container, *gorm.DB, string, error) {
    var env = map[string]string{
        "POSTGRES_PASSWORD": DbPass,
        "POSTGRES_USER":     DbUser,
        "POSTGRES_DB":       DbName,
    }
    var port = "5432/tcp"

    path := `C:/Desktop/Folder/golang/TgBotReminder/internal/db/postgresql/migration/notes.sql`

    req := testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            Image:        "postgres:latest",
            ExposedPorts: []string{port},
            Env:          env,
            WaitingFor:   wait.ForLog("database system is ready to accept connections"),
            SkipReaper:   true,
            Mounts: testcontainers.Mounts(
                testcontainers.BindMount(path, "/docker-entrypoint-initdb.d"),
            ),
        },
        Started: true,
    }
...

The directory I’ve mentioned in the path variable exists. But I got the error: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /pipe/docker_engine: creating reaper failed: failed to create container.
I got this error even if I comment lines with Mounts.
How to fix it? And also is it correct to type filename in the path variable ("notes.sql")? Or should I only type the directory here?

Thank you in advance

Update 1

I also tried this code right now:

container, err := tspostgres.RunContainer(ctx,
        testcontainers.WithImage("postgres:latest"),
        tspostgres.WithInitScripts(filepath.Join(".", "test_migration", "notes.sql")),
        tspostgres.WithDatabase("test-db"),
        tspostgres.WithUsername("postgres"),
        tspostgres.WithPassword("postgres"),
        testcontainers.WithWaitStrategy(
            wait.ForLog("database system is ready to accept connections").
                WithOccurrence(2).WithStartupTimeout(5*time.Second)),
    )

    if err != nil {
        log.Fatal(err)
    }

got the same error

Update 2

Here is another piece of code I’ve tried:

path := `/${PWD}/desktop/folder/golang/tgbotreminder/internal/db/postgresql/migration/notes.sql`
target := "/docker-entrypoint-initdb.d/"
...
Mounts: testcontainers.ContainerMounts{
                testcontainers.ContainerMount{
                    Source: testcontainers.GenericBindMountSource{
                        HostPath: path,
                    },
                    Target: testcontainers.ContainerMountTarget(target),
                },
            },

No luck again 🙁

Update 3

Here is my another attempt:

Mounts: testcontainers.Mounts(testcontainers.ContainerMount{
                Source: testcontainers.GenericBindMountSource{
                        HostPath: path,
                },
                Target: testcontainers.ContainerMountTarget(target),
        }),

It also doesn’t work

2

Answers


  1. it seems like the issue lies with the Mounts try:

    Mounts: []testcontainers.Mount{
                {
                    Source: path,
                    Target: "/docker-entrypoint-initdb.d/notes.sql",
                    Type:   testcontainers.BindMountType,
                },
            },
    
    

    when specifying the source path for a bind mount, you should provide the complete path to the file, including the file name itself. So, it is correct to include the filename ("notes.sql") in the path variable.

    Login or Signup to reply.
  2. It seems like the issue is with the image Ryuk and some hard-coded values which is not compatible with windows.

    testcontainers-go currently doesn’t support "Windows containers": it has some hard-coded defaults that don’t consider the possibility of Windows containers.

    See https://github.com/testcontainers/testcontainers-go/issues/948

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search