Docker remote API port bindings example

This is an example of using the remote API to create and run a container with port bindings. This is the equivalent action through the docker CLI:

$ docker run -p host_ip:host_port:container_port image_name

The CLI’s run command corresponds to multiple remote API commands. When using the remote API you need to specify ExposedPorts when creating the container and PortBindings when starting it. (see here for more details)

Specifying ExposedPorts when creating containers:

POST /containers/create
{
"Image": image_id,
"ExposedPorts": {
"container_port/tcp": {}
}
}

Specifying PortBindings when starting containers:

POST /containers/(id)/start
{
"id": id,
"PortBindings": {
"container_port/tcp": [
{
"HostIp": "host_ip", // Strings, not numbers here
"HostPort": "host_port"
}
]
}
}

Written by

Senior Software Development Engineer @ Amazon. Trumpet player, drum corps enthusiast.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store