Last Updated: February 25, 2016
·
722
· yitsushi

Get IP + port from boo2docker

If you use boot2docker you met the problem with port numbers too :) I'm on Mac and it's a huge issue for me to find my needed port number of a specified container.

Here is a script to get it as an URL (good for binding webservers).

#!/bin/bash

if [[ -z $2 ]]; then
  echo "Usage: $0 <conatiner> <port>"
  exit;
fi

echo `boot2docker ip 2>/dev/null`:`docker port $1 $2 | sed -e 's/.*://g'`

It has echo but my real script contains open like:

#!/bin/bash

if [[ -z $2 ]]; then
  echo "Usage: $0 <conatiner> <port>"
  exit;
fi

open "http://"`boot2docker ip 2>/dev/null`:`docker port $1 $2 | sed -e 's/.*://g'`"/"

It's good on mac because it's open the URL automatically in my browser. I put it into my ~/.bin/dockerport and I added this path to my PATH env variable, so I can run it as a normal "program".

Most of time I give a name to my containers so I can give that name instead of the Container ID.

(clockwork) Ξ Docker/nginx-php → docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
f4bc32074e0f        ad55c90a04a3        "start"             2 days ago          Up 2 days           0.0.0.0:49154->80/tcp   experiments-container-1
(clockwork) Ξ Docker/nginx-php → dockerport f4bc32074e0f 80
192.168.59.103:49154
(clockwork) Ξ Docker/nginx-php → dockerport experiments-container-1 80
192.168.59.103:49154