0

Setup - I have a simple server running inside of a docker container. Dockerfile:

  web:
    image: my_web
    build:
      context: ./
      dockerfile: web.docker
    container_name: my_web
    networks:
      - front
    ports:
      - "4200:4200"
      - "4300:4300"
    volumes:
      - www:/var/www
      - wwwlogs:/var/www/storage/logs
    env_file:
      - ${SERVICE_ENVIRONMENT}.env

web.docker:

# start build from node:latest
FROM node:latest

MAINTAINER me <info@example.com>

# Install system-wide dependencies
RUN apt-get -yqq update

... some unimportant setup stuff ...

USER me

# expose port
EXPOSE 4200

WORKDIR /me

## Run server
CMD ["/bin/bash", "-c", "ng serve --proxy-config projects/my-ui/src/environments/proxy.conf-local.json --hmr"]

This all works. The server comes up

...
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
...

And from inside the container I can hit the service fine:

> docker exec -u 0 -it my_web /bin/bash
root@621da3c6697f:/me# curl localhost:4200
<!doctype html>
<html>
<head>
...

However, from outside the container (OSX host), I get an empty response:

> curl localhost:4200
curl: (52) Empty reply from server

I don't think this has anything directly to do with docker networking. I have other API services set up that are responding fine. I also logged in to the web container and started a simple web server with python:

me@621da3c6697f:/me# python -m SimpleHTTPServer 4300
Serving HTTP on 0.0.0.0 port 4300 ...

And I can reach that fine from the host:

> curl localhost:4300
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
...

And the server spits out an access log line:

172.22.0.1 - - [18/Oct/2018 19:56:59] "GET / HTTP/1.1" 200 -

I've also switched things around so the simple HTTP server is running on port 4200, but that doesn't make a difference.

I'm thinking this has something to do with how npm binds to a host (only listening to requests to 127.0.0.1 or some such), but for the life of me I can't figure it out. Anyone have any advice to further debug or ideas on changing how npm binds addresses?

--- Edit ---

There's also a difference between accessing a port without anything running on it. From the host:

> curl localhost:4000
curl: (7) Failed to connect to localhost port 4000: Connection refused
> curl localhost:4200
curl: (52) Empty reply from server

So it's definitely hitting something bound to 4200, it's just that server refuses to give any content out...

DrTeeth
  • 927
  • 2
  • 10
  • 32
  • I'm not sure you mean "npm" here. – Oliver Charlesworth Oct 18 '18 at 20:06
  • I'll admit, the Angular stack is *not* my strength. But I'm starting the server with "npm run start-local" which is why I specified npm. That in turn expands to "ng serve --proxy-config projects/xform-ui/src/environments/proxy.conf-local.json --hmr" (which is what I now have directly coded into my docker file). Should this be over in Node? Angular? – DrTeeth Oct 18 '18 at 20:10

1 Answers1

0

OK, figured it out - it is how it binds to the host. Been searching for a while, and finally came across this post nodejs app doesn't connect to localhost when running within a docker container

Long story short, specify the host IP address to bind to on the ng serve line:

--host 0.0.0.0

i.e.

CMD ["/bin/bash", "-c", "ng serve --host 0.0.0.0 --proxy-config projects/my-ui/src/environments/proxy.conf-local.json --hmr"]

I think you can probably set this within the angular config files as well, but this was enough to unblock me for now.

And now it responds to requests from the host:

> curl localhost:4200
<!doctype html>
<html>
<head>
...
DrTeeth
  • 927
  • 2
  • 10
  • 32