Docker container with Angular2 app and NodeJs not responding

I created a new Angular2 angular-cli application and ran it in Docker. But I can not connect it to the local host.

First, I run the application on my local machine:

ng new project && cd project && "put my Dockerfile there" && docker build -t my-ui . 

I run it with the command:

 docker run -p 4200:4200 my-ui 

Then try my localhost:

 curl localhost:4200 

and get

curl: (56) Error Recv: connection reset using a peer

Then I tried switching to the running container (docker exec -ti container-id bash) and running curl localhost: 4200, and it works.

I also tried to start the container with -net = host param:

 docker run --net=host -p 4200:4200 my-ui 

And it works. What is the problem? I also tried to start the container in daemon mode and this did not help. Thanks.

My Dockerfile

 FROM node RUN npm install -g angular-cli@v1.0.0-beta.24 && npm cache clean && rm -rf ~/.npm RUN mkdir -p /opt/client-ui/src WORKDIR /opt/client-ui COPY package.json /opt/client-ui/ COPY angular-cli.json /opt/client-ui/ COPY tslint.json /opt/client-ui/ ADD src/ /opt/client-ui/src RUN npm install RUN ng build --prod --aot EXPOSE 4200 ENV PATH="$PATH:/usr/local/bin/" CMD ["npm", "start"] 
+5
source share
2 answers

It seems that you are using ng serve to start the development server and by default run the loop interface (available only on localhost). You must specify a specific parameter:

 ng serve --host 0.0.0.0 

to run it on all interfaces.

+7
source

You need to modify angular-cli to serve the application from outside ie update your npm script number to ng serve --host 0.0.0.0

+3
source

Source: https://habr.com/ru/post/1262353/


All Articles