Access Angular application in a Docker container

  angular, docker, dockerfile, localhost, node.js

I’m trying to run an Angular app inside a docker container but i can’t access it when I go to http://localhost:4200. When I run the application I can see the usual angular logs but I can’t access it from my browser.

I tried to expose port 4200, specify the port when running the app “docker run -p 4200:4200 angular-example” and specify the host and the port in the ng serve command but none of these manipulations worked.

My Dockerfile

# base image
FROM node:12.2.0

# install chrome for protractor tests
RUN wget -q -O - | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install node-sass
RUN npm install
RUN npm install -g @angular/[email protected]

# add app
COPY . /app


# start app
CMD ng serve --host --port 4200

The command I used to run the image

docker run -p 4200:4200  angular-example

and the logs that I get when I run the application

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
** Angular Live Development Server is listening on, open your browser on http://localhost:4200/ **

Date: 2019-10-13T04:08:51.354Z
Hash: 518bf687971012e084e7
Time: 89920ms
chunk {main} main.js, (main) 304 kB [initial] [rendered]
chunk {polyfills} polyfills.js, (polyfills) 237 kB [initial] [rendered]
chunk {runtime} runtime.js, (runtime) 6.08 kB [entry] [rendered]
chunk {styles} styles.js, (styles) 178 kB [initial] [rendered]
chunk {vendor} vendor.js, (vendor) 7.82 MB [initial] [rendered]
ℹ 「wdm」: Compiled successfully.

I get a ERR_CONNECTION_REFUSED when I launch the application instead of reaching the actual application.

Source: StackOverflow