Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker inspect format retrieve port mapping

Tags:

docker

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:

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 ?

like image 383
izissise Avatar asked Jun 04 '15 14:06

izissise


3 Answers

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
like image 124
larsks Avatar answered Oct 12 '22 20:10

larsks


You can use index in your template:

--format '1234/tcp:{{ (index (index .NetworkSettings.Ports "1234/tcp") 0).HostPort }}'
like image 45
Vincent Maillol Avatar answered Oct 12 '22 18:10

Vincent Maillol


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

like image 5
user2915097 Avatar answered Oct 12 '22 19:10

user2915097