从图像创建Docker容器,而不启动它 [英] Create Docker container from image without starting it
问题描述
作为我的部署策略的一部分,我正在使用Upstart管理Docker容器。
As part of my deployment strategy, I am managing Docker containers with Upstart.
为了做到这一点,我需要从注册表中提取图像,并创建一个名为容器(如启动运行容器的启动脚本 t管理生命周期)
To do that, I need to pull an image from a registry and create a named container (as suggested on Upstart script to run container won't manage lifecycle )
有没有办法创建容器而不先运行映像?我不想要启动一个容器(可能会引起副作用),停止它,然后在其他地方管理。
Is there a way to create the container without first running the image? I don't want to have to start a container (which may introduce side effects), stop it, and then manage elsewhere.
例如: p>
For example, something like:
docker.io create -e ENV1=a -e ENV2=b -p 80:80 --name my_first_container sample/containe
推荐答案
您可以使用 Docker Remote API 。
首先调整docker守护进程在跑。配置它以监听端口4243上的HTTP请求,除了默认unix套接字:
First of all adjust how docker daemon is running. Configure it to listen to HTTP requests on port 4243 in addition to the default unix socket:
sudo sh -c "echo 'DOCKER_OPTS=\"-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock\"' > /etc/default/docker"
现在,您可以使用 / containers / create
端点创建容器而不运行它:
Now, you can use the /containers/create
endpoint to create a container without running it:
curl -X POST -H "Content-Type: application/json" http://localhost:4243/containers/create?name=my_first_container -d '
{
"Name": "dtest2",
"AttachStdin": "false",
"AttachStdout": "false",
"AttachStderr": "false",
"Tty": "false",
"OpenStdin": "false",
"StdinOnce": "false",
"Cmd":["/bin/bash", "-c", "echo Starting;sleep 20;echo Stopping"],
"Image": "ubuntu",
"DisableNetwork": "false"
}
'
注意? name = my_first_container
参数我添加到curl请求url。这是您的容器名称。
Pay attention to the ?name=my_first_container
parameter I added to the curl request url. This is how you name your container.
侧记 - 不需要添加HTTP界面就可以实现,但是看起来更容易显示该解决方案使用简单的卷曲POST请求。
Side note - The same can be achieved without adding the HTTP interface, however it seems easier to show the solution using a simple curl POST request.
这篇关于从图像创建Docker容器,而不启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!