(Day 14) Task : Docker Port Expose :-

Aditya SharmaAditya Sharma
3 min read
  • Exposing and publishing ports in Docker is essential for enabling communication between your containerized applications and the external world. This guide breaks down the concepts and provides simple, step-by-step instructions to help you manage Docker ports effectively.

  • In this session we’ll learn how will the user access the content of a container through internet.

  • Container doesn’t have any ip and to access anything using internet ip is required.

  • Internet Access Port : 80.

  • Since , there is a mapping b/w ec2 instance & container when a user runs that instance using public ip in its device remotely then it’ll go to the ec2 instance & since mapping is there user will be able to see content of container that may be website or anything.

Understanding Docker Ports

  • EXPOSE: A Dockerfile instruction that documents which ports the container listens on. It doesn't publish the port to the host machine.

  • -p / --publish: A Docker run command option that maps a container's port to a port on the host, making it accessible externally.

  • -P / --publish-all: Automatically maps all exposed ports to random ports on the host.

How to Expose and Publish Ports

Difference between docker attach and docker exec :-

Docker attach :

  • Functionality: Attaches your terminal to the container's main process, allowing you to interact as if you were directly connected to it.

  • Behavior:

    • Input and output are shared; what you type is sent to the container, and its output is displayed in your terminal.

    • If the container was started with -i (interactive), your inputs are directed to its stdin.

    • Exiting the attach session (e.g., via CTRL+C) can stop the container if the main process terminates as a result.

Docker exec :

  • Functionality: Executes a new command in a running container, creating a separate process from the main application.

  • Behavior:

    • Each exec session is independent, allowing multiple concurrent sessions.

    • Does not interfere with the main process; exiting the exec session leaves the container running.

    • Supports running commands as different users within the container.

Difference between Expose and Publish(-p) :-

Basically we have Four Options:

  1. Neither Specify Expose nor publish(-p) :

    The service in the container will only be accessible from inside container only.

  2. Only Specify Expose :

    In this , Service won’t be accessible from outside Docker , but from inside other Docker Containers Can.

  3. Specify Expose and publish(-p):

    The Service can be accessible from anywhere , even outside Docker.

  4. Only Specify publish(-p) :

    Docker does an implicit expose. This is because , if a port is open to public , it’s also open to other docker containers hence , -p includes expose.

0
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS