Docker port mapping is not working on windows 10

佐手、 提交于 2020-08-02 17:48:46

问题


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.

Docker version - 18.03.0-ce, build 0520e24302

My simple app code:

'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}`);

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" ]

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

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


回答1:


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.

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

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



回答2:


It is 2020, and docker treats these commands differently:

docker run -p 3000:80 my_image (Correct format; port option has to come before image name.)

docker run my_image -p 3000:80 (Wrong format; docker will not notify you, and will silently fail port publishing.)

Hopefully this helps someone (or me, next time I work with docker xD)




回答3:


Your port mapping is 8080-8081 and you're trying to connect to localhost:8080 on your windows machine.

You need to go inside your docker container and curl to localhost:8081 or from your windows machine curl to your hostname:8080.

You can get your hostname in windows running

hostname
curl your_hostname:8080

You can get get inside your docker container using its id or name (on your picture it's called loving_hugle) and then curling to localhost

docker exec -it loving_hugle bash
curl localhost:8081


来源:https://stackoverflow.com/questions/50890981/docker-port-mapping-is-not-working-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!