使用options / arguments从主机中覆盖默认的docker run

FROM alpine:3.5 CMD ["echo", "hello world"] 

所以build立docker build -t hello .后, docker build -t hello . 我可以通过调用docker run hello来运行hello,并获得输出hello world

现在让我们假设我想运行lssh – 这很好。 但我真正想要的是能够传递论据。 例如ls -al ,或者甚至tail -f /dev/null以保持容器运行而不必更改Dockerfile

我怎么去做这个? 我在exec模式下的尝试失败了… docker run hello --cmd=["ls", "-al"]

docker run命令中的图像名称之后的任何内容都将成为CMD的新值。 所以你可以运行:

 docker run hello ls -al 

请注意,如果定义了ENTRYPOINT ,则ENTRYPOINT将接收CMD的值作为参数,而不是直接运行CMD 。 所以你可以像下面这样定义一个入口点作为shell脚本:

 #!/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 "$@" 

问:但我真正想要的是能够传递论据。 例如ls -al,或者甚至t​​ail -f / dev / null,以保持容器运行而不必更改Dockerfile

这是刚刚实现的:

  docker run -d hello tail -f /dev/null 

所以容器在后台运行,它可以让你执行里面的任意命令:

 docker exec <container-id> ls -la 

而且,例如一个shell:

 docker exec -it <container-id> bash 

另外,我build议你@BMitch说。