Bobcares

Docker 5432 bind address already in use | Resolved

by | Dec 28, 2022

Are you struggling with the docker 5432 bind address already in use error message? Our Docker Support team is here to lend a hand with your queries and issues.

How to resolve docker 5432 bind address already in use?

Typically, the issue “address already in use” occurs when we try to expose a container port that is already acquired on the host machine.

To resolve the issue, first, we need to reproduce the problem.

Assume that port 8080 on the Docker host machine is already occupied.

There are multiple reasons for this, like the Tomcat server has occupied this port or some random process is using it.

Let’s now try to run a Docker container and expose the same port 8080 on the host:

$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung
-p 8080:5432 -v /data:/var/lib/postgresql/data --name postgresql-baeldung postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
42c077c10790: Already exists
3c2843bc3122: Pull complete
...
ad029fbc8984: Pull complete
Digest: sha256:2d1e636f07781d4799b3f2edbff78a0a5494f24c4512cb56a83ebfd0e04ec074
Status: Downloaded newer image for postgres:latest
22e97efd420eee8ba2d77a956933d00b04784b0b04c97ffc09e127f10932dea9
docker: Error response from daemon: driver failed programming external connectivity
on endpoint postgresql-baeldung (154f66d3ba8bee4dafba092137311c43b950c31884e199d34d2b1d301496f9b5):
Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use.

In the above output, port 8080 has already been acquired, so we cannot run the Docker container.

Since the container failed to start, we must remove it as well.

Let’s take a look at the command to remove the Docker container:

$ docker rm -f postgresql-baeldung
postgresql-baeldung

Here, we have successfully removed the “postgresql-baeldung” Docker container.

Solving the Bind Address Issue

Today, let us see the methods followed by our support techs to resolve it.

1.Killing the Process

The Error “address already in use” occurred because some process was already running on the same port.

So we can resolve the issue just by killing the process.

To stop the process, we need the process ID (PID), which we can fetch using the lsof command.

The lsof command lets us know which processes open which files, so we can use this command to know the PID of the process running on a particular port.

By default, the lsof package is not installed in Linux.

Let’s check out the command to install the lsof:

$ yum install -y lsof

Using the above command, we’ve successfully installed the lsof package.

Let’s look into the command to get the PID of the process using the lsof command:

$ lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 78812 root 66u IPv6 0xe28ed26f1f48e597 0t0 TCP *:http-alt (LISTEN)

Here, in the output of the above command, we can see that a Java process is already running on the 8080 port, causing the “Error bind: address already in use” issue.

To resolve the issue, we need to kill this process using the kill command:

$ kill 78812

Now, we can run our container without any issues:

$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung
-p 8080:5432 -v /data:/var/lib/postgresql/data --name postgresql-baeldung postgres
154f66d3ba8bee4dafba092137311c43b950c31884e199d34d2b1d301496f9b5

This time, the Docker container ran successfully without any port conflict.

2.Expose Free Port

We can also resolve the above issue by exposing a free port while running the Docker container.

We can find open ports in Linux using the ss or netstat command:

$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung -p 8082:5432 -v /data:/var/lib/postgresql/data --name postgresql-baeldung postgres
154f66d3ba8bee4dafba092137311c43b950c31884e199d34d2b1d301496f9b5

Here, we have to make sure that we should remove the stopped container “postgresql-baeldung” before running the fresh container.

Similar Port Binding Issue

In Docker, we can also face another issue similar to the “Error bind: address already in use“.

In this issue we get the following error:

“Bind for 0.0.0.0:8080 failed: port is already allocated.”

The “port is already allocated” issue occurs if we run two different Docker containers on the same port.

Let’s run the “postgresql-baeldung” container on 8080 port:

$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung -p 8080:5432 -v /data:/var/lib/postgresql/data --name postgresql-baeldung postgres
5bddeacbe97229e4ee9cf2ed571fea309651f6cf6fecf433f22255e8cc506277
docker: Error response from daemon: driver failed programming external connectivity
on endpoint postgresql-baeldung (b3469040c1fe37509c792e206e68d02ccc95a51b3353809b9462d15f6d670cc9):
Bind for 0.0.0.0:8080 failed: port is already allocated.

The solution to the “Bind for 0.0.0.0:8080 failed: port is already allocated” issue is similar to the one discussed above.

All we need to do is either free up the port or use some other available port.

One of the key differences between both the issues is that the issue “address already in use” occurs when we run a Docker container on a port on which a process is already running on the host.

While the “port is already allocated” issue occurs when we try to run a Docker container with the same port on which a Docker container is already running.

Conclusion

To conclude, our Support Engineers demonstrated the cause behind the docker 5432 bind address already in use error and how to resolve it easily.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF