Saturday, April 18, 2020

Docker Assignments #2 : Container Linking

Docker Linking feature help to communicate containers each other and transmit data by using container name.
As mentioned in the previous post ( i.e. Default Bridge Network  ), All containers under default bridge network will be communicated only by using container IP-address

Docker Linking help to communicate containers each other by using container name
  1. Create alpine container(let us name it as 'alpine1') using default bridge network
  2. Create another alpine container (let us name it as 'alpine2')  and link 'alpine1' using docker linking
  3. Get IP-addresses of both the containers
  4. Enter into 'alpine2' container using interactivemode
  5. ping 'alpine1' container using IP-address 
  6. ping 'alpine1' container using container name
Step 1:
  By default all containers  will be created using default bridge network
     > docker run -itd --name alpine1 alpine     

Use below command to check both containers are up and running
     > docker ps

Step 2:
  Use  --link attribute for linking containers
     > docker run -itd --name alpine2 --link alpine1 alpine     

Step 3:
    use below command to get container IP-address
     > docker inspect alpine1
     > docker inspect alpine2

Step 4:
  Use below command to enter running container using interactive mode
     > docker exec -it alpine2 sh

Step 5:  
    Ping alpine1 container using IP-address
      # ping -c 2 172.17.0.4  (i.e. ping will be successful )

Step 6:  
  Ping alpine1 container using name
      # ping -c 2 alpine1  (i.e. ping will be successful )

-> Docker adds a host entry for the source container in the /etc/hosts file ( i.e. apline1 container id and name gets added in the alpine2 -  etc/hosts file)
-> By default ,  Container ENV variables will be shared across linking containers

Happy Coding :)




No comments:

Post a Comment