✅ 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?
No description
No description
No description
110 Replies
only you know
only you knowOP5mo ago
when I try to send a get method from postman using this uri http://localhost:8000/CaseHistory/GetAllCaseHistory it returns Error: read ECONNRESET
Pobiega
Pobiega5mo ago
ports are outside:inside meaning 27018:27017 maps 27018 on the host to 27017 inside the container
only you know
only you knowOP5mo ago
so my connection string must end with 27018
Pobiega
Pobiega5mo ago
hm, well actually, since this is inside a compose that shouldn't be the case now that I think about it
only you know
only you knowOP5mo ago
I do think so because I tried both:/
Pobiega
Pobiega5mo ago
Networked service-to-service communication uses the CONTAINER_PORT
so yeah you were right on using 27017
only you know
only you knowOP5mo ago
so the problem is not connection string
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
I checked inside containers from docker desktop my app was working
Pobiega
Pobiega5mo ago
like the container logs for the web app?
only you know
only you knowOP5mo ago
but I didn't try to communicate with mongo inside webapp container yh it's just prints usual logs
Pobiega
Pobiega5mo ago
right well uh Im still confused. If you have not tried yet, whats wrong? 😛
only you know
only you knowOP5mo ago
No description
Pobiega
Pobiega5mo ago
oh okay
only you know
only you knowOP5mo ago
web app container logs
Pobiega
Pobiega5mo ago
look at that message
only you know
only you knowOP5mo ago
the message with 8080?
Pobiega
Pobiega5mo ago
Now listening on ...:8080
Pobiega
Pobiega5mo ago
No description
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
uhh it worked
Pobiega
Pobiega5mo ago
yeah your problem wasn't mongo, it was the http listeners port setup 🙂
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
you have some kind of mongo client tool right? like, for mssql we have SSMS, for postgres we have pgadmin
only you know
only you knowOP5mo ago
yes I have mongo compass
Pobiega
Pobiega5mo ago
great so connect that to localhost:27018 assuming you are running compass on the same computer that is running docker desktop
only you know
only you knowOP5mo ago
yep
only you know
only you knowOP5mo ago
it's same with my app's db right
No description
Pobiega
Pobiega5mo ago
yep same credentials, just different hostname/port since we are now outside the container network
only you know
only you knowOP5mo ago
in compose yaml i gave db name and collection name as environement but I don't have it here so should I create manually?
Pobiega
Pobiega5mo ago
sure give it the same values
only you know
only you knowOP5mo ago
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
only you know
only you knowOP5mo ago
I get something like:
No description
Pobiega
Pobiega5mo ago
its saying 7045 in the message?
only you know
only you knowOP5mo ago
yeah so should I add another port for 7045
Pobiega
Pobiega5mo ago
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 what
only you know
only you knowOP5mo ago
yes.
Pobiega
Pobiega5mo ago
why is your app trying to connect to localhost:7045?
only you know
only you knowOP5mo ago
yes normally when I run project from my pc it was working on 7045.
Pobiega
Pobiega5mo ago
well yeah, but now its inside docker
only you know
only you knowOP5mo ago
should I add sth like 8081:7045
Pobiega
Pobiega5mo ago
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 unexpectedf
only you know
only you knowOP5mo ago
are they seperate? blazor request and other backend request?
Pobiega
Pobiega5mo ago
? wait, you have... 3 different things? blazor, api, mongo? but your compose file only states two containers
only you know
only you knowOP5mo ago
blazor and api in same project. but do i making big mistake?
Pobiega
Pobiega5mo ago
im confused what kind of blazor app is this?
only you know
only you knowOP5mo ago
found it
No description
Pobiega
Pobiega5mo ago
okay thats confusing.
only you know
only you knowOP5mo ago
7045 port's coming from here should I make it 8000?
Pobiega
Pobiega5mo ago
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?
only you know
only you knowOP5mo ago
it's default I think. I only changed compose file what do you think
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
like this?
No description
Pobiega
Pobiega5mo ago
Yeah
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
Yeah well your service only listens on http so far But you have Https redirect enabled Bad combination
only you know
only you knowOP5mo ago
what do you suggest app.UseHttpsRedirection(); what if I remove this from program.cs
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
what would you prefer if you have app
Pobiega
Pobiega5mo ago
again, depends on hosting
only you know
only you knowOP5mo ago
still not working but this time there is no log at all
Pobiega
Pobiega5mo ago
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 http
only you know
only you knowOP5mo ago
oh I see makes sense
Pobiega
Pobiega5mo ago
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
only you know
only you knowOP5mo ago
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
using ProtaTestTrack2.Controllers;
using ProtaTestTrack2.Data;
using ProtaTestTrack2.Repository;
using ProtaTestTrack2.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.Configure<MongoDBSettings>(builder.Configuration.GetSection("ProtaTestDB"));
builder.Services.AddScoped<MongoDBModel>();
builder.Services.AddScoped<FeatureService>();
builder.Services.AddScoped<CaseService>();
builder.Services.AddScoped<CaseHistoryService>();
builder.Services.AddHttpClient();




var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseAuthorization();
app.MapControllers();



app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<ProtaTestTrack2.Components.App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode();


app.Run();
using ProtaTestTrack2.Controllers;
using ProtaTestTrack2.Data;
using ProtaTestTrack2.Repository;
using ProtaTestTrack2.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.Configure<MongoDBSettings>(builder.Configuration.GetSection("ProtaTestDB"));
builder.Services.AddScoped<MongoDBModel>();
builder.Services.AddScoped<FeatureService>();
builder.Services.AddScoped<CaseService>();
builder.Services.AddScoped<CaseHistoryService>();
builder.Services.AddHttpClient();




var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseAuthorization();
app.MapControllers();



app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<ProtaTestTrack2.Components.App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode();


app.Run();
this is my program.cs
Pobiega
Pobiega5mo ago
check the logs
only you know
only you knowOP5mo ago
I didn't see anything do you
No description
Pobiega
Pobiega5mo ago
nope seems fine
only you know
only you knowOP5mo ago
what am I missing did I gave the port on wrong spot
only you know
only you knowOP5mo ago
No description
Pobiega
Pobiega5mo ago
try removing the 7045 port mapping, but idk its odd that its "not responding" when you did no changes
only you know
only you knowOP5mo ago
totally still not working
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
it's working with 27018 already my point is blazor didn't respond
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
wdym then
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
but I can use db through api
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
I mean i can use mongodb right now with 27017 api's are communicating with mongo
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
🤯 wdym bro
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
my backend communicates with mongo db and I can make CRUD operations right now is it okay now
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
funny but really makes sense I gotta check db from inside container then?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
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
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
uuuuuhh I've found why blazor is not working
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
but it works
No description
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
Pobiega said this
No description
only you know
only you knowOP5mo ago
at the top
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
only you know
only you knowOP5mo ago
same here
Pobiega
Pobiega5mo ago
its specifically when using docker compose it creates whats called a "network" of containers that can access eachother without going via the host
only you know
only you knowOP5mo ago
I'm taking connection refused error log.
No description
only you know
only you knowOP5mo ago
but I can connect the default page with no problem
No description
only you know
only you knowOP5mo ago
oh I found the problem solved it
Pobiega
Pobiega5mo ago
what was the problem? and the solution
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
right. but FE runs outside the container since its WASM on the client machine
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
k
only you know
only you knowOP5mo ago
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
Pobiega
Pobiega5mo ago
/close
Want results from more Discord servers?
Add your server