英文:
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<>(
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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论