如何使用TestContainers在容器中执行cmd命令?

How to execute cmd command in the container up with TestContainers?

提问人:gstackoverflow 提问时间:10/10/2023 更新时间:10/10/2023 访问量:56

问:

我可以从cmd执行命令(在以testcontainers启动的容器内)。我想从testcontainers api执行相同的命令

/ # LDB_MODULES_PATH=/usr/lib/samba/ldb/ ldbmodify -H /var/lib/samba/private/sam.ldb /tmp/test.ldif 
Modified 0 records successfully

enter image description here

选项1:

sambaContainer.execInContainer(
            "LDB_MODULES_PATH=/usr/lib/samba/ldb/",
            "ldbmodify",
            "-H",
           "/var/lib/samba/private/sam.ldb",
            "/tmp/test.ldif",
        )

我收到错误:

OCI runtime exec failed: exec failed: unable to start container process: exec: "LDB_MODULES_PATH=/usr/lib/samba/ldb/": stat LDB_MODULES_PATH=/usr/lib/samba/ldb/: no such file or directory: unknown

我尝试了 2 个选项:

选项 2:

sambaContainer
            .withEnv("LDB_MODULES_PATH","/usr/lib/samba/ldb/" )
            .execInContainer(
            "ldbmodify",
            "-H",
           "/var/lib/samba/private/sam.ldb",
            "/tmp/test.ldif",
        )

我收到错误:

WARNING: Module [samba_dsdb] not found - do you need to set LDB_MODULES_PATH?
Unable to load modules for /var/lib/samba/private/sam.ldb: (null)
Failed to connect to /var/lib/samba/private/sam.ldb - (null)

第二个选项的结果与执行的结果相同

ldbmodify -H /var/lib/samba/private/sam.ldb /tmp/test.ldif

从 CMD:

enter image description here

如何从testContainers执行命令?

java bash cmd samba testcontainers

评论


答:

0赞 gstackoverflow 10/10/2023 #1

这工作原理:

val sambaContainer = GenericContainer(SAMBA_IMAGE)
                     ...
                    .withEnv("LDB_MODULES_PATH","/usr/lib/samba/ldb/")
sambaContainer.start()
sambaContainer.execInContainer(
                "ldbmodify",
                "-H",
               "/var/lib/samba/private/sam.ldb",
                "/tmp/test.ldif",
            )