Unable to access application outside of Docker container(e.g. Browser)

Hello,

I have an application running inside docker on the port 8585. I can access the application internally by going to the terminal of the container through docker desktop and doing curl to the localhost:8585 to it and I can see HTML content being returned by the application. However, when I try to hit the URL outside of docker (http://localhost:8585) i.e. on the browser running on Mac then I get “ERR_EMPTY_RESPONSE” error on the same port i.e. 8585.

Below is the command I used to run the docker image

docker run -d -p 8585:8585 ftl-server --name ftls1@localhost:8585

Below are the set of commands that I ran to prove the application is working fine internally on the docker container but not from outside.

Sunnys-MacBook-Pro:FTL - Sunny$ docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                    NAMES
405cb36f8868   ftl-server   "/opt/run.sh -…"   4 minutes ago   Up 4 minutes   0.0.0.0:8585->8585/tcp   great_pasteur
Sunnys-MacBook-Pro:FTL - Sunny$ docker exec -it 405cb36f8868 bash
bash-4.4$ pwd
/opt
bash-4.4$ curl http://localhost:8585

<!DOCTYPE html>
<html data-ng-app="ftl">
<head lang="en">
  <base href="/">
  <meta charset="UTF-8">
  <title translate="APPLICATION_TITLE"></title>

I had tried running a sample “node js” app and don’t get this issue i.e. I am able to access “node js” app both inside and on the mac browser but wondering why this would be the case for this specific application that I am trying to run. If I am able to do curl locally then ideally it should be accessible from outside of the docker container. Can share more details on the docker inspect if needed but wondering if someone else come across similar challenge in the past?

If your application is listening on localhost inside the container, nothing will be able to access it from outside. You have three localhosts. ONe inside the container, one in the VM of Docker Desktop and one on the macOS host. Even if you forward local ports to the container from the macOS host, it will be forwarded to the container IP not the localhost of the container. That is not possible. I am just guessing the syntax, since I don’t know your application, but based on your quoted command I would try something like this:

docker run -d -p 8585:8585 ftl-server --name ftls1@0.0.0.0:8585

Thanks rimelek for explaining. It makes complete sense now as I did try to do that also through the container ip and had the same error and only started to wonder when the node application was working with the same configuration (i.e. with localhost) but I can see now in the nodejs that it has host as “0.0.0.0” defined in the application which is why the translation worked but not in my other application as “host” is not declared.

Changing it to 0.0.0.0 has worked now for me. Thanks for your help.

Cheers,
Sunny