I have a .NET Core 2.1 API which, when run via Visual Studio in Windows, receives a HTTP GET from the browser and successfully returns data from a MySQL DB.
I've been trying to add the API to a Docker container (inside Ubuntu) - this builds, but I can't access the API from the browser:
What I've tried:
- Checking available ports. Here's the relevant output:
- Connecting using a different browser: In Chromium, it says 'This site can't be reached: localhost unexpectedly closed the connection.'
- Testing unused ports in the browser to see if this returns a different message - for Firefox, unused ports return 'Unable to connect' instead of 'Secure connection Failed', and in Chromium they return 'localhost refused to connect.' instead of 'localhost unexpectedly closed the connection.'
- In Firefox, configuring 'security.tls.insecure_fallback_hosts' to 'localhost'.
- Double-checking that ports are mapped correctly in docker-compose.yml - under 'ports' for the relevant container, 44329 is mapped to 44329.
- Running a DB query when the API starts and logging the result: this is successful. I run
docker-compose up
, the API starts, connects to the DB container, and it logs the result of an SQL query to a text file. So the problem is unlikely to be anything to do with the database. - Logging GET requests: Inside Windows, the GET requests are successfully logged, but in the Ubuntu Docker container, they're not.
This might be relevant: In Windows, the API only works properly if you start it from Visual Studio. If you execute dotnet webapi.dll
, you get this output
...\netcoreapp2.1>dotnet webapi.dll
...
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
So here it's not running on 44329; instead it's accessible via 5001 in the browser, where you get this message which is not chill at all: Clicking 'ADVANCED' allows you to continue to the API.
- So I've also tried connecting via 5001 in Ubuntu without luck.
Here's the output from 'docker-compose up':
matt@Matt-Ubuntu:~/docker2$ docker-compose up
Starting docker2_mysql_1 ... done
Starting docker2_dbmodelmapper_1 ... done
Attaching to docker2_mysql_1, docker2_dbmodelmapper_1
... (mysql stuff) ...
dbmodelmapper_1 | Hosting environment: Production
dbmodelmapper_1 | Content root path: /app
dbmodelmapper_1 | Now listening on: http://[::]:80
dbmodelmapper_1 | Application started. Press Ctrl+C to shut down.
Seeing the port 80, I've tried connecting to that, without success.
The relevant container as shown by
docker ps
is:
What kind of issue am I looking at here?
As pointed out by multiple people in the comments (thanks sp0gg, Martin Ullrich and Daniel Lerps), the API was incorrectly listening on port 80.
The solution was to map port 5000 to 44329 in docker-compose.yml for the API container, and also modify the Dockerfile to pass ports in as arguments to dotnet when starting the ASP.NET API: