✅ connecting docker container from localhost
hello, i built a backend and created a mongo in docker. and then I wrote a docker compose then I check inside of docker containers. both working fine but I can't connect from my pc. what's the problem?
110 Replies
when I try to send a get method from postman using this uri http://localhost:8000/CaseHistory/GetAllCaseHistory it returns Error: read ECONNRESET
ports
are outside:inside
meaning 27018:27017
maps 27018
on the host to 27017
inside the containerso my connection string must end with 27018
hm, well actually, since this is inside a compose that shouldn't be the case now that I think about it
I do think so because I tried both:/
Networked service-to-service communication uses the CONTAINER_PORTso yeah you were right on using 27017
so the problem is not connection string
doesnt look like it
what exactly do you mean by
both working fine but I can't connect from my pc.? like, I see the containers running.. and the way its set up, your asp app should be able to talk to mongo
I checked inside containers from docker desktop my app was working
like the container logs for the web app?
but I didn't try to communicate with mongo inside webapp container
yh it's just prints usual logs
right
well uh
Im still confused. If you have not tried yet, whats wrong? 😛
oh okay
web app container logs
look at that message
the message with 8080?
Now listening on ...:8080
you are forwarding 8000 on the host to 80 inside the container
but inside the container, your httplistener is on
8080
so change that to 8000:8080
uhh
it worked
yeah your problem wasn't mongo, it was the http listeners port setup 🙂
can I see the collections inside docker container that I used for mongo?
I just want to be sure about im using right collection and db
you have some kind of mongo client tool right?
like, for mssql we have SSMS, for postgres we have pgadmin
yes I have mongo compass
great
so connect that to
localhost:27018
assuming you are running compass on the same computer that is running docker desktopyep
it's same with my app's db right
yep
same credentials, just different hostname/port since we are now outside the container network
in compose yaml i gave db name and collection name as environement but I don't have it here so should I create manually?
sure
give it the same values
last question is its working fine but in app I also have front side like blazor but I can't connect it with localhost:8000/xpage
I get something like:
its saying 7045 in the message?
yeah so should I add another port for 7045
http://localhost:8000
should be where your webapp is externally
uh
i dont know what we are looking at in that screenshot
is it the output from http://localhost:8000
?
or whatyes.
why is your app trying to connect to
localhost:7045
?yes
normally when I run project from my pc it was working on 7045.
well yeah, but now its inside docker
should I add sth like 8081:7045
no
you should figure out why your app is trying to connect to
localhost:7045
from your docker compose, it seems the only external service your webapp uses is mongo
on 27017
but now its trying to make http calls to some web service/api on 7045
, which is unexpectedfare they seperate? blazor request and other backend request?
?
wait, you have... 3 different things?
blazor, api, mongo?
but your compose file only states two containers
blazor and api in same project. but do i making big mistake?
im confused
what kind of blazor app is this?
found it
okay thats confusing.
7045 port's coming from here should I make it 8000?
your launch profile should only change what port the httplistener starts on in debug mode
and also, note how in the screenshot from your logs your webapp listens on HTTP
not HTTPS
do you have a http redirect in your webapp setup btw?
it's default I think. I only changed compose file
what do you think
I think you should not be containerizing your app at this stage, tbh
Just makes it harder to debug and stuff, since during development you will likely be doing a lot of that
Mongo etc in docker is perfectly fine, but your own app can prob wait
yh I know but my senior is asked for me to do it somehow I managed to dockerize
but just backend side of project
I wonder why blazor is not working
I gotta make it today and it's the only thing left
Well you need to figure out how the FE and the BE talk to eachother, what protocol, what port
An easy fix is probably forwarding 7045 to 7045
like this?
Yeah
well it's not working :/
2024-07-05 17:18:23 warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
2024-07-05 17:18:23 Failed to determine the https port for redirect.
I've found this log in web app
Yeah well your service only listens on http so far
But you have Https redirect enabled
Bad combination
what do you suggest
app.UseHttpsRedirection();
what if I remove this from program.cs
yeah either that, or set up your app to also listen on https
depends on how you want to host this app down the line
what would you prefer if you have app
again, depends on hosting
still not working but this time there is no log at all
you should never ever host something on HTTP these days, but thats just at the outmost layer
for example, at work all our stuff uses HTTP... because we host inside kubernetes with a service called
traefik
that adds routing, load balancing and https
so client <-> traefik is https, then traefik <-> our backend is httpoh I see makes sense
same thing if you plan to use something like nginx as a reverse proxy
but if you actually plan to expose kestrel directly, it should be using https
I may need to get more knowledge about both to decide but I'll note it
btw do you have idea why it's not working this time? I just removed https redirection and built again
it's not responding in server side too
this is my program.cs
check the logs
I didn't see anything do you
nope seems fine
what am I missing
did I gave the port on wrong spot
try removing the 7045 port mapping, but idk
its odd that its "not responding" when you did no changes
totally
still not working
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
it's working with 27018 already
my point is blazor didn't respond
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
wdym then
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
but I can use db through api
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
I mean i can use mongodb right now
with 27017
api's are communicating with mongo
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
🤯
wdym bro
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
my backend communicates with mongo db and I can make CRUD operations right now
is it okay now
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
funny but really makes sense
I gotta check db from inside container then?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
oh they're same
so it works with 27017
from container app connects db with 27017 but im connecting mongo compass with 27018 in my pc
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
uuuuuhh I've found why blazor is not working
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
but it works
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Pobiega said this
at the top
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
same here
its specifically when using docker compose
it creates whats called a "network" of containers that can access eachother without going via the host
I'm taking connection refused error log.
but I can connect the default page with no problem
oh I found the problem
solved it
what was the problem?
and the solution
inside the containers front end and backend communicates with 8080. I thought I can make them communicate with 8000 as I use to reach backend with 8000
right. but FE runs outside the container
since its WASM on the client machine
that's right so wasm working in the container so I had to make it 8080 when try to communicate with backend
that's how I solve the problem
k
now I changed the port as 8080:8080 for avoid confusion for another users
thank you for your help
how do I mark as solved
/close