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:

  • Got an error when running rake db:create in Docker
  • Convert Docker image to Google Compute Engine VM image
  • Rebuilding docker image is failing although using the same Dockerfile as shown in the hub
  • understanding parameters passed to -v in Docker
  • Bash - seamlessly run scripts with CRLF line endings
  • Access a webserver inside a docker container from outside
  • 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 can we make volume as optional in docker compose .yml , how can we mount volume as optional using flag
  • How to prevent docker-compose building the same image multiple times?
  • Will docker cost system resources to exchange data with system?
  • Why the image that built by myself dont run on the kubernetes,and the pod always restart?
  • Docker - sh service script dont take options
  • How do I create a PID trap for multiple commands separated by &&
  • 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.