Create a Docker container from an image without starting it

As part of my deployment strategy, I manage Docker containers using Upstart.

To do this, I need to extract the image from the registry and create a named container (as suggested in the Upstart script to run the container will not control the life cycle )

Is there a way to create a container without first launching the image? I do not want to start the container (which can introduce side effects), stop it, and then manage it elsewhere.

For example, something like:

docker.io create -e ENV1=a -e ENV2=b -p 80:80 --name my_first_container sample/containe 
+6
source share
2 answers

You can achieve this using the Docker Remote API .

First of all, configure how the docker daemon works. Configure it to listen for 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" 

Now you can use the endpoint /containers/create to create the container without starting 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" } ' 

Note the ?name=my_first_container added to the curl request url. This is what you call your container.

Side note . The same thing can be done without adding an HTTP interface, however, it seems easier to show the solution using a simple POST request request.

+3
source

In case anyone else comes across this question, now this can be done using the docker create . See https://docs.docker.com/engine/reference/commandline/create/

+11
source

Source: https://habr.com/ru/post/972605/


All Articles