polonskiy / crowdr

Crowdr is a tool for managing multiple Docker containers
MIT License
95 stars 9 forks source link

TIP: easy docker bidirectional links without 'network' cmd (older docker version) #20

Closed coderofsalvation closed 8 years ago

coderofsalvation commented 8 years ago

FYI: Just wrote this hook to easily link 2 containers both ways:

get_ip(){
  docker inspect "$(crowdr_fullname $1)" | grep IPAddress | sed 's/.*: "//g;s/".*//g'
}

# usage: connect_containers servernameA servernameB
connect_containers(){
  host="$(crowdr exec $1 grep "$2" /etc/hosts)" && [[ ${#host} == 0 ]] && crowdr exec $1 sed -i 's/'$2'//g' /etc/hosts
  host="$(crowdr exec $2 grep "$1" /etc/hosts)" && [[ ${#host} == 0 ]] && crowdr exec $2 sed -i 's/'$1'//g' /etc/hosts
  echo "$(get_ip $2) $2 $(crowdr_fullname "$2")" | crowdr pipe $1 tee -a /etc/hosts
  echo "$(get_ip $1) $1 $(crowdr_fullname "$1")" | crowdr pipe $2 tee -a /etc/hosts
}

It works pretty sweet in combination with the wait_port function from the README.md example.

REASON: the docker link parameter is nice, but I couldn't find any way to setup bidirectional links. (Correct me if I'm wrong please)

byrnedo commented 8 years ago

I think that using docker networking now solves this kind of thing. Just create a network and specify it for both containers

docker network create --driver overlay my-net

docker run  ... --net my-net ....

You will end up with hosts entries for all containers in network in each container.

On 11 January 2016 at 15:32, coderofsalvation notifications@github.com wrote:

FYI: Just wrote this hook to easily link 2 containers both ways:

usage: connect_containers servernameA servernameB

connect_containers(){ host="$(crowdr exec $1 grep "$2" /etc/hosts)" [[ ${#host} == 0 ]] && { echo "$(get_ip $1) $1 $(crowdr_fullname "$1")" | crowdr pipe $2 tee /etc/hosts echo "$(get_ip $2) $2 $(crowdr_fullname "$2")" | crowdr pipe $1 tee /etc/hosts; } || echo "already connected!" }

the docker link parameter is nice, but I couldn't find any way to setup bidirectional links. (Correct me if I'm wrong please)

— Reply to this email directly or view it on GitHub https://github.com/polonskiy/crowdr/issues/20.

coderofsalvation commented 8 years ago

sweet! Unfortunately my ISP runs a particular (older version). As soon as they upgrade I'll use the 'network' cmd. I'll leave this here for other legacy docker users.