Docker Community Forums

Share and learn in the Docker community.

Port mapping not working via API calls

Hello,

Docker version

Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:25:03 2018
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:27 2018
OS/Arch: linux/amd64
Experimental: false

I am trying to create docker containers via curl API calls. somehow I am unable to create container with port mappings.

Below is the API call I have used for container creation.

sudo curl --unix-socket /var/run/docker.sock -H “Content-Type: application/json” -d ‘{“Image”: “mohanstack/auto:1.0”,“HostConfig”:{“Binds”:[“jvol1:/var/lib/vol1”],“PortBindings”:{“8080/tcp”:[{“HostIp”: “0.0.0.0”,“HostPort”:“8754”}]}} }’ -X POST http:/v1.36/containers/create

curl --unix-socket /var/run/docker.sock -X POST http:/v1.36/containers/f3694876c289/start

docker inspect f3694876c289

[
{
“Id”: “f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43”,
“Created”: “2020-01-29T17:39:55.46555433Z”,
“Path”: “sh”,
“Args”: [
“run.sh”
],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 9741,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2020-01-29T17:40:54.04354803Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:26ab8e6473ce080d39c5ea09616fcc51b4d6d35838fe62f8d98fc880752be2d7”,
“ResolvConfPath”: “/var/lib/docker/containers/f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43/hostname”,
“HostsPath”: “/var/lib/docker/containers/f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43/hosts”,
“LogPath”: “/var/lib/docker/containers/f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43/f3694876c289de468b468ae4fbc857a919772b9b1816c708c3d4feeb0d356c43-json.log”,
“Name”: “/dreamy_roentgen”,
“RestartCount”: 0,
“Driver”: “aufs”,
“Platform”: “linux”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “docker-default”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: [
“jvol1:/var/lib/vol1”
],
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “default”,
“PortBindings”: {
“8080/tcp”: [
{
“HostIp”: “”,
“HostPort”: “7865”
}
]
},
“RestartPolicy”: {
“Name”: “”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“CapAdd”: null,
“CapDrop”: null,
“Dns”: null,
“DnsOptions”: null,
“DnsSearch”: null,
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “shareable”,
“Cgroup”: “”,
“Links”: null,
“OomScoreAdj”: 0,
“PidMode”: “”,
“Privileged”: false,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“SecurityOpt”: null,
“UTSMode”: “”,
“UsernsMode”: “”,
“ShmSize”: 67108864,
“Runtime”: “runc”,
“ConsoleSize”: [
0,
0
],
“Isolation”: “”,
“CpuShares”: 0,
“Memory”: 0,
“NanoCpus”: 0,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: null,
“BlkioDeviceReadBps”: null,
“BlkioDeviceWriteBps”: null,
“BlkioDeviceReadIOps”: null,
“BlkioDeviceWriteIOps”: null,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpuRealtimePeriod”: 0,
“CpuRealtimeRuntime”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: null,
“DeviceCgroupRules”: null,
“DiskQuota”: 0,
“KernelMemory”: 0,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: null,
“OomKillDisable”: false,
“PidsLimit”: 0,
“Ulimits”: null,
“CpuCount”: 0,
“CpuPercent”: 0,
“IOMaximumIOps”: 0,
“IOMaximumBandwidth”: 0,
“MaskedPaths”: [
“/proc/acpi”,
“/proc/kcore”,
“/proc/keys”,
“/proc/latency_stats”,
“/proc/timer_list”,
“/proc/timer_stats”,
“/proc/sched_debug”,
“/proc/scsi”,
“/sys/firmware”
],
“ReadonlyPaths”: [
“/proc/asound”,
“/proc/bus”,
“/proc/fs”,
“/proc/irq”,
“/proc/sys”,
“/proc/sysrq-trigger”
]
},
“GraphDriver”: {
“Data”: null,
“Name”: “aufs”
},
“Mounts”: [
{
“Type”: “volume”,
“Name”: “jvol1”,
“Source”: “/var/lib/docker/volumes/jvol1/_data”,
“Destination”: “/var/lib/vol1”,
“Driver”: “local”,
“Mode”: “z”,
“RW”: true,
“Propagation”: “”
}
],
“Config”: {
“Hostname”: “f3694876c289”,
“Domainname”: “”,
“User”: “root”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
],
“Cmd”: null,
“Image”: “mohanstack/auto:1.0”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: [
“sh”,
“run.sh”
],
“OnBuild”: null,
“Labels”: {
“org.label-schema.build-date”: “20191001”,
“org.label-schema.license”: “GPLv2”,
“org.label-schema.name”: “CentOS Base Image”,
“org.label-schema.schema-version”: “1.0”,
“org.label-schema.vendor”: “CentOS”
}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “f26d1bc46b0ab0b67a0770e6d164a35f76b19a878c0011d61b24cda7dfa199eb”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {},
“SandboxKey”: “/var/run/docker/netns/f26d1bc46b0a”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “bbe4c20ab13255c3392dea991f0e19861c1be474afeccc18d35673e9b44505ab”,
“Gateway”: “172.17.0.1”,
“GlobalIPv6Address”: “2001:db8:1::242:ac11:4”,
“GlobalIPv6PrefixLen”: 64,
“IPAddress”: “172.17.0.4”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “2001:db8:1::1”,
“MacAddress”: “02:42:ac:11:00:04”,
“Networks”: {
“bridge”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “f28e064284b020c7d652143933c0f52824f5fc58239727206ed484ee1b163c99”,
“EndpointID”: “bbe4c20ab13255c3392dea991f0e19861c1be474afeccc18d35673e9b44505ab”,
“Gateway”: “172.17.0.1”,
“IPAddress”: “172.17.0.4”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “2001:db8:1::1”,
“GlobalIPv6Address”: “2001:db8:1::242:ac11:4”,
“GlobalIPv6PrefixLen”: 64,
“MacAddress”: “02:42:ac:11:00:04”,
“DriverOpts”: null
}
}
}
}
]

when I used docker run command, port mapping works fine. Did I miss anything in the container creation API call?

Thanks,
Mohan

It worked fine after adding Exposed port .

curl --unix-socket /var/run/docker.sock -H “Content-Type: application/json” -d ‘{“Image”:“mohanstack/auto:1.0”,“HostConfig”:{“Binds”:[“jvol1:/var/lib/vol1”],“ExposedPorts”: {“8080/tcp”: {}},“PortBindings”:{“8080/tcp”:[{“HostIp”: “”,“HostPort”:“8754”}]}}}’ -X POST http:/v1.37/containers/create