3

I have looked at other similar questions like: Access to Docker Container Port and access docker container's port and Cannot access port on host mapped to docker container port and I think I am doing everything I can to be able to access port 4200 on my docker container yet I am not able to.

Here is the output of docker ps:

$ docker ps
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS               NAMES
298f832ca50d        redcricket/fisherman:latest   "/usr/sbin/init"    About an hour ago   Up 39 minutes       4200/tcp            gallant_matsumoto

My container is pingable:

$ ping `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' gallant_matsumoto`

Pinging 172.17.0.2 with 32 bytes of data:
Reply from 172.17.0.2: bytes=32 time=110ms TTL=239
Reply from 172.17.0.2: bytes=32 time=82ms TTL=239
Reply from 172.17.0.2: bytes=32 time=92ms TTL=239
Reply from 172.17.0.2: bytes=32 time=80ms TTL=239

Ping statistics for 172.17.0.2:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 80ms, Maximum = 110ms, Average = 91ms

On my server I start angular like so:

[root@298f832ca50d HelloWorld]# ng serve --host 0.0.0.0 --port 4200 --disableHostCheck
WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
WARNING: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.
 10% building 3/3 modules 0 activeℹ 「wds」: Project is running at http://0.0.0.0:4200/webpack-dev-server/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: 404s will fallback to //index.html
chunk {main} main.js, main.js.map (main) 9.77 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 251 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.09 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 16.3 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 3.8 MB [initial] [rendered]
Date: 2019-07-06T19:58:08.308Z - Hash: 201a042264b357c18e36 - Time: 14593ms
** Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
ℹ 「wdm」: Compiled successfully.

But I still cannot access http://172.17.0.2:4200/

C:\Users\plankton>curl http://172.17.0.2:4200/
curl: (7) Failed to connect to 172.17.0.2 port 4200: Timed out

What did I miss?

Update: Thanks seahorsepip I tried this:

$  docker run --privileged  -p=4200:4200 -itd -e container=docker  -v /sys/fs/cgroup:/sys/fs/cgroup  redcricket/fisherman:latest /usr/sbin/init
870cd55d0bc65ffe27c595b0092f28a5e333c235ae355a2469563b09058cdf22

plankton@DESKTOP-C8MFTFD MINGW64 /c/Program Files/Docker Toolbox
$ docker ps
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS              PORTS                    NAMES
870cd55d0bc6        redcricket/fisherman:latest   "/usr/sbin/init"    8 seconds ago       Up 3 seconds        0.0.0.0:4200->4200/tcp   kind_kapitsa

But I am still not able to connect ...

C:\Users\plankton>curl http://localhost:4200/
curl: (7) Failed to connect to localhost port 4200: Connection refused

Update II: Thanks to David below I now know the VM's ip

$ docker-machine.exe ip
192.168.99.100

I am not sure how to use this IP though.

On the client I get ...

C:\Users\plankton>curl http://192.168.99.100:4200
curl: (7) Failed to connect to 192.168.99.100 port 4200: Connection refused

If I try to restart the server like so ...

[root@bfd864884a59 HelloWorld]# ng serve --host 192.168.99.100
An unhandled exception occurred: listen EADDRNOTAVAIL: address not available 192.168.99.100:4200
See "/tmp/ng-YKKmQ9/angular-errors.log" for further details.
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • If you're using Docker Toolbox, you'll need to use the VM's IP address, most often 192.168.99.100. I highly recommend reading Docker's [Getting Started, Part 2: Containers](https://docs.docker.com/get-started/part2/) documentation on building and running custom images: your image's default `CMD` should just start your application without you needing to get an interactive shell in it, and you should not need `--privileged` or to bind-mount anything in `/sys`. – David Maze Jul 06 '19 at 23:05
  • Thanks for the pointers David. I think the VM's IP is `172.17.0.2` which I determined by executing `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' gallant_matsumoto`. I have tried that I think. No joy. – Red Cricket Jul 06 '19 at 23:26
  • The IP addresses `docker inspect` returns are basically useless in many contexts; you cannot directly reach them from the host with Docker Toolbox, for example. You never need to use or know these IP addresses and shouldn't need to look them up. – David Maze Jul 06 '19 at 23:35
  • Ok so how to I get the "VM's IP address"? – Red Cricket Jul 06 '19 at 23:38
  • `docker-machine ip` will tell you. – David Maze Jul 07 '19 at 00:38
  • ah! yes I see `$ docker-machine.exe ip 192.168.99.100` But I am unsure what to do the this ip? – Red Cricket Jul 07 '19 at 01:27

1 Answers1

2

The port is only reachable in the Docker network itself, you need to forward the port to outside the Docker network:

docker run -p 4200:4200 ...

Then you can reach your docker instance at localhost:4200.

More info on Docker networking: https://docs.docker.com/config/containers/container-networking/

More info on publish and expose (difference): What is the difference between "expose" and "publish" in Docker?

seahorsepip
  • 4,519
  • 1
  • 19
  • 30
  • Do I still need to `--expose 4200` option when I execute `docker run ...`? – Red Cricket Jul 06 '19 at 20:59
  • Expose is recommended but when you publish (-p) a port, Docker implicitly already exposes that port for you so you can leave it out and it will still work even though it's not recommend. More info: https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker – seahorsepip Jul 07 '19 at 05:21
  • The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. – seahorsepip Jul 07 '19 at 05:23