Skip to content

devops-java/docker-nginx-containers-network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

docker-nginx-containers-network

Containers Communication In A Network

Two containers can communicate with each other when they are in same network. Below are the steps to achieve the communication between containers in a network.

  1. Create and View network.
  2. Create nginx image.
  3. Run the image created in step 2 as container mynginx1 with user defined network created in step 1.
    • Verify that container is running.
  4. Run the image created in step 2 as container mynginx2 with user defined network created in step 1.
    • Verify that container is running.
  5. Access each container using curl.
  6. Verify each container attached to user defined network.
  7. Go into the container mynginx1. Ping to the mynginx2.
  8. Go into the container mynginx2. Ping to the mynginx1.
  9. Getting logs of each of the container.
  10. Remove resources.
    • Stop the both of the containers.
    • Remove the images.
    • Remove the network.

Create and View network

  • create: sudo docker network create --driver bridge my_nw
  • view: sudo docker network ls

image

Create Nginx Image

  • Download this repository. Go to the repostiroy directory. Open the terminal.

  • create image: sudo docker build -t my-nginx-image . image You will see below screen while running the above command. image Imgae create successfully image

  • view: sudo docker images image

Run Image As Container(mynginx1) By Attaching With The Network Name

  • run : sudo docker run --rm --name mynginx1 --network="my-nw" -d -p 81:80 my-nginx-image
  • view: sudo docker ps
  • mynginx1 is exposed to 81 port of the localhost.

Run Image As Container(mynginx2) By Attaching With The Network Name

  • run : sudo docker run --rm --name mynginx2 --network="my-nw" -d -p 82:80 my-nginx-image
  • view: sudo docker ps
  • mynginx2 is exposed to 82 port of the localhost. image

Access Each Container Using Curl

  • curl(mynginx1) : curl http://localhost:81 image

  • curl(mynginx2) : curl http://localhost:82 image

Verify Each Container Attached To Network

  • inspect mynginx1 container : sudo docker container inspect mynginx1 image
  • inspect mynginx2 container : sudo docker container inspect mynginx2 image

For both of the containers you can see the network name is my-nw.

Go Into Container mynginx1 and Access mynginx2

  • sudo docker exec -it mynginx1 bash image
  • Access mynginx2 from container mynginx1. See I am using container name instead container's ip.curl http://mynginx2:80 image
  • validate if ping is working or not. Ping will show the network name as my-nw. ping mynginx2 image
  • exit from the container mynginx1. exit image

image

Go Into Container mynginx2 and Access mynginx1

  • Screenshots for this use case is similar to above. So I am not attaching again.
  • sudo docker exec -it mynginx2 bash
  • Access mynginx1 from container mynginx2. See I am using container name instead container's ip.curl http://mynginx1:80
  • validate if ping is working or not. ping mynginx2
  • exit from the container mynginx1. exit

Remove Resources

  • stop containers: sudo docker stop mynginx1 mynginx2 . This will stop and remove the containers as we have started the containers with --rm flag. verify if containers are removed or not: sudo docker psand then sudo docker ps -a. image We are seeing some old containers but not the containers mynginx1 and mynginx2.

  • delete image: sudo docker rmi my-nginx-image. Verify if image is deleted or not. sudo docker images . image After delete. we are not seeing our image my-nginx-image.

  • delete network: sudo docker network rm my-nw. Verify if network is deleted or not. sudo docker network ls. After delete, we wont able to see the network my-nw. image

What Next?

Practice again from the beggining of this document.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published