Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to exposed port of container started with docker-compose on Windows

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
like image 981
Nima Avatar asked Sep 03 '25 03:09

Nima


2 Answers

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.

like image 146
Juan Avatar answered Sep 06 '25 02:09

Juan


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.

like image 21
Guido U. Draheim Avatar answered Sep 06 '25 01:09

Guido U. Draheim