当我将其设置为在docker容器中运行时,为什么我的应用程序监听端口80而不是端口3000?
我正在尝试什么?
我正在尝试在docker容器中运行我的nodejs应用程序,并希望在该容器之外使用它(通过3000端口的浏览器).
I'm trying to run my nodejs app inside a docker container and want to use it outside of the container(through my browser on port 3000).
DockerFile
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm run build
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]
app.ts(相关部分)
const port = process.env.port || 3000;
let app = express();
app.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
我正在运行的
命令
$ docker run --net = host< imgName>
操作系统-> Windows 7
OS --> Windows 7
在Oracle Virtual Box中,我已将网络设置更改为桥接网络.
And in the Oracle Virtual Box I've changed network settings to bridged networks.
命令成功运行,服务器开始在端口80上侦听.我可以通过邮递员,curl,浏览器等从端口80的容器外部访问它.
The command runs successfully and the server starts listening on port 80. And I can access it from outside the container on port 80 through postman,curl,browser,etc.
我在哪里做错了?如何使其能够在端口3000上侦听?如果我没有显式暴露
它,它甚至还能在端口上监听吗?
Where I am doing wrong? How can I make it to listen on port 3000? And also how is it even able to listen on port if I haven't exposed
it explicitly?
我认为码头工人正在将 port
作为 environment变量
传递,并将其设置为80,因为我自己没有传递任何环境变量.
I think the docker is passing port
as an environment variable
and setting it to 80 as I am not passing any environment variable myself.
请帮助我对 docker
刚起步.
仅在您的 Dockerfile
中公开不会为您完成此操作.当您执行 docker run
或使用 docker-example.yaml
执行时,您将需要对其进行映射.
Just exposing in your Dockerfile
won't do it for you. You will need to map it when you do docker run
or execute using docker-example.yaml
.
要实现此目的,您将需要使用 $ docker run -p 3000:3000
.之后,您将不需要使用-net = host
,这应该是端口80上提供该服务的原因.
To achieve that you will need to use $ docker run -p 3000:3000
. Following that you won't need to use --net=host
that should be the reason why the service is available on port 80.
希望有帮助!
干杯!