Go语言 测试容器:如何修复绑定源路径不存在

snvhrwxg  于 2023-08-01  发布在  Go
关注(0)|答案(4)|浏览(87)

我正在尝试使用testcontainers运行一个容器。下面是我的代码:

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

字符串
我在路径变量中提到的目录存在。但我得到了错误: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。即使我用挂载注解行也会得到这个错误。怎么修?在路径变量(“notes.sql”)中输入filename是否正确?还是我应该只在这里输入目录?
先谢谢你了

更新1

我现在也试了这个代码:

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


得到相同的错误

更新2

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

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


没有运气了:(

更新3

以下是我的另一个尝试:

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


它也不工作

relj7zay

relj7zay1#

问题似乎是图像Ryuk和一些与windows不兼容的硬编码值。
testcontainers-go目前不支持“Windows容器”:它有一些硬编码的默认值,不考虑Windows容器的可能性。
参见https://github.com/testcontainers/testcontainers-go/issues/948

jtw3ybtb

jtw3ybtb2#

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

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上的Windows容器(WCOW)的,但我在Windows上运行Linux容器(LCOW)时遇到了这个问题。
解决方法是禁用收割器。实际上,您已经在代码中设置了SkipReaper: true,并且您打算禁用reaper。问题是,从github.com/testcontainers/testcontainers-go@v0.20.0SkipReaper被完全忽略,您必须使用以下方法之一禁用它:
1.将ryuk.disabled=true添加到.testcontainers.properties文件。在Windows上,文件的完整路径是C:/Users/%my_username%/.testcontainers.properties
1.设置TESTCONTAINERS_RYUK_DISABLED=true环境变量此方式优先于属性文件。

dxpyg8gm

dxpyg8gm3#

问题似乎在于坐骑的尝试:

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

字符串
当指定绑定挂载的源路径时,应该提供文件的完整路径,包括文件名本身。因此,在路径变量中包含文件名(“notes.sql”)是正确的。

lpwwtiir

lpwwtiir4#

已解决

C:/Users/%my_username%/中创建了一个文件.testcontainers.properties,并在那里写道:ryuk.disabled=true

相关问题