MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Docker Tutorials: Working with Ports in Docker Containers

ScmGalaxy banner reading Working with Ports in Docker Containers, with the Docker whale

Port expose and publish has to happen when a container is created. Just stop the existing container and create a new one in its place with the added expose and/or publish options.

By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers. Each outgoing connection will appear to originate from one of the host machine’s own IP addresses thanks to an iptables masquerading rule on the host machine that the Docker server creates when it starts:

[code]$ sudo iptables -t nat -L -n
…
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all — 172.17.0.0/16 0.0.0.0/0[/code]

The Docker server creates a masquerade rule that lets containers connect to IP addresses in the outside world. If you want containers to accept incoming connections, you will need to provide special options when invoking docker run. There are two approaches.

How to map ports to containers?
Approach 1
First, you can supply -P or –publish-all=true|false to docker run
or
EXPOSE line in the image’s Dockerfile
or
–expose <port> commandline flag and maps it to a host port somewhere within an ephemeral port range.

Approach 2
Mapping can be specified explicitly using -p SPEC or –publish=SPEC option. It allows you to particularize which port on docker server – which can be any port at all, not just one within the ephemeral port range – you want mapped to which port in the container.

How to EXPOSE Port on running container?

Mehtod 1 – Using docker commit
Commit your current container to a new image and then do a docker run specifying the new port range and the new image name.

[code]$ docker stop containerID
$ docker commit containerID newImageName:tag
$ docker run -d –name db -p 8091-8094:8091-8094 -p 11210:11210 newImageName:tag[/code]

Method 2 – using iptables

[code]HOST> iptables -t nat -A DOCKER -p tcp –dport 443 -j DNAT –to-destination 172.17.0.2:443
HOST> iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp –source 172.17.0.2 –destination 172.17.0.2 –dport https
HOST> iptables -A DOCKER -j ACCEPT -p tcp –destination 172.17.0.2 –dport https[/code]

Docker Tutorials Fundamental To Advanced-2021 Crash Course:- https://bit.ly/3hOIbTB

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Docker Lab, Excercise & Assignment – 2

Part 1: How to Access the Docker Container Accessing containers is a fundamental skill. Whether you want to debug, update configuration, or inspect running services, you’ll need…

Read More

Docker commands Guide – docker network with examples

Here’s a complete tutorial on docker network, covering what it does, examples, and use cases. What is docker network? docker network is a command used to manage…

Read More

Docker commands Guide – docker port with examples

Here’s a complete tutorial on the docker port command, including how it works, use cases, and a comprehensive list of examples. What is docker port? docker port…

Read More

Docker Tutorials: Remove all Docker Containers, all Images, all Volumes, and all Networks

How to stop all Docker Containers To delete all containers including its volumes use How to remove all stopped Docker Containers How to delete all Docker Images…

Read More

Docker Lab, Excercise & Assignment – 6 – Docker Linking

Required Images. Run the mySql Docker Container Run the wordpress Docker Container Run the following commands and what kinds of environment variables its get exposed to wordpress…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x