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.
source share