本文介绍了Docker端口映射在Windows 10上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手。我正在尝试让一个简单的节点应用程序在docker上运行。但是我在docker端口发布方面遇到了问题。

I am new to docker. I am trying to get a simple node app running on docker. However I am facing an issue with the docker port publish.

- 18.03.0-ce,版本0520e24302

Docker version - 18.03.0-ce, build 0520e24302

我简单的应用代码:

'use strict';

const express = require('express');

// Constants
const PORT = 8081;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

我的docker文件:

My docker file:

FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8081

CMD [ "npm", "start" ]

-0.0.0.0:8080- > 8081 / tcp,充满爱意的拥抱

My docker ps output - 0.0.0.0:8080->8081/tcp, loving hugle

-无法连接到本地主机端口8080:连接被拒绝。

Output from curl command from my local - Failed to connect to localhost port 8080: Connection refused.

推荐答案

在Windows上Linux容器是在Windows主机OS上运行的虚拟机内部创建的。该虚拟机被分配了IP。进行卷曲时,应使用此IP而不是 localhost 。在这里, localhost 表示Windows主机,而不是我们打算在端口8080上击中的虚拟机。

On Windows, Linux containers are created inside a virtual machine that runs on Windows host OS. This virtual machine gets assigned an IP. While doing the curl, you should use this IP instead of localhost. Here, localhost means the Windows host and not the virtual machine that we intend to hit on the port 8080.

要知道分配给虚拟机的IP,请运行 docker-machine ls 命令。您将获得类似于以下内容的输出:

To know the IP assigned to the virtual machine, run the docker-machine ls command. You will get output similar to the following:

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v18.05.0-ce

在上述命令输出中的 URL 下记录IP,在您的计算机上运行该命令时,它将是一个不同的IP。然后使用它进行卷曲:

Note the IP in the above command output under URL -- it would be a different IP when you run the command on your machine. Then use it to do the curl:

curl -i 192.168.99.100:8080

这篇关于Docker端口映射在Windows 10上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:25