uee*_*iie 2 javascript express docker
我尝试运行一个带有节点的简单容器,我确实获得了添加的日志,.listen(port, log)
但是无法使用浏览器访问它。
我是Docker的新手,所以也许我错过了一些东西。
我首先运行docker build -t my-node-app .
来创建我的图像。
然后我跑 docker run -it --rm -name myapp -p 3000:80 my-node-app
然后服务器运行,我看到在回叫中添加的日志 app.listen(port ,cb)
但是当我尝试打开时localhost:3000
,什么也没出现。
当我在没有docker的计算机上本地运行时,它工作正常。
我也尝试使用通过找到的IP地址访问该URL docker inspect myapp
,但是它也没有解决。
Dockerfile:
FROM node:4-onbuild
EXPOSE 80
Run Code Online (Sandbox Code Playgroud)
节点index.js:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
console.log('hello')
res.send('it works!');
});
app.listen(PORT, () => {
console.log(`${process.env.NODE_ENV}server is running on port ${PORT} on ${process.platform}`);
});
Run Code Online (Sandbox Code Playgroud)
The syntax of the -p
switch is: $HOSTPORT:$CONTAINERPORT
.
Looking at that, you are mapping the Host Port 3000, to the Guest Port 80. But, your node server isn't listening on 80; it's listening on 3000.
You can either:
PORT
environement variable to 80 with -e "PORT=80"
See ENV
switch-p 3000:3000
so that you are using the right guest port此外,您已经EXPOSE 80
在Dockerfile中进行了设置,但是仍然没有公开正确的端口。默认情况下,Node仍根据您的服务器代码侦听3000。
另外,由于您也使用-p 123:456
,因此有可能覆盖EXPOSE
设置。请参阅:覆盖Dockerfile映像默认值。
归档时间: |
|
查看次数: |
568 次 |
最近记录: |