程序开发 · 2024年10月7日

testcontainers-go 中的卷

当前位置: > > > > testcontainers-go 中的卷

来源:stackoverflow
2024-04-20 20:06:39
0浏览
收藏

有志者,事竟成!如果你在学习Golang,那么本文《testcontainers-go 中的卷》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我有一个 docker-compose 文件,我正在尝试使用 testcontainers-go 重新创建该文件:

version: '3'
services:
  node1:
    image: "osixia/openldap:1.3.0"
    command: ['--copy-service', '--loglevel=debug']
    environment:
      - ldap_organisation=test
      - ldap_domain=test.com
      - ldap_base_dn=dc=test,dc=com
      - ldap_tls=false
    ports:
      - "3898:389"
    volumes:
      - "/path/to/testdata/node1.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/node1.ldif"

下面是执行代码:

ldapport, err := nat.newport("tcp", "389")
if err != nil {
    panic(err)
}

ctx := context.background()
req := testcontainers.containerrequest{
    image:        imagename,
    exposedports: []string{ldapport.port() + "/" + ldapport.proto()},
    env: map[string]string{
        "ldap_organisation": "test",
        "ldap_domain": "test.com",
        "ldap_base_dn": "dc=test,dc=com",
        "ldap_tls": "false",
    },

    bindmounts: map[string]string{
        "/path/to/testdata/node1.ldif":
            "/container/service/slapd/assets/config/bootstrap/ldif/custom/node.ldif",
    },
    waitingfor:   wait.forlog("slapd starting"),

}

ldapc, err := testcontainers.genericcontainer(ctx, testcontainers.genericcontainerrequest{
    containerrequest: req,
    started:          true,
})
if err != nil {
    panic(err)
}
defer ldapc.terminate(ctx)

docker-compose 文件工作正常,但是当我尝试使用 go 运行容器时,容器崩溃,其日志包含以下内容:

sed: cannot rename /container/service/slapd/assets/config/bootstrap/ldif/custom/sedah0ove: Device or resource busy

我不确定 go 代码和 docker-compose 声明之间有什么区别。

解决方案

答案实际上就在问题中,在本例中:

Cmd:          []string{"--copy-service"}

需要添加到testcontainers.containerrequest

来自 文档:

由于启动脚本会修改 ldif 文件,因此如果您不想覆盖它们,则必须向入口点添加 –copy-service 参数。

我将其添加到我的 docker-compose 文件中,但在 go 代码中忘记了它。

到这里,我们也就讲完了《testcontainers-go 中的卷》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注公众号,带你了解更多关于的知识点!