Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Connection Refused" on Docker node container

Tags:

docker

I am attempting to run a simple Angular 1.5 app on my Mac within the a custom image built from the official Node image here but I get a "Connection Refused" error when attempting to access the site running inside the container. I am able to run the Angular app locally on my Mac with no error.

Here is my Dockerfile:

FROM        node
COPY        . /var/www
WORKDIR     /var/www
RUN         npm install
EXPOSE      8888
ENTRYPOINT ["npm", "start"]

In package.json, "npm start" is set to spin up the site on port 8888:

"start": "http-server -a localhost -p 8888 -c-1 ./app",

I built the custom image like this:

$docker build -t beerdb-web .

The image builds successfully:

$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
beerdb-web          latest              cf406b4dacc7        13 minutes ago      760.6 MB

I then launch a container from the image like so:

$docker run -p 8888:8888 -d cf

The contaier appears to be running fine and is properly mapping the container port 8888 to the docker machine port 8888:

$docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
828aeb67bc73        cf                  "npm start"         2 seconds ago       Up 2 seconds        0.0.0.0:8888->8888/tcp   condescending_almeida

I can inspect the container and see that it has an IP address and port 8888 exposed:

"NetworkSettings": {
   "Bridge": "",
   "SandboxID": "0d7180998a7c0bceb0c911069538f29297422fdf98bb859eba78e6584ce65919",
   "HairpinMode": false,
   "LinkLocalIPv6Address": "",
   "LinkLocalIPv6PrefixLen": 0,
   "Ports": {
       "8888/tcp": [
           {
               "HostIp": "0.0.0.0",
               "HostPort": "8888"
           }
       ]
   },
   "SandboxKey": "/var/run/docker/netns/0d7180998a7c",
   "SecondaryIPAddresses": null,
   "SecondaryIPv6Addresses": null,
   "EndpointID": "99bc9f9fad5a2445687327d45fb4fe29fb8267b08f119d3912720ced48b7d074",
   "Gateway": "172.17.0.1",
   "GlobalIPv6Address": "",
   "GlobalIPv6PrefixLen": 0,
   "IPAddress": "172.17.0.2",
   "IPPrefixLen": 16,
   "IPv6Gateway": "",
   "MacAddress": "02:42:ac:11:00:02",
   "Networks": {
       "bridge": {
           "IPAMConfig": null,
           "Links": null,
           "Aliases": null,
           "NetworkID": "41b55572b74543f33c99b3f86339ad4c81e9abaacbe0e92077f36cacae29a9c0",
           "EndpointID": "99bc9f9fad5a2445687327d45fb4fe29fb8267b08f119d3912720ced48b7d074",
           "Gateway": "172.17.0.1",
           "IPAddress": "172.17.0.2",
           "IPPrefixLen": 16,
           "IPv6Gateway": "",
           "GlobalIPv6Address": "",
           "GlobalIPv6PrefixLen": 0,
           "MacAddress": "02:42:ac:11:00:02"
       }
   }
}

My machine appears fine as well:

$docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.3   

But when I navigate to http://192.168.99.100:8888/ from my Mac I get "Connection Refused".

In the snippet below I have ssh'd into the docker machine. You'll see that I can ping the IP address of the container, but when I use curl to try and hit port 8888 I get connection refused:

$docker-machine ssh
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.12.3, build HEAD : 7fc7575 - Thu Oct 27 17:23:17 UTC 2016
Docker version 1.12.3, build 6b644ec
docker@default:~$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.092 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.092 ms
^C
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.092/0.092/0.092 ms
docker@default:~$ 
docker@default:~$ curl 172.17.0.2:8888
curl: (7) Failed to connect to 172.17.0.2 port 8888: Connection refused
docker@default:~$ 

To make sure the site is running properly within the container I launched a command prompt into the container and used curl to hit localhost:8888 and my web site's home page was returned.

I believe that the custom image with built properly from the node base image and that it is functioning as expected. I believe there is a problem with either the port mapping or with the machine itself. I have a basic install of the Docker toolkit on my Mac and have not made any changes to the Default machine that runs inside VirtualBox, yet I still cannot access the web site running inside my container.

Very frustrated, any help is appreciated.

like image 358
vt_todd Avatar asked Feb 05 '23 13:02

vt_todd


1 Answers

The -a localhost switch in your http-server command line causes the HTTP server to bind itself to the container's loopback device (127.0.0.1). Since the container uses a different network namespace than the host (your Docker machine), 127.0.0.1 in the container is not equal to 127.0.0.1 on the host.

In order to be reachable from other network interfaces, bind to all available interfaces by specifying 0.0.0.0 as the bind address (-a 0.0.0.0, which coincidentally is also the default value for this option, allowing you to omit it entirely):

"start": "http-server -p 8888 -c-1 ./app",
like image 79
helmbert Avatar answered Feb 13 '23 03:02

helmbert