问题
I created a docker container using official node image.
docker pull node
then I created an angular 2 app using angular-cli.
ng new docker-demo
then I ran my docker container from the directory of my angular2 app using following command:
cd docker-demo
& then inside the directory :
docker run -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start
where pwd stands for directory of my angular2 app.
now in my console I can see my angular2 app getting bundled & running just like it runs on my local machine.
but when I try to access it from my browser using ip of my docker-machine :
http://192.168.99.100:8080/
it doesn't work.
PS: angular2 app works perfectly fine in my local system.
is there any extra configuration required?
any inputs?
thanks in advance.
回答1:
I had a similar problem. Was able to fix it using ng serve --host 0.0.0.0
as the starting command for my container. It is important to remember exposing the right ports at build time. Make sure you use EXPOSE 4200
or any other port you want to expose your app in your Dockerfile. 4200 is the default for Angular2/4 apps, so most of the time is the best choice.
If you're using Docker compose, the same thing. Remember to expose the port and use the appropriate arguments in the ng serve command. Hope it helps you and any other fellow captain struggling with the same problem.
回答2:
Your docker command should be like this:
docker run --rm --name my-container -it -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start
- --rm -> will remove you container when you stop it
- -it -> will folow log output
- --name -> give a name for your container
You need -it if you want follow the log or -d if you dont wana folow the log output.
I recomment to use --name your-container-name to make easy to remove/stop/start your container.
To check the status of you container:
docker ps -a
Where -a will show all containers, inclusive stoped containers.
See more here: Docker Documentation
回答3:
You need to expose the port as per the image installation guide:
Create a Dockerfile in your Node.js app project
FROM node:4-onbuild
# replace this with your application's default port
EXPOSE 8888
You can then build and run the Docker image:
$ docker build -t my-nodejs-app .
$ docker run -it --rm --name my-running-app my-nodejs-app
So in your case:
> ng new docker-demo
> cd docker-demo
> echo "FROM node:6.6.0-wheezy
EXPOSE 4200" > Dockerfile
> docker build -t my-nodejs-app .
> docker run --rm --name docker-demo -p 8080:4200 -v $(pwd):/var/www -w "/var/www" my-nodejs-app npm start
You app should be accessible on 127.0.0.1:8080
(replace 127.0.0.1 with relevant docker ip).
来源:https://stackoverflow.com/questions/39551135/not-able-to-access-angular2-app-from-docker-container