Docker inspect format retrieve port mapping

I’d like to retrieve the port mapped to a container using docker inspect, I found something like that:

docker inspect --format=" {{ .NetworkSettings.Ports }} " containerid

Output:

  • How to have “RUN” command in docker-compose similar to dockerfile?
  • How to set host in nginx configuration running inside docker?
  • Minikube: how to ssh into the VM?
  • Dockerizing PostgreSQL - psql Connection refused
  • docker cassandra with opscenter unable connected
  • Make docker exit during dockerfile if errors in webpack
  • map[1234/tcp:[map[HostIp:0.0.0.0 HostPort:49159]] 3306/tcp:<nil> 4444/tcp:<nil> 4567/tcp:<nil> 4568/tcp:<nil>]
    

    But I’d like to have an output like this:

    1234/tcp:49159
    

    Is it possible ?

  • How do I expose Docker a Docker port via Kubernetes in Google Cloud?
  • DOCKER: cannot pull from my private registry
  • How to pass environment variable values into files in the docker container I'm trying to run
  • Docker 1.13.0 volume mapping file corruption
  • Changing permissions of added file to a Docker volume
  • Starting fig process with supervisor
  • 3 Solutions collect form web for “Docker inspect format retrieve port mapping”

    See also my answer How to get ENV variable when doing Docker Inspect I guess you can adapt and get directly what you want

    The docker port command may be more useful; it produces output like this:

    $ docker port 0a7b4df54966
    443/tcp -> 0.0.0.0:4430
    80/tcp -> 0.0.0.0:8888
    

    You can use index in your template:

    --format '1234/tcp:{{ (index (index .NetworkSettings.Ports "1234/tcp") 0).HostPort }}'
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.