Solution to Docker port is already allocated and address already in use error
In Docker port conflict, find the container or process using the port; Change the host port and verify the Compose mapping correctly.
Seen when starting a Docker containerport is already allocatedor bind: address already in usemessage indicates that Docker was unable to allocate a combination of IP and port on the host. Even if the application in the container is healthy, the port selected for the external connection may be used by another process.
The solution is to identify the process using the port and stop that process or change the host port of the new container as needed. It is okay if the internal port of two containers is 80; The conflict occurs on the port trying to broadcast on the same host address.
1. Separate the host port and container port
docker run -p 8080:80 nginx
In this example the left8080is the host port; on the right80is the container port. Run a second application on the same host8081:80You can publish it with . The two containers use separate ports on the host side while listening to port 80 in different network areas.
The IP address is also part of the binding behavior. Representing all interfaces0.0.0.0binding may conflict with usage on a particular interface. Read together the IP and port shown in the error message; Just changing the name of the container does not solve the problem.
2. Find the Docker container using the port
docker ps --format "table {{.Names}}\t{{.Ports}}"
docker ps --filter publish=8080
Your first commandPORTSon the host side in the column:8080->Search for its match. The Publish filter is an additional narrowing tool; Verify the actual IP/host port mapping again in the PORTS column. Different Compose projects may also request the same host port.
If the container you found is an old development instance that is not being used,docker stop you can run it. Don't stop a still-needed service just to free up a port; It may be more appropriate to change the host port of the new service. Stopped containers normally do not listen on the port; It is not necessary to delete all containers.
3. Check the process outside Docker
Listening port and process ID in Windows PowerShell:
Get-NetTCPConnection -LocalPort 8080 -State Listen |
Select-Object LocalAddress, LocalPort, OwningProcess
Get-Process -Id
On Linux:
sudo ss -ltnp 'sport = :8080'
On macOS:
lsof -nP -iTCP:8080 -sTCP:LISTEN
Once you know the identity of the process, close it through the application or service it belongs to. For example, use the normal stop operation on a local development server terminal. Forcibly terminating unknown PIDs may result in loss of data or service. Note that TCP is checked in the commands; If the error is related to UDP broadcast, choose a tool suitable for the protocol.
4. Change only host port in Compose
In the development example below, the application still uses port 80 within the container. The host side is moved to 8081 and connected to localhost for access from the local computer only:
services:
web:
image: nginx:stable
ports:
- "127.0.0.1:8081:80"
docker compose config
docker compose up -d web
docker compose ps
The config command shows the combined Compose configuration. Override files or environment variables can add another port. In production, fix the image version according to your project policy; The label here is for the example. If external access is required, plan the interface to be connected separately.
5. Verify the new port on the application side as well
In browserhttp://localhost:8081Open the address. If containers connect to each other over the same Compose network, they generally use the service name and container port; for exampleweb:80. Changing the host port does not automatically change this internal address.
If you are getting an access permission or reserved port error in Windows even though the listening process is not visible, check if the error text is actuallyaddress already in useCheck whether . Reserved port ranges and access barriers are a different situation. Consider restarting Docker Desktop after configuration and process control.
Short guide
Common mistake
docker system pruneor using volume deletions as the first solution to the port error. Correcting the port mapping is sufficient in most cases; Deleting a volume may affect permanent data.