12

I'm new to docker. I have read the tutorial in docker remote API . In aspect of creating container. It show me too many param to fill. I want to know what is equivalent to this command :

docker run -d -p 5000:5000 --restart=always --name registry registry:2.

I have no idea about it. Can anyone tell me? Thanks!

v11
  • 2,124
  • 7
  • 26
  • 54

3 Answers3

12

Original answer (July 2015):

That would be (not tested directly), as in this tutorial (provided the remote API is enabled):

First create the container:

curl -v -X POST -H "Content-Type: application/json" -d '{"Image": " registry:2.",}' http://localhost:2376/containers/create?name=registry

Then start it:

curl -v -X POST -H "Content-Type: application/json" -d '{"PortBindings": { "5000/tcp": [{ "HostPort": "5000" }] },"RestartPolicy": { "Name": "always",},}' http://localhost:2376/containers/registry/start?name=registry

Update February 2017, for docker 1.13+ see rocksteady's answer, using a similar idea but with the current engine/api/v1.26.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer,I have succeed. – v11 Jul 14 '15 at 06:49
  • I use HttpClient in java to connect api, add param that you show, it return me error about "invalid character 'I' looking for beginning of value", why? I update my question. – v11 Jul 14 '15 at 07:23
  • @v11 considering you mentioned that you had succeeded before, could you make that a new question? That will allow other to use this one for curl. It didn't mention the java API initially. – VonC Jul 14 '15 at 07:34
5

More or less just copying VonCs answer in order to update to todays version of docker (1.13) and docker remote api version (v1.26).

What is different:

  • All the configuration needs to be done when the container is created, otherwise the following error message is returned when starting the container the way VonC did. {"message":"starting container with non-empty request body was deprecated since v1.10 and removed in v1.12"}

First create the container: (including all the configuration) curl -v -X POST -H "Content-Type: application/json" -d @docker.conf http://localhost:2376/containers/create?name=registry The file docker.conf looks like this:

{
  "Image": registry:2.",
  "ExposedPorts": {
    "5000/tcp": {}
  },
  "HostConfig": {
    "PortBindings": {
      "5000/tcp": [
        {
          "HostPort": "5000"
        }
      ]
    },
    "RestartPolicy": {
      "Name": "always"
    }
    "AutoRemove": true
  }
}

Then start it: (the parameter name is not necessary, the container is just named registry) curl -v -X POST -H "Content-Type: application/json" http://localhost:2376/containers/registry/start

rocksteady
  • 2,320
  • 5
  • 24
  • 40
2

Create docker container in Docker Engine v1.24

Execute the post request -

 curl -X POST  -H "Content-Type: application/json" http://DOCKER_SERVER_HOST:DOCKER_PORT/v1.24/containers/create?name=containername

In the request body, you can specify the JSON parameters like

{
        "Hostname": "172.x.x.x",
        "Image": "docker-image-name",
        "Volumes": "",
        "Entrypoint": "",
        "Tty": true
}

It creates your docker container

Start the container

Execute the POST request

curl -X POST  http://DOCKER_SERVER_HOST:DOCKER_PORT/v1.24/containers/containername/start

Reference link - https://docs.docker.com/engine/api/v1.24/

UDIT JOSHI
  • 1,298
  • 12
  • 26