C
C#5mo 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
surf685mo ago
does whatever you're doing work when you use another port?
Dachi
DachiOP5mo 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
surf685mo ago
so what command are you using to start the container?
Dachi
DachiOP5mo ago
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
surf68
surf685mo ago
can you pastebin the contents of your compose files?
Calvin
Calvin5mo ago
Show your docker-compose.yml file
Dachi
DachiOP5mo ago
do what?
Calvin
Calvin5mo ago
Ok so You know that file
Dachi
DachiOP5mo ago
version: '3.4' services: catalogdb: image: postgres catalog.api: image: catalog.api build: context: . dockerfile: Services/Catalog/Catalog.API/Dockerfile volumes: postgres_catalog:
Calvin
Calvin5mo ago
Docker-compose.yml Yes Ok do you have any other files? Maybe a Dockerfile?
Dachi
DachiOP5mo 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"]
Calvin
Calvin5mo ago
Ok so try running docker ps Let us know the output
Dachi
DachiOP5mo ago
its empty
Calvin
Calvin5mo ago
Ehat command are you running to start the containers?
Dachi
DachiOP5mo ago
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d
Calvin
Calvin5mo ago
And you arent running anything on your actual machine thats using port 5432?
Dachi
DachiOP5mo ago
no i checked it via lsof -i :5432 and it outputed nothing that's why i was confused
Calvin
Calvin5mo ago
Hm
Dachi
DachiOP5mo ago
we can call if it is better for you
Calvin
Calvin5mo ago
That is strange Unfortunately im at work
Dachi
DachiOP5mo ago
sure i am using mac if that changes anything
Calvin
Calvin5mo ago
Oh wait How are you running this? Vm?
Dachi
DachiOP5mo ago
rider
Calvin
Calvin5mo ago
Docker desktop?
Dachi
DachiOP5mo ago
rider has built in docker executor
Calvin
Calvin5mo ago
Brb
Dachi
DachiOP5mo 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
surf685mo ago
Does this work? docker run --rm -p 5432:80 nginx
Dachi
DachiOP5mo 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
surf685mo ago
alright, something on your machine is definitely already running on port 5432
Dachi
DachiOP5mo 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
surf685mo ago
netstat -tuln | grep 5432 Does that highlight anything?
Dachi
DachiOP5mo 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
surf685mo ago
that looks like it's a bit of a red herring
Dachi
DachiOP5mo ago
i will literally never forget that command thanks a lot ❤️
surf68
surf685mo 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
DachiOP5mo ago
yeah i could fix it no i get nothing by running lsof
surf68
surf685mo ago
ok well if you've fixed it then all good 👍
Dachi
DachiOP5mo ago
my bad i could not fix it i think it is with the docker
surf68
surf685mo ago
try docker run --rm -p 17839:80 nginx i.e. some other random port that's very unlikely to be in use
Dachi
DachiOP5mo ago
is it suppose to take long?
surf68
surf685mo 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
DachiOP5mo 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
surf685mo ago
and you just ran that command the once?
Dachi
DachiOP5mo ago
i terminated the first one and ran again
surf68
surf685mo ago
do you get any output from docker ps?
Dachi
DachiOP5mo 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
surf685mo 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
DachiOP5mo ago
yes
surf68
surf685mo ago
okay, so you can kill and remove that container (docker rm -f 62d69a2a3132)
Dachi
DachiOP5mo ago
done i checked its killed
surf68
surf685mo 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
DachiOP5mo ago
so i should just use another port cuz for some reason something is running on that port
surf68
surf685mo 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
DachiOP5mo 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?