1

I'm having trouble accessing apps that should expose ports to the host via docker-compose. Here is a reproducible example:

I create a new angular app using the angular CLI:

ng new angular-docker

Then I create a Dockerfile with the following contents in that directory:

FROM node:8

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install

EXPOSE 4200

CMD ["npm", "start"]

Next I create a docker-compose.yaml file in the same directory:

version: '3'

services:
  angular:
    build: .
    container_name: angular-docker
    volumes:
      - ./src:/usr/src/app/src
    ports:
      - "4200:4200"

Then I run:

docker-compose up

I wait until I get the following line in the docker-compose output

angular-docker | ** Angular Live Development Server is listening on localhost: 4200, open your browser on http://localhost:4200/ **

From docker inspect angular-docker I see that the port-forwarding rule is in place:

...
"Ports": {
            "4200/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "4200"
                }
            ]
        },
...

Now, when I try to go to http://localhost:4200 in Chrome, I get ERR_EMPTY_RESPONSE.

However, when I use docker exec -it angular-docker bash to bash into the container, I get a response, when I curl localhost:4200.

My environment:

  • OS: Windows 10 Pro Version 10.0.16299 Build 16299
  • Docker: 18.03.1-ce-win65 (17513)

I figured that this might be a firewall issue. So for debugging, I closed the firewall application (I'm using Kaspersky Internet Security), with no luck.

Why can I not access the container from the exposed port?

EDIT:

netstat -an | findstr ":4200" returns the following:

Proto  Local Address          Foreign Address        State
TCP    0.0.0.0:4200           0.0.0.0:0              LISTENING
TCP    [::1]:4200             [::]:0                 LISTENING
Nima
  • 309
  • 4
  • 13
  • Possible duplicate of [Bind container port to host inside Dockerfile](https://stackoverflow.com/questions/42552034/bind-container-port-to-host-inside-dockerfile) – André Andrade May 12 '18 at 21:00
  • Have you checked if your server is listening on 0.0.0.0:4200 or localhost:4200? – Raghwendra Singh May 12 '18 at 21:30
  • @Cyclops see my edit – Nima May 13 '18 at 00:08
  • @Nima I wanted to confirm if that netstat was from host or from inside of docker container. Because on host, docker proxy binds 0.0.0.0:4200 as well as localhost:4200. Anyway, juan's answer and your acceptance towards his answer confirms my suspicion that your server was not listening on 0.0.0.0:4200 – Raghwendra Singh May 14 '18 at 12:14

4 Answers4

9

You can't use the default npm start out of the box within a docker container.

One alternative is to update that command in your package.json to run ng serve -H 0.0.0.0 like this:

"start": "ng serve -H 0.0.0.0"

This extra -H 0.0.0.0 is to listen to all the interfaces from the container.

Then as your port mappings are working, you should get your site on the desired port localhost:4200 from the host machine.

Juan
  • 5,401
  • 3
  • 28
  • 31
2

Would you please check http://127.0.0.1:4200

On a lot of Linux servers I bump into the problem that "localhost" resolves into "::1" being ipv6 while the application is only configured to listen on ipv4 addresses. Double-checking with "127.0.0.1" can figure that out.

Guido U. Draheim
  • 2,688
  • 1
  • 17
  • 16
0

When you use EXPOSE in docker, the port stay available to tcp connections wit others containers in your docker network. If you want to be able to connect a port using http, you should map ports from your local machine to a container port.

PORTS
 -3000:3000

Where the first port is the port on your local machine an the second ṕort is on the container.

André Andrade
  • 196
  • 2
  • 14
  • If you look at the docker-compose.yaml, I have set the ports configuration option to "4200:4200". Removing the `EXPOSE 4200` in the Dockerfile, didn't change anything. – Nima May 12 '18 at 23:59
0

Try to change like this

services: 
 application:
   container_name: angular-docker
   image: Your_image_build_from_docker_file
   ports:
    - "4200:4200"
   volumes:
    - ./src:/usr/src/app/src
Truong Dang
  • 2,459
  • 13
  • 20