How to bind docker container port with host

Total
0
Shares
bind docker container port with host

To bind a docker container port with host, use this command –

docker run -p{host_port}:{container_port} {image_name}

Let’s see this using an example.

First, we need a docker image which exposes a port. Generally the database images do expose some standard ports. Here we will install mongodb

docker pull mongo
docker pull mongo

Now we need to start the container. But before that we should know, what port is exposed by Mongodb. We can get this information from their website and also from docker hub page. According to it –

The MongoDB server in the image listens on the standard MongoDB port, 27017, so connecting via Docker networks will be the same as connecting to a remote mongodb.

https://hub.docker.com/_/mongo

The next decision we need to take is, what port on the host are we going to bind to it. If you are not using 27017 already or running multiple containers of mongodb, then you can simply use the same port. Otherwise use any which is available. Let’s use 27020 –

docker run -p27020:27017 mongo

This will bind the port and run a container –

running docker container of mongodb by binding port on 27020

Since we didn’t use -d flag (for detached mode), it gets attached to this terminal window and showing the logs. If we run in detached mode –

docker run -d -p27020:27017 mongo

Now our terminal will be free to continue the work –

running mongo docker container in detached mode by binding port on 27020

We can check the status of this container

docker ps
checking mongo container using docker ps command

You can see from this image that our container is running and it’s port 27017 is bounded to the host port 27020. All the requests coming to the host port 27020 will be addressed by this container.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
plex sonarr radarr jackett transmission docker

sonarr radarr jackett transmission docker

Table of Contents Hide PlexSonarrRadarrJackettTransmissionDocker-compose for Plex, Sonarr, Radarr, Jackett and TransmissionConclusion In this article we will see how to run docker containers for Plex, Sonarr, Radarr, Jackett and Transmission.…
View Post