Nima
Nima

Reputation: 349

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:

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

Upvotes: 4

Views: 15737

Answers (4)

Juan
Juan

Reputation: 6383

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.

Upvotes: 10

Guido U. Draheim
Guido U. Draheim

Reputation: 3271

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.

Upvotes: 2

Truong Dang
Truong Dang

Reputation: 3427

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

Upvotes: 0

André Andrade
André Andrade

Reputation: 313

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.

Upvotes: 0

Related Questions