Bobcares

“Port is already allocated” error in Docker – How to fix

by | Dec 29, 2021

Wondering how to resolve “port is already allocated” error in Docker? We can help you. As part of our Server Management Services, we assist our customers with several docker queries. Today, let us see how our Support techs proceed to resolve it.

Overview
  1. What Cause “Port is already allocated” error in Docker?
  2. How to resolve Port is already allocated error in Docker?
  3. Other Troubleshooting Tips
  4. Conclusion

What Cause “Port is already allocated” error in Docker?

1. Another Docker Container Using the Same Port: We may already have another container running on the host, using the same port that the new container is trying to bind to. Docker prevents two containers from binding to the same port on the host to avoid conflicts.

2. A Process on the Host Machine Using the Port: The port might already be in use by some other process or service running on the host machine, such as a web server (Apache, Nginx), database (MySQL, PostgreSQL), or any other application.

3. Restarting or Recreating Containers Without Stopping the Previous Instance: If a container is restarted or recreated without properly stopping and removing the previous instance, the previous container may still have its ports bound.

4. Docker Network Misconfiguration: If there is a misconfiguration in Docker’s networking setup, such as multiple networks trying to use the same IP range or port binding, this could lead to port conflicts.

5. Zombie Containers: Sometimes, containers that have not been properly cleaned up or stopped can still have ports allocated, leading to this error.

How to resolve Port is already allocated error in Docker?

Typically, error might look as shown below:

ERROR: for postgres Cannot start service postgres: driver
failed programming external connectivity on endpoint test_postgres_1
Bind for 0.0.0.0:5432 failed: port is already allocated

From the above error message we can see that another container or an application listening on the port 5432 or machine. This prevents service from allocating that port. Usually, we use docker-compose ps to list all running containers and docker-compose down to stop those containers.

However, docker-compose commands only execute on containers relate to images in our docker-compose.yml files. We also need to check containers start without docker-compose. This is the process we use to stop the correct containers.

Firstly, we stop all containers start by docker-compose (or just that one container).

docker-compose down

Then, we list all running containers.

docker container ls -a
CONTAINER ID IMAGE ... PORTS NAMES
45eec0e12d63 postgres ... 0.0.0.0:5432->5432/tcp test_postgres_1

Let’s grab the CONTAINER ID and stop/remove the container.

docker stop 45eec0e12d63
docker rm 45eec0e12d63

Other Troubleshooting Tips

1. We can use netstat -lnp or lsof -i to list all active processes and their open ports.

2. Identify any processes that are using the conflicting port. If needed, terminate those processes or modify their port configurations.

3. List running Docker containers using docker ps.

4. Check if any containers are already using the required port. If so, stop or remove those containers, or reconfigure them to use a different port.

5. Inspect the Docker Compose file or docker run command for any duplicate or overlapping port mappings.

6. If conflicts are found, adjust the mappings accordingly.

7. If the port is in use by a critical system service or another application that cannot be changed, assign a different port for the Docker container instead.

[Stuck in between? We’d be glad to assist you]

Conclusion

This approach helps in systematically resolving port conflicts in Docker environments. In short, today we saw steps followed by our Support Techs resolve “Port is already allocate” error in Docker.

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

2 Comments

  1. Eran Bar

    but what if the port is being hold by an external application?

    Reply
    • Hiba Razak

      Hi Eran,
      If the port needed by a Docker container is held by an external application, either stop or reconfigure the conflicting application, change the Docker container’s port mapping, or use a different port inside the container.

      Reply

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