本文介绍了无法在 localhost:4200 的浏览器上访问 nodejs 应用程序(docker run -p 4200:4200 ....)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我需要一些帮助,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

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 或 npm serve 可以查看 package.json -> 脚本部分并启动 ng serve,然后 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:4200ipaddress:4200 但如果我通过运行 npm 在 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?

推荐答案

EDIT

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 表示你没有暴露端口.

接下来检查是否在 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 node.js 的 get api 如果工作正常,您的节点工作正常#EXPOSE 4200 是罪魁祸首.

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 ....)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:21