Search code examples
dockerdocker-for-windows

Save Dynamic port in Docker for Windows


Is there any way to save and access to the dynamic port that Docker create for each containers? We need it to be Dynamic and it is now just we don't know how to keep them as we need to use it somewhere else. Is maybe Environmental Variable a way? This is for Docker in Windows.


Solution

  • The mapped ports are available via the Docker API in the containers endpoint

    The information is exposed in the cli via:

    docker port CONTAINER
    

    The same information can be pulled from docker inspect, which allows you to format the output how you want with --format and Go templates

    docker inspect CONTAINER --format ^
      '{{ range $i, $el := .NetworkSettings.Ports }}{{(index $el 0).HostPort}}{{end}}'