Skip to content

Instantly share code, notes, and snippets.

@gongo
Created October 28, 2014 13:08
Show Gist options
  • Save gongo/5c340f98ee32c3c0c229 to your computer and use it in GitHub Desktop.
Save gongo/5c340f98ee32c3c0c229 to your computer and use it in GitHub Desktop.
expose に対して forwarding されたホストのポートをコンテナの内部から取得する

Requirements

  • jq to /opt/jq on container

  • Mount docker unix socket when docker run

    $ docker run -v /var/run/docker.sock:/var/run/docker.sock -P -d container-name
    

Command

#!/bin/bash

echo -e "GET /containers/${HOSTNAME}/json HTTP/1.0\r\n" | nc -U /var/run/docker.sock | sed '1,/^\r$/d' | /opt/jq '.NetworkSettings.Ports[][0].HostPort | tonumber'

# => 49164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment