问题描述
请给我一些帮助,这是docker的新手;感觉这是我想念的小事.
Please I need some help, new to docker; have a feeling it's something minor I missed.
我正在尝试使用dockerfile和docker-compose运行nodejs应用程序;我使用的是Ubuntu 17.04 LTS,尽管状态显示UP并正在运行,但我无法从 localhost:4200
I'm trying to run a nodejs app using a dockerfile and docker-compose; I'm on Ubuntu 17.04 LTS however, eventhough the status shows UP and runnning I can't access the app from localhost:4200
docker-compose.yml
version: '3'
services:
my-app:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/usr/src/app
ports:
- "4200:4200"
docker ps
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6a99aab4e37 my-app "ng serve" 10 minutes ago Up 10 minutes 0.0.0.0:4200->4200/tcp my-app_1
我运行了以下命令并获得了一个IP地址,我在浏览器中使用了该ip:port#,但应用程序无法显示:
I ran the following command and got an ip address, I used that ip:port# in browser but app won't show up:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' 'my-app_1'
当我进入localhost:4200时不起作用;我还尝试了:docker run --rm -it -p 4200:4200 my_app_container
并运行,我可以看到npm start运行并在端口4200上侦听,但是当我进入浏览器并键入localhost:4200
时,我什么也没得到,只是:无法访问此站点; ERR_NAME_NOT_RESOLVED
when I go to localhost:4200 it doesn't work; I also tried: docker run --rm -it -p 4200:4200 my_app_container
and it runs and I can see that npm start runs and listens on port 4200 but when I go to browser and type localhost:4200
I get nothing, just: this site can't be reached; ERR_NAME_NOT_RESOLVED
ubuntu17@ubuntu:~/playground/apps/my_app-ui$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_app latest d41d3d77811b 17 minutes ago 1.24GB
nept0 latest df93134e2539 21 minutes ago 1.24GB
node 9 c888d933885c 5 days ago 676MB
registry 2.6.1 c2a449c9f834 6 months ago 33.2MB
ubuntu17@ubuntu:~/playground/apps/my_app-ui$ docker run -it --rm -p 4200:4200 nept0
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2018-01-16T15:46:19.502Z
Hash: 00ec980debaf8e47350d
Time: 12386ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 270 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 511 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 11.8 MB [initial] [rendered]
webpack: Compiled successfully.
最后: -最奇怪的是,如果我不使用docker并仅通过运行以下命令手动构建应用程序:ng serve or npm serve
,它将进入package.json
->脚本部分并启动ng服务,然后localhost:4200
运行正常,因此我输给了1.
LASTLY: - The weirdest thing is if I don't use docker and just build the app manually by simply running: ng serve or npm serve
which looks into package.json
-> script section and starts ng serve, then localhost:4200
runs fine so I'm lost at to 1.
为什么通过docker运行时无法在浏览器上访问localhost:4200
或ipaddress:4200
,但是如果我仅通过运行npm start在docker外部手动构建,它可以工作吗?
Why I can't access localhost:4200
or ipaddress:4200
on browser when running via docker but it works if I build manually outside of docker by just running npm start?
推荐答案
编辑
https://stackoverflow.com/a/48286174/2663059
有解决办法.
编辑
首先.为什么要这样做.
First of all . Why you have done this .
#EXPOSE 4200
Dockerfile中的
. #在前面意味着在dockerfile中添加注释.使用这个
in Dockerfile. # in front means comment in dockerfile . Use this
EXPOSE 4200
#EXPOSE 4200 means you have not expose the port .
然后检查容器是否在Docker中运行了节点服务器.你怎么能检查这个. docker exec -it nept0 bash或可以尝试
And next check that container running your node server inside the Docker or not.How can you check is this. docker exec -it nept0 bashor can try
docker exec -it nept0 /bin/bash
然后您可以运行
curl localhost:4200 或,如果您的节点工作正常#EXPOSE 4200是node.js的get api.
curl localhost:4200 or the get api of the node.js if that working fine your node is working fine #EXPOSE 4200 was culprit .
您可以在此处阅读有关docker exec的更多信息 https://docs.docker. com/engine/reference/commandline/exec/
You can read for docker exec here more https://docs.docker.com/engine/reference/commandline/exec/
让我知道是否有任何问题.
Let me know if any issue.
这篇关于无法在localhost:4200的浏览器上访问nodejs应用(docker run -p 4200:4200 ....)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!