Introduction To Docker Expose | How Its Works And Its Benefits

Introduction To Docker Expose |  How Its Works And Its Benefits
Techiio-author
Written by Nilima PaulFebruary 19, 2022
8 min read
Docker
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog post, We will know how its works in Docker EXPOSE.

Introduction to Docker EXPOSE:

Docker EXPOSE is a Dockerfile mandate or guidance indicated in the Dockerfile to illuminate that the picture made by this Dockerfile will tune in on this port while running a compartment. It doesn't uncover the referenced port; rather, it is only a sort of documentation that lets an individual know who runs the holder about the port that should be presented or distributed to permit correspondence to the compartment from outside. We need to utilize the '- p's choice while running the compartment to distribute the port so the application facilitated inside the holder can be gotten to remotely.

How does EXPOSE work in Docker:

As far as we might be concerned, is just utilized for documentation; there is no exceptional usefulness remembered for this guidance which implies if we do exclude this guidance in the Dockerfile, the picture made utilizing Dockerfile is as yet going to work similarly, yet indeed, assuming Docker picture is made by another person and the holder is getting made by another person then who will make the compartment will get befuddled that on which port application is tuning in.

Example of Docker EXPOSE:

Given below is the example of Docker EXPOSE:

We will create two nginx images with and without the ‘EXPOSE’ instruction mentioned in the Dockerfile and try to access the Nginx within the container, from different containers, and the host.

Step 1: First thing, we need to create two Dockerfile, we will mention the ‘EXPOSE’ instruction in the first Dockerfile, and we will create the second one without the ‘EXPOSE’ instruction.

docker file with ‘EXPOSE’ instruction:

Code 1

FROM ubuntu
RUN apt-get update && apt-get install -y Nginx
RUN apt-get install -y curl
EXPOSE 80

Dockerfile without ‘EXPOSE’ instruction:

Code 2

FROM ubuntu
RUN apt-get update && apt-get install -y Nginx
RUN apt-get install -y curl

Step 2: Let’s build Docker images using the above two Dockerfile.

Code 3

docker build -t Nginx: expose.
docker build –t Nginx:no_expose.
docker image ls

Step 3: Now, start a container using the ‘nginx: expose’ image, connect to it, and access the default page of Nginx.

Code 4

docker run -d nginx:expose
docker exec -it <container_ID> sh
#curl http://locahost:80

In the above snapshot, we can see that the Nginx default page is accessible.

Step 4: Let’s check the IP address of this container so that we can connect this from a different container.

Code 5

#ip addr

Step 5: Let’s create another container that has curl already installed and access the Nginx default page using the IP address of the Nginx: expose container.

Code 6

docker run -d  ubuntu:curl sleep 3600
docker exec -it <container_id> sh
#curl http://<IP_Address>:80

Step 6: Finally, let’s try to access it from the host.

Code 7

curl http://localhost:80

Step 7: So, we are unable to access the Nginx default page because the port has not been exposed while running the container in step 3. Let’s run another container with the same image ‘nginx: expose’ but expose port 80 this time.

Code 8

docker run -d  -p 80:80 nginx:expose

In the above snapshot, we can see that we can access the Nginx default page from the host on port 80 as well.

Step 8: Let’s create a new container using the ‘nginx:no_expose’ image and expose port 8080 on the host as we have already used port 80.

Code 9

docker run -d -p 8080:80 nginx:no_expose
docker exec -it <container_ID> sh
#curl http://locahost:80

In the above snapshot, we can access the default page from inside the container.

Step 9: Let’s check the container’s IP address and access it from a different container that we have used above.

Code 10

#ip addr

Code 11

docker exec -it <container_ID> sh

In the above snapshot, we can see that the Nginx default is accessible from the different containers as well.

Step 10: Finally, access it from the host, but this time we have to use port 8080.

Code 12

curl http://localhost:8080

Benefits of Docker EXPOSE:

Given underneath are the benefits referenced:

  • It is ha helpful to guide to archive the ports in the Dockerfile itself, on which that application will tune in assuming a holder is got made utilizing that picture.
  • It explains the disarray that could happen between who has made the Docker picture and who will run the compartment utilizing that picture.

Conclusion:

Adequately sure, the above situation has explained that the 'Uncover' guidance is just utilized for documentation in the Dockerfile as we have seen that the two compartments are acting similarly as far as usefulness whether we have referenced the 'Uncover' guidance or not.

Docker
Yaml
Nginx
Docker Expose
DevOps
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance