REST API container create and port bindings

eXPi picture eXPi · Dec 17, 2013 · Viewed 23.2k times · Source

I have image from dockerfile, which setup container, but I need to create and start containers with docker REST API. Problem is that I have issue with exposing ssh port. I have removed EXPOSE from dockerfile, and build image.

After that I made POST request on /containers/create with this json:

{"Image":"frantiseks/apac","ExposedPorts":{"22/tcp":{}},"Memory":600000,"CpuShares":50}

Container was successfully created so as next step I started it with this POST request /containers/$id/start with JSON:

{"PortBindings": { "22/tcp": [{ "HostPort": "11022" }] }}

But after inspect container I do not see mapped ports, so container does not exposed 22 to host 11022 port. I am using version 0.7.1.

Could someone tell me what I am doing wrong? Thanks

PS: Inspected container: http://jsonblob.com/52b01e45e4b0439bc58ec8d4

Answer

BMitch picture BMitch · Oct 16, 2016

(Apologies for the late answer, "community" decided to bump your question)

With the current 1.24 api, this is all done in the container create. You need to include the following there:

   "HostConfig": {
     "PortBindings": { "22/tcp": [{ "HostPort": "11022" }] },
   }