Skip to content

Instantly share code, notes, and snippets.

@binario200
Last active May 28, 2016 06:45
Show Gist options
  • Save binario200/356db71695e5e4c6290d5375afb4a47c to your computer and use it in GitHub Desktop.
Save binario200/356db71695e5e4c6290d5375afb4a47c to your computer and use it in GitHub Desktop.
// here will going to see how to connect containers
//but wait why don't jus try simple LINKING CONTAINERS? so lets see
// pull the no sql redis database
docker pull redis
// run a container with the database, check for parameter -d for the daemon way of run the container
docker run -d --name redis1 redis
//run other container using the redis database
// check for --link flag
// The value provided to the — link flag is sourcecontainername:containeraliasname.
docker run -it --link redis1:redis --name redisclient1 busybox
// see the entries at /etc/hots that will allow containers communicat
cat /etc/hosts
//check the conectivity
ping redis
// check how docker injected environment variables with the information of the linked container
SET
//exit the container
exit
//lets create a container with a redis client
docker run -it --link redis1:redis --name client1 redis sh
// now we will going to connect to the redis server running in the other container
redis-cli -h redis
//lets try some redis command
PING
set myvar DOCKER
get myvar
// exit or not, raise other client just for fun
redis-cli -h redis
get myvar
// thanks to https://rominirani.com/docker-tutorial-series-part-8-linking-containers-69a4e5bf50fb#.ji8zkbf88
// *** TRYING something more difficult about dockers networking ?
docker run -itd --name=networktest ubuntu
// the docker network by default is BRIDGE
// list the docker networks availables or created
docker network ls
//inspect bridge -> thi will show thet containers in the bridge network
docker network inspect bridge
// you could try , to see how to disconnect a container of the net, and inspect again the bridge network
docker network disconnect bridge networktest
/// creating your own network, -d flag tells to docker to use the bridge driver
docker network create -d bridge my-bridge-network
// inspect your newly created network
docker network inspect my-bridge-network
// create a database postgres container and attached it to our created network, and inspect the network again
docker run -d --net=my-bridge-network --name db training/postgres
// now lets raise a container with a web app in phyton to check the conectivity to the database
docker run -d --name web training/webapp python app.py
//inspect the ip adress of the web app container, keep the ip address
docker inspect web | grep -i address
// enter to the database container
docker exec -it db bash
// try to ping the web app using its ip addredd
ping ip-address
//oops it seems web app is not responding
//inspecting our created network and it seems is not in, lets connect it to the net
docker network connect my-bridge-network web
// inspect again the net or the contair, get the ip address of the web app and try the ping againg
ping ip-address ### it seems that this time it works
// you are feeling cool? try to connect containers amount different hosts
// hint: you will need to use the overlay driver and maybe an key-value storage
// a gist? yeah myabe comming soon.
// thanks to https://docs.docker.com/engine/userguide/containers/networkingcontainers/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment