Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker port mapping is not working on windows 10

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.

like image 724
Jie Sun Avatar asked Jun 16 '18 19:06

Jie Sun


2 Answers

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)

like image 88
Ghasan غسان Avatar answered Oct 12 '22 15:10

Ghasan غسان


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
like image 23
Sanjeev Sachdev Avatar answered Oct 12 '22 15:10

Sanjeev Sachdev