【问题标题】:Docker Running running within container but not reachable through browserDocker Running 在容器内运行,但无法通过浏览器访问
【发布时间】:2021-01-07 10:11:31
【问题描述】:

作为 Docker 新手,我正在学习一个教程,但使用的是我自己的个人 MERN-stack 项目。我的个人项目文件夹结构由一个前端文件夹和一个后端文件夹组成,我的 Dockerfile 放在根目录中。我的前端使用 localhost:3000,后端使用 localhost:5000。我正在尝试在浏览器中查看我的应用程序;然而,它把我带到了一个页面,上面写着this site can't be reachedhttp://localhost:3000 and http://localhost:5000http://172.17.0.3:3000,它只是一个永远加载的空白页面。 如果有帮助,我正在使用 macOS。

我已采取的步骤:

docker build -t foodcore:1.0 .

docker run -p 3001:3000 -p 5001:5000 foodcore:1.0

我的终端中的结果:

> server@1.0.0 dev
> concurrently "nodemon server.js" "npm run client"

[0] [nodemon] 2.0.6
[0] [nodemon] to restart at any time, enter `rs`
[0] [nodemon] watching path(s): *.*
[0] [nodemon] watching extensions: js,mjs,json
[0] [nodemon] starting `node server.js`
[1] 
[1] > server@1.0.0 client
[1] > cd .. && cd client && npm start
[1] 
[1] 
[1] > client@0.1.0 start
[1] > react-scripts start
[1] 
[0] Thu, 07 Jan 2021 01:15:15 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:12:9
[0] Thu, 07 Jan 2021 01:15:15 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
[0] Listening at: http://localhost:5000
[1] ℹ 「wds」: Project is running at http://172.17.0.3/
[1] ℹ 「wds」: webpack output is served from 
[1] ℹ 「wds」: Content not from webpack is served from /FoodCore/client/public
[1] ℹ 「wds」: 404s will fallback to /
[1] Starting the development server...
[1] 
[1] Compiled successfully!
[1] 
[1] You can now view client in the browser.
[1] 
[1]   Local:            http://localhost:3000
[1]   On Your Network:  http://172.17.0.3:3000
[1] 
[1] Note that the development build is not optimized.
[1] To create a production build, use npm run build.

码头集装箱

CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                              NAMES
6c5abad55b1b   foodcore:1.0          "npm run dev"            32 minutes ago   Up 32 minutes   0.0.0.0:3001->3000/tcp, 0.0.0.0:5001->5000/tcp     optimistic_chandrasekhar

Dockerfile

FROM node:latest

RUN mkdir -p /FoodCore

COPY . /FoodCore

WORKDIR /FoodCore/client

RUN npm install

WORKDIR /FoodCore/server

RUN npm install

EXPOSE 3000 5000

ENTRYPOINT [ "npm", "run", "dev" ]

非常感谢您抽出宝贵时间阅读本文。

更新

原来我试图访问 http://localhost:3000 但我将我的应用程序设置为在 3001 运行。

【问题讨论】:

    标签: docker dockerfile


    【解决方案1】:

    更新

    通过这个大纲得出的结果

    docker run -p <host_port>:<container_port>

    我最初将我的主机端口设置为 3001 而不是 3000。因此,访问了错误的端口

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 2021-12-09
      • 2019-05-04
      • 2021-08-04
      • 1970-01-01
      • 2018-09-07
      • 2019-07-18
      • 2015-06-27
      相关资源
      最近更新 更多