C
C#2mo ago
Dachi

Docker

I am trying to start databse by docker but it fails by saying that that port is already in use "Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432: bind: address already in use" But then i run - lsof -i :5432 It shows nothing. No other docker image or container is running.
55 Replies
surf68
surf682mo ago
does whatever you're doing work when you use another port?
Dachi
DachiOP2mo ago
i am just starting to learn all of these stuff and was following a tutorial and since i had this error was curious what was the reason and what can cause it
surf68
surf682mo ago
so what command are you using to start the container?
Dachi
DachiOP2mo ago
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
surf68
surf682mo ago
can you pastebin the contents of your compose files?
JohnStoober
JohnStoober2mo ago
Show your docker-compose.yml file
Dachi
DachiOP2mo ago
do what?
JohnStoober
JohnStoober2mo ago
Ok so You know that file
Dachi
DachiOP2mo ago
version: '3.4' services: catalogdb: image: postgres catalog.api: image: catalog.api build: context: . dockerfile: Services/Catalog/Catalog.API/Dockerfile volumes: postgres_catalog:
JohnStoober
JohnStoober2mo ago
Docker-compose.yml Yes Ok do you have any other files? Maybe a Dockerfile?
Dachi
DachiOP2mo ago
this is override
c#
version: '3.4'

services:
catalogdb:
container_name: catalogdb
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=Dachi123
- POSTGRES_DB=CatalogDb
restart: always
ports:
- "5432:5432"
volumes:
- postgres_catalog:/var/lib/postgresql/data

catalog.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ConnectionStrings__Database=Server=catalogdb;Port=5432;Database=CatalogDb;User Id=postgres;Password=Dachi123;Include Error Detail=true
ports:
- "6000:8080"
- "6060:8081"
volumes:
- ./Services/Catalog/Catalog.API:/app
depends_on:
- catalogdb
c#
version: '3.4'

services:
catalogdb:
container_name: catalogdb
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=Dachi123
- POSTGRES_DB=CatalogDb
restart: always
ports:
- "5432:5432"
volumes:
- postgres_catalog:/var/lib/postgresql/data

catalog.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ConnectionStrings__Database=Server=catalogdb;Port=5432;Database=CatalogDb;User Id=postgres;Password=Dachi123;Include Error Detail=true
ports:
- "6000:8080"
- "6060:8081"
volumes:
- ./Services/Catalog/Catalog.API:/app
depends_on:
- catalogdb
this is dockerfile
c#
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Services/Catalog/Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
COPY ["BuildingBlocks/BuildingBlocks/BuildingBlocks.csproj", "BuildingBlocks/BuildingBlocks/"]
RUN dotnet restore "Services/Catalog/Catalog.API/Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Catalog.API.dll"]
c#
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Services/Catalog/Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
COPY ["BuildingBlocks/BuildingBlocks/BuildingBlocks.csproj", "BuildingBlocks/BuildingBlocks/"]
RUN dotnet restore "Services/Catalog/Catalog.API/Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Catalog.API.dll"]
JohnStoober
JohnStoober2mo ago
Ok so try running docker ps Let us know the output
Dachi
DachiOP2mo ago
its empty
JohnStoober
JohnStoober2mo ago
Ehat command are you running to start the containers?
Dachi
DachiOP2mo ago
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
JohnStoober
JohnStoober2mo ago
And you arent running anything on your actual machine thats using port 5432?
Dachi
DachiOP2mo ago
no i checked it via lsof -i :5432 and it outputed nothing that's why i was confused
JohnStoober
JohnStoober2mo ago
Hm
Dachi
DachiOP2mo ago
we can call if it is better for you
JohnStoober
JohnStoober2mo ago
That is strange Unfortunately im at work
Dachi
DachiOP2mo ago
sure i am using mac if that changes anything
JohnStoober
JohnStoober2mo ago
Oh wait How are you running this? Vm?
Dachi
DachiOP2mo ago
rider
JohnStoober
JohnStoober2mo ago
Docker desktop?
Dachi
DachiOP2mo ago
rider has built in docker executor
JohnStoober
JohnStoober2mo ago
Brb
Dachi
DachiOP2mo ago
no, now i am trying to run it thru terminal by this command docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
surf68
surf682mo ago
Does this work? docker run --rm -p 5432:80 nginx
Dachi
DachiOP2mo ago
Status: Downloaded newer image for nginx:latest
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432: bind: address already in use.
ERRO[0032] error waiting for container:
Status: Downloaded newer image for nginx:latest
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432: bind: address already in use.
ERRO[0032] error waiting for container:
surf68
surf682mo ago
alright, something on your machine is definitely already running on port 5432
Dachi
DachiOP2mo ago
yeah i guess it is jjust a waste of time. i've been debugging it for 2 days anyways thanks for the time
surf68
surf682mo ago
netstat -tuln | grep 5432 Does that highlight anything?
Dachi
DachiOP2mo ago
Dachis-MacBook-Pro:OnlineShopSystem dachilekborashvili$ netstat -tuln | grep 5432
1708425ce76e64e7 stream 0 0 170842534dd08dbf 0 0 0 /tmp/.s.PGSQL.5432
Dachis-MacBook-Pro:OnlineShopSystem dachilekborashvili$
Dachis-MacBook-Pro:OnlineShopSystem dachilekborashvili$ netstat -tuln | grep 5432
1708425ce76e64e7 stream 0 0 170842534dd08dbf 0 0 0 /tmp/.s.PGSQL.5432
Dachis-MacBook-Pro:OnlineShopSystem dachilekborashvili$
damn i hting you found it
surf68
surf682mo ago
that looks like it's a bit of a red herring
Dachi
DachiOP2mo ago
i will literally never forget that command thanks a lot ❤️
surf68
surf682mo ago
wait, you've fixed it? i think that's just a red herring. do you get any different output with lsof run as root? it could be another user has the port in use.
Dachi
DachiOP2mo ago
yeah i could fix it no i get nothing by running lsof
surf68
surf682mo ago
ok well if you've fixed it then all good 👍
Dachi
DachiOP2mo ago
my bad i could not fix it i think it is with the docker
surf68
surf682mo ago
try docker run --rm -p 17839:80 nginx i.e. some other random port that's very unlikely to be in use
Dachi
DachiOP2mo ago
is it suppose to take long?
surf68
surf682mo ago
the container should start up pretty quickly? visit http://localhost:17839 in your browser when it's started and you should see the nginx hello world
Dachi
DachiOP2mo ago
Dachis-MacBook-Pro:~ dachilekborashvili$ docker run --rm -p 17839:80 nginx docker: Error response from daemon: driver failed programming external connectivity on endpoint hopeful_gates (e777ca74d2296c695662ac9d30b4ae3d94d84b63040451a41508494bd9b27ead): Bind for 0.0.0.0:17839 failed: port is already allocated.
surf68
surf682mo ago
and you just ran that command the once?
Dachi
DachiOP2mo ago
i terminated the first one and ran again
surf68
surf682mo ago
do you get any output from docker ps?
Dachi
DachiOP2mo ago
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 62d69a2a3132 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:17839->80/tcp hopeful_galileo
surf68
surf682mo ago
ok so the container is already running and bound to that port presumably that link i sent you works on your machine and shows you the nginx hello world page?
Dachi
DachiOP2mo ago
yes
surf68
surf682mo ago
okay, so you can kill and remove that container (docker rm -f 62d69a2a3132)
Dachi
DachiOP2mo ago
done i checked its killed
surf68
surf682mo ago
so your docker install seems to be working fine, you can spin up and bind ports for containers, it's just convinced that something else is already using port 5432
Dachi
DachiOP2mo ago
so i should just use another port cuz for some reason something is running on that port
surf68
surf682mo ago
if you can't find the culprit then yeah, i'd just use another port to get you going you could always restart your machine and see whether that frees up the port too if you haven't already tried that
Dachi
DachiOP2mo ago
i just simply changed port it is running @surf68@JohnStooberthanks both of you for your time and help

Did you find this page helpful?