C
C#4mo ago
Thiago R.

Local machine refuses to connect to an ASP.NET Core Web API container

Hello I need to find out how to access my Docker container via the host machine. My connection is refused, no matter the tool I use (browser, curl, wget, javascript's fetch). I've been looking for a solution since yesterday, but no success at all. I've been trying to instantiate two containers within a same docker network, which is basically an ASP.NET Core Web API and a PostgreSQL server. I have 0 Docker knowledge at all. I was using Rider's feature for instantiating containers and it worked properly. However, I decided to use a docker-compose.yml file instead, because I was editing the same environment variables in both containers (such as PostgreSQL credentials and a default user seed data). I've deleted the IDE configuration before I actually start to build the docker-compose.yml file as follows:
version: '3.8'

services:
webapi:
image: trsaints-frontend-api
build:
context: .
dockerfile: Dockerfile
environment:
- JWT_SINGING_KEY=${JWT_SINGING_KEY}
- JWT_ISSUER=${JWT_ISSUER}
- JWT_AUDIENCE=${JWT_AUDIENCE}
- DB_HOST=db
- DB_PORT=5432
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- ALLOWED_HOSTS=${ALLOWED_HOSTS}
- ALLOWED_CORS_DOMAINS=${ALLOWED_CORS_DOMAINS}
- ADMIN_USERNAME=${ADMIN_USERNAME}
- ADMIN_PASSWORD=${ADMIN_PASSWORD}
- ASPNETCORE_URLS=http://localhost:8080
ports:
- "8082:8080"
- "8081:8081"
depends_on:
- db
networks:
- trsaints-network

db:
image: postgres:alpine3.20
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5433:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- trsaints-network

volumes:
postgres_data:

networks:
trsaints-network:
version: '3.8'

services:
webapi:
image: trsaints-frontend-api
build:
context: .
dockerfile: Dockerfile
environment:
- JWT_SINGING_KEY=${JWT_SINGING_KEY}
- JWT_ISSUER=${JWT_ISSUER}
- JWT_AUDIENCE=${JWT_AUDIENCE}
- DB_HOST=db
- DB_PORT=5432
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- ALLOWED_HOSTS=${ALLOWED_HOSTS}
- ALLOWED_CORS_DOMAINS=${ALLOWED_CORS_DOMAINS}
- ADMIN_USERNAME=${ADMIN_USERNAME}
- ADMIN_PASSWORD=${ADMIN_PASSWORD}
- ASPNETCORE_URLS=http://localhost:8080
ports:
- "8082:8080"
- "8081:8081"
depends_on:
- db
networks:
- trsaints-network

db:
image: postgres:alpine3.20
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5433:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- trsaints-network

volumes:
postgres_data:

networks:
trsaints-network:
Both containers run properly, I just can't connect to the api container externally.
7 Replies
Thiago R.
Thiago R.OP4mo ago
Source Code this is the API source code: https://github.com/trsaints/trsaints-frontend-api The api container's http port is mapping to 8082 becaues I have an apache server running on the host's 8080 port. But that shouldn't be an issue at all, as far as I know. What I've tried until then Also, I've tried the following suggestions from forum posts with related issues: * Checking iptables rules to assure the container address is not being blocked by the host's firewall: sudo iptables -L -v -n | grep 808:
0 0 ACCEPT 6 -- !br-233688ab8a57 br-233688ab8a57 0.0.0.0/0 172.18.0.3 tcp dpt:8080
0 0 ACCEPT 6 -- !br-233688ab8a57 br-233688ab8a57 0.0.0.0/0 172.18.0.3 tcp dpt:8081
0 0 ACCEPT 6 -- !br-233688ab8a57 br-233688ab8a57 0.0.0.0/0 172.18.0.3 tcp dpt:8080
0 0 ACCEPT 6 -- !br-233688ab8a57 br-233688ab8a57 0.0.0.0/0 172.18.0.3 tcp dpt:8081
* Disabling ufw temporarily * Setting ASPNETCORE_URLS to http://+:8080/ * Adding Kestrel:EndPoints:Http:Url=http://+:8080 to docker-compose.yml * Delete the container's and its images to clear any previous data None of the options above had any effect.
blueberriesiftheywerecats
try changing - ASPNETCORE_URLS=http://localhost:8080 to - ASPNETCORE_URLS=http://+:8080
Thiago R.
Thiago R.OP4mo ago
no effect ;/
blueberriesiftheywerecats
try adding Kestrel:EndPoints:Http:Url=http://+:8080
Thiago R.
Thiago R.OP4mo ago
in the docker-compose file? no effect at all Docker networking docker network inspect for that docker-compose network is as follows. I just removed the MacAddress parts for security reasons:
[
{
"Name": "trsaints-frontend-api_trsaints-network",
"Id": "233688ab8a5770f3d111211532b94c44d1aba571082730ce255a2d28907fb62a",
"Created": "2024-08-17T11:13:17.240077754-03:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"6c88858fcf034d3884d06e6938c1e4a58502611f0f477d95d28e070cd90cacdc": {
"Name": "trsaints-frontend-api-webapi-1",
"EndpointID": "eb2bdfd7b767d1db6d85d6d4181953edd24d5a3f41a53dae69b92e6193c40f52",
"MacAddress": "[Removed from post for security reasons]",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"6ddd6e368a0abd3671753083dc67aaed0be6f543c963c51b6d63c4cc01c3d6de": {
"Name": "trsaints-frontend-api-db-1",
"EndpointID": "6bbb83ee9689601cd27a40eb4e315832405d9ffbb0bc136364da9c1da5298482",
"MacAddress": "[Removed from post for security reasons]",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "trsaints-network",
"com.docker.compose.project": "trsaints-frontend-api",
"com.docker.compose.version": "2.29.1"
}
}
]
[
{
"Name": "trsaints-frontend-api_trsaints-network",
"Id": "233688ab8a5770f3d111211532b94c44d1aba571082730ce255a2d28907fb62a",
"Created": "2024-08-17T11:13:17.240077754-03:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"6c88858fcf034d3884d06e6938c1e4a58502611f0f477d95d28e070cd90cacdc": {
"Name": "trsaints-frontend-api-webapi-1",
"EndpointID": "eb2bdfd7b767d1db6d85d6d4181953edd24d5a3f41a53dae69b92e6193c40f52",
"MacAddress": "[Removed from post for security reasons]",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"6ddd6e368a0abd3671753083dc67aaed0be6f543c963c51b6d63c4cc01c3d6de": {
"Name": "trsaints-frontend-api-db-1",
"EndpointID": "6bbb83ee9689601cd27a40eb4e315832405d9ffbb0bc136364da9c1da5298482",
"MacAddress": "[Removed from post for security reasons]",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "trsaints-network",
"com.docker.compose.project": "trsaints-frontend-api",
"com.docker.compose.version": "2.29.1"
}
}
]
As the output suggests, the container's address is 172.18.0.3, but any tool that attempts to connect externally just gets refused and fails
curl http://172.18.0.3:8082
telnet 172.18.0.3 8082

curl: (7) Failed to connect to 172.18.0.3 port 8082 after 0 ms: Couldn't connect to server
Trying 172.18.0.3...
telnet: Unable to connect to remote host: Connection refused
curl http://172.18.0.3:8082
telnet 172.18.0.3 8082

curl: (7) Failed to connect to 172.18.0.3 port 8082 after 0 ms: Couldn't connect to server
Trying 172.18.0.3...
telnet: Unable to connect to remote host: Connection refused
Actually, I've tried to run busybox and appropriate images for fetching data inside the network, but looks like the response is the same:
docker run --rm --network trsaints-frontend-api_trsaints-network appropriate/curl http://172.18.0.3:8080/swagger

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
docker run --rm --network trsaints-frontend-api_trsaints-network appropriate/curl http://172.18.0.3:8080/swagger

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Sossenbinder
Sossenbinder4mo ago
So you can confirm with the docker logs that the server is running?
Thiago R.
Thiago R.OP4mo ago
so, it was actually an IDE thing. Running the containers using the IDE's UI would actually set the environment to development and enable Swagger, for example I found out that the environment was set to Production after some debugging I had to explicitly add an ASPNETCORE_ENVIRONMENT to the docker-compose.yml file thank you all for checking the post, btw
Want results from more Discord servers?
Add your server