Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to obtain ip of linked container using link alias?

I have two containers one link to another. As the following...

docker run -i -t --name container1 ubuntu:trusty

docker run -i -t --name container2 --link container1:aliasc1 ubuntu:trusty /bin/bash

is there a simple way to obtain ip of container1 from container2 using link alias aliasc1?

I orchestrate container using docker-compose

like image 636
theShadow89 Avatar asked Nov 29 '25 01:11

theShadow89


2 Answers

Docker will create an entry in /etc/host for the containers linked. So you can get that ip from there. I.e.:

cat /etc/hosts | grep aliasc1 | awk {'print $1'}

like image 74
Javier Cortejoso Avatar answered Nov 30 '25 14:11

Javier Cortejoso


Docker automatically creates environment variables for this purpose. For example if container1 exposes port 80, then container2 will have $ALIASC1_PORT_80_TCP_ADDR defined.

See http://docs.docker.com/userguide/dockerlinks/#environment-variables

like image 27
Corin Avatar answered Nov 30 '25 15:11

Corin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!