如何在 Docker 测试容器内添加运行参数

huangapple go评论91阅读模式
英文:

How to add run arguments within docker test containers

问题

针对我正在尝试在测试容器中运行的特定图像,需要在构建后像这样启动容器:

docker run \
  -v ~/volume:/tmp/volume \
  --cap-add SYS_NICE --cap-add SYS_RESOURCE --cap-add SYS_PTRACE \
  docker-image

然而,我想知道如何将运行时选项 --cap-add SYS_NICE --cap-add SYS_RESOURCE --cap-add SYS_PTRACE 部分添加到 Dockerfile 中并构建自己的本地副本,或者使用 TestContainer 方法,或者是否可以通过配置 Docker 守护程序来实现这一点?我以为 Container.withCommand() 会这样做,但似乎会覆盖启动命令,据我所知,并不会添加这些运行参数。

英文:

For the particular image I am trying to run with test containers, it is required to start the container like so after building:

docker run \
  -v ~/volume:/tmp/volume\
  --cap-add SYS_NICE --cap-add SYS_RESOURCE --cap-add SYS_PTRACE\
  docker-image

However i'm wondering how I go about adding the runtime options --cap-add SYS_NICE --cap-add SYS_RESOURCE --cap-add SYS_PTRACE portion to either the Dockerfile and build my own local copy or use the TestContainer methods or would this even be achieved from configuring the docker daemon? I thought Container.withCommand() did this but seems to overwrite the starting command, not adding these run arguments as far as i am aware.

答案1

得分: 1

你可以使用GenericContainer#withCreateContainerCmdModifier(...),以下是完整示例:

new GenericContainer<>(
            DOCKER_HOST_CONTAINER_NAME
        ).withCreateContainerCmdModifier(
            it -> it.withHostConfig(
                HostConfig.newHostConfig()
                    .withCapAdd(Capability.NET_ADMIN, Capability.NET_RAW)
                    .withNetworkMode(network.getId())
            )
        ).withNetwork(network)
            .withNetworkAliases(dockerHostName)
            .waitingFor(
                Wait.forLogMessage(".*Forwarding ports.*", 1)
            )
英文:

You can make use of GenericContainer#withCreateContainerCmdModifier(...), here is the full example:

new GenericContainer&lt;&gt;(
            DOCKER_HOST_CONTAINER_NAME
        ).withCreateContainerCmdModifier(
            it -&gt; it.withHostConfig(
                HostConfig.newHostConfig()
                    .withCapAdd(Capability.NET_ADMIN, Capability.NET_RAW)
                    .withNetworkMode(network.getId())
            )
        ).withNetwork(network)
            .withNetworkAliases(dockerHostName)
            .waitingFor(
                Wait.forLogMessage(&quot;.*Forwarding ports.*&quot;, 1)
            )

huangapple
  • 本文由 发表于 2020年8月21日 04:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63512635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定