我正在try 使用测试容器运行容器.以下是我的代码:

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,
    }
...

我在PATH变量中提到的目录存在.但我得到的错误是: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. 即使我用挂载注释行,我也会收到这个错误. 如何修复它?另外,在PATH变量("notes.sql")中输入文件名是否正确?或者我应该只在这里输入目录?

先谢谢你

Update 1

我现在也try 了这个代码:

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)
    }

收到相同的错误

Update 2

下面是我try 过的另一段代码:

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),
                },
            },

再也没有运气了:

Update 3

这是我的另一次try :

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

它也不起作用

推荐答案

我在我的机器上遇到了同样的问题.完整的错误消息为:

2023/07/11 12:15:35 github.com/testcontainers/testcontainers-go - Connected to docker:
  Server Version: 20.10.14
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 7912 MB
2023/07/11 12:15:35 ? Creating container for image docker.io/testcontainers/ryuk:0.5.1
panic: 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

正如@Pratheesh-PC所指出的,这意味着它无法从图像docker.io/testcontainers/ryuk:0.5.1创建容器.它与您想要创建的您自己的容器的装载配置无关.我认为@Pratheesh-PC的答案中提供的链接是针对Windows容器(WCOW)的,但我在Windows(LCOW)上运行Linux容器时遇到了这个问题.

一种解决办法是禁用收割机.事实上,你已经在你的代码中设置了SkipReaper: true,并且你打算禁用收割机.问题是,从github.com/testcontainers/testcontainers-go@v0.20.0开始,SkipReaper被完全忽略,您必须使用the following approaches中的一个将其禁用:

  1. ryuk.disabled=true加到.testcontainers.properties文件中.在Windows上,该文件的完整路径为C:/Users/%my_username%/.testcontainers.properties.
  2. 设置TESTCONTAINERS_RYUK_DISABLED=true环境变量.这种方式优先于属性文件.

Go相关问答推荐

GetSecretValue,get identity:get credentials:无法刷新缓存的凭据

难以为多个平台添加Go Bazel构建选项

使用GO从RDPMC获得价值

Golang中的泛型 struct /接口列表

将DATE类型的SQL字段扫描到GO struct 字段

golang regex基于关键字拆分字符串

go测试10m后如何避免超时

Golang:访问any类型泛型上的字段

运行 Docker 容器 - X11 无法打开显示:0

死锁 - 所有 goroutine 都处于睡眠状态(即使使用等待组)

更改多对多连接表的名称

如何使用 GolangCI 删除未使用的导入

致命错误 - 所有 Goroutines 都睡着了!僵局

go-echo中如何防止+转义

访问传递给可变参数函数的通用 struct 的特定字段

Golang计算 struct struct 中的字段数

Golang:隐式 struct 匹配

Golang 数据库/sql 与 SetMaxOpenConns 挂起

出于某种原因,Golang (Go) AES CBC 密文被填充了 16 个 0x00 字节

Golang - 使用正则表达式提取链接