使用选项/参数覆盖从主机运行的默认docker
因此,在构建使用选项/参数覆盖从主机运行的默认docker,docker,Docker,因此,在构建docker build-t hello之后。我可以通过调用docker run hello来运行hello,并获得输出hello world 现在让我们假设我希望运行ls或sh——这很好。但我真正想要的是能够传递参数。e、 g.ls-al,甚至tail-f/dev/null,以保持容器运行而无需更改Dockerfile 我该怎么做呢?我尝试exec模式失败得很惨docker run hello--cmd=[“ls”,“-al”]docker run命令中图像名称之后的任何内容都将成
docker build-t hello之后。
我可以通过调用docker run hello
来运行hello,并获得输出hello world
现在让我们假设我希望运行ls
或sh
——这很好。但我真正想要的是能够传递参数。e、 g.ls-al
,甚至tail-f/dev/null
,以保持容器运行而无需更改Dockerfile
我该怎么做呢?我尝试exec模式失败得很惨
docker run hello--cmd=[“ls”,“-al”]
docker run命令中图像名称之后的任何内容都将成为cmd
的新值。因此,您可以运行:
FROM alpine:3.5
CMD ["echo", "hello world"]
请注意,如果定义了ENTRYPOINT
,则ENTRYPOINT
将作为参数接收CMD
的值,而不是直接运行CMD
。因此,您可以使用以下内容将入口点定义为shell脚本:
docker run hello ls -al
但我真正想要的是能够传递参数。e、 g.ls-al,甚至tail-f/dev/null来保持容器运行,而不必更改Dockerfile
这是通过以下方式实现的:
#!/bin/sh
echo "running the entrypoint code"
# if no args are passed, default to a /bin/sh shell
if [ $# -eq 0 ]; then
set -- /bin/sh
fi
# run the "CMD" with exec to replace the pid 1 of this shell script
exec "$@"
因此,容器在后台运行,它允许您在其中执行任意命令:
docker run -d hello tail -f /dev/null
docker exec ls-la
例如,外壳:
docker exec <container-id> ls -la
docker exec-it bash
另外,我向您推荐@BMitch所说的内容
docker exec -it <container-id> bash