-2

i have a node js angular application which i am trying to containerize it.

  1. i am using Docker file to build the image and run the docker
  2. i am not getting any error and the build is success Node.js build

Dockerfile

Ajay R
  • 1
  • 4
  • ** Angular Live Development Server is listening on 0.0.0.0:4100, open your browser on http://localhost:4100/ ** ℹ 「wdm」: Compiled successfully. – Ajay R Aug 04 '19 at 09:32
  • Provide your entire `dockerfile` and `docker run` command. – mchawre Aug 04 '19 at 09:33
  • # Pulling out NODE Image FROM node:latest # Creating a main directory to work with Docker RUN mkdir /usr/src/app # Setup the main dir as working dir WORKDIR /usr/src/app # Copy the project dir to DOCKER working dir COPY . /usr/src/app # Command to install the Angular-Cli@project_version RUN npm install RUN npm uninstall -g @angular/cli RUN npm install -g @angular/cli@7.2.2 RUN npm link @angular/cli RUN ng config -g cli.warnings.versionMismatch false #RUN npm run-script build EXPOSE 8080 CMD ng serve --port 4100 – Ajay R Aug 04 '19 at 09:33
  • please update it in the question can't see it properly in comments. – mchawre Aug 04 '19 at 09:34
  • added the screen shots for docker file and output of the nodejs build @mchawre – Ajay R Aug 04 '19 at 09:36
  • 3
    Why do you think we want *screenshots*? – jonrsharpe Aug 04 '19 at 09:41
  • @AjayR you are exposing port `8080` and application listening on `4100`, first expose `4100` then `docker run -p 4100:4100 youimage:tag` – Adiii Aug 04 '19 at 10:05
  • i cannot paste the docker file code hence i posted screen shots. @jonrsharpe – Ajay R Aug 04 '19 at 10:29
  • Why can't you paste it? It's just text. – jonrsharpe Aug 04 '19 at 10:29
  • @Adiii i did the same no go still i cannot access on my host machine 5c58fecc2d9c latestops:v2 "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 0.0.0.0:4100->4100/tcp objective_spence – Ajay R Aug 04 '19 at 11:36

1 Answers1

0

Since your node.js angular application is running on port 4100. You need expose that port using port mapping.

docker run -itd -p 4100:4100 myimage:v1

NOTE: -p option will expose container port on to the host port.

Now you can access your application at http://host-ip:4100

Hope this helps.

mchawre
  • 10,744
  • 4
  • 35
  • 57
  • I tried it as well its not working. root@master:~/ops-automation-services/ops-automation-services# docker run -itd -p 4100:4100 opstest:v1 f1fb7ec1241adbfffc77d276a66798ac76feed7c8135bac969b2485ee8f290d7 root@master:~/ops-automation-services/ops-automation-services# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1fb7ec1241a opstest:v1 "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:4100->4100/tcp, 8080/tcp pedantic_kare – Ajay R Aug 04 '19 at 09:45
  • i do not get any error its just do not load anyting – Ajay R Aug 04 '19 at 09:48
  • do this `docker exec -it f1fb7ec1241a sh` it will get you inside the container shell and now run `curl localhost:4100` and `netstat -anp | grep 4100` to check whether your application is up and running and listening on which port. – mchawre Aug 04 '19 at 09:50
  • i ran the above curl command i get an html out put # curl localhost:4100 i have pasted half of it . when i to a netstat -anp | grep 4100 i get netstat not found – Ajay R Aug 04 '19 at 10:26
  • What happens when you do `curl localhost:4100` or `curl host-ip:4100` from the host machine. Can you paste the output here. – mchawre Aug 04 '19 at 10:28
  • stills same issue , logged into container and did curl localhost:4100 i get an out put html format . i did a net stat -anp | grep 4100 root@5c58fecc2d9c:/usr/src/app# netstat -anp | grep 4100 tcp 0 0 127.0.0.1:4100 0.0.0.0:* LISTEN 7/ng serve --port 4 – Ajay R Aug 04 '19 at 11:44
  • Are you trying to curl from your host machine. – mchawre Aug 04 '19 at 11:45
  • No mchware i have a ec2 instance which is ubuntu i have docker on it . i am exposing 4100 port from docker and i am hitting my ec2 ip:4100 @mchawre – Ajay R Aug 04 '19 at 11:47
  • Have you verified port 4100 of your ubuntu ec2 machine is open in security group. – mchawre Aug 04 '19 at 11:48
  • Also if on linux shell of ubuntu ec2 machine the curl command works? – mchawre Aug 04 '19 at 11:49
  • the curl command and netstat works only with in docker container. when i use docker exec -it and then curl, netstat. i installed net-tools on host ec2 instance and did netstat -anp | grep 4100 i get the below output root@ip-172-31-83-86:~# netstat -anp | grep 4100 tcp6 0 0 :::4100 :::* LISTEN 4690/docker-proxy @mchawre – Ajay R Aug 04 '19 at 11:51
  • what happens when you do curl from the ec2 shell? – mchawre Aug 04 '19 at 12:01
  • root@ip-172-31-83-86:~# curl localhost:4100 curl: (56) Recv failure: Connection reset by peer – Ajay R Aug 04 '19 at 12:10
  • try to curl with private/public ip of machine – mchawre Aug 04 '19 at 12:11
  • when i try public ip:4100 it going to next prompt and dose not do anything. private ip :4100 its says connection refused. @mchawre – Ajay R Aug 04 '19 at 12:23
  • go through this https://stackoverflow.com/questions/46778868/ng-serve-not-working-in-docker-container something might help. – mchawre Aug 04 '19 at 12:28
  • finally i got it resolved but my concern is if we use --host 0.0.0.0 in Docker file . it give me a vulnerability .root@master:~/ops-automation-services/ops-automation-services# docker run -p 8080:4200 opslatest:v1 WARNING: 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" – Ajay R Aug 04 '19 at 13:57
  • i resolved it by following that link which you gave me. but will it cause a security issue – Ajay R Aug 04 '19 at 14:37