Docker 在容器内运行但无法通过浏览器访问

时间:2021-01-07 01:37:36

标签: docker dockerfile

作为 Docker 的新手,我正在学习教程,但使用的是我自己的个人 MERN 堆栈项目。我的个人项目文件夹结构由前端文件夹和后端文件夹组成,我的 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.

docker 容器

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 运行。

1 个答案:

答案 0 :(得分:0)

更新

原来是通过这个大纲

docker run -p <host_port>:<container_port>

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