Ele*_*ena 8 docker dockerfile docker-compose docker-swarm docker-machine
我找不到关于 Docker sleep 命令的作用的明确解释,有人可以解释一下吗?例如:
docker container run -d alpine sleep 1d
我在网上查了一下,但找不到简单的解释。
Zei*_*tor 10
Run Code Online (Sandbox Code Playgroud)docker container run -d --name mycontainer myimage:mytag sleep infinity
The last part after the image name  (i.e. sleep infinity) is not a docker command but a command sent to the container to override its default command (set in the Dockerfile).
An extract from the documentation (you can get it typing man sleep in your terminal, it may vary depending on the implementation)
Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days
To my surprise, the parameter infinity is not documented in my implementation but is still accepted. It's quite easy to understand: it pauses indefinitely (i.e. until the command is stopped/killed). In your own example above, it will pause for one day.
What is the usual reason to use sleep as a command to run a docker container?
A docker container will live until the command it runs finishes. This command is normally set in the Dockerfile used to build the image (in a CMD stanza) and can be overridden on the command line (as in the above examples).
A number of base images (like base OS for debian, ubuntu, centos....) will run a shell as the default command (bash or sh in general). If you try to spawn a container from that image using its default command, it will live until the shell exits.
当以交互方式运行此类图像时(即使用docker container run -it .....),它将一直运行直到您结束 shell 会话。但是,如果您想在后台启动它(即使用docker container run -d ...),它将立即退出,留下一个停止的容器。
在这种情况下,您可以通过使用一个长时间运行的命令覆盖默认命令来“伪造”一个长时间运行的服务,该命令基本上除了等待容器停止之外什么也不做。两个广泛使用的命令是sleep infinity(或适合您需要的任何时期)和tail -f /dev/null
启动这样的容器后,您可以使用它来测试您需要的任何内容。最常见的方法是对其运行交互式 shell:
# command will depend on shells available in your image
docker exec -it mycontainer [bash|sh|zsh|ash|...]
完成实验/测试后,您可以停止并回收容器
docker container stop mycontainer
docker container rm mycontainer
| 归档时间: | 
 | 
| 查看次数: | 36042 次 | 
| 最近记录: |