2

I have created sample docker image. When I try to run the image it is displaying running on http://0.0.0.0:8000 but it is not running actually in the localhost

How to resolve that issue?

Here Is my docker file:

     FROM node:carbon

# Create app directory
WORKDIR C:\Users\user2\FirstDocker

# 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 8000
CMD [ "npm", "start" ]

Server.js

'use strict';

const express = require('express');

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

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

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

package.json

{
  "name": "DockerApplication",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Nithya <[email protected]>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

when I run the image:

docker run nitikishu/sampledocker output:

 dockerapplication!1.0.0 start /C:usersuser2FirstDocker
 node server.js
 Running on http://0.0.0.0:8000

But that site is not reached in localhost:8000

Docker command to run an image docker command

I have used this command to run and check the host both 8000 and 8001 is not reaching

2
  • Unless you specify a fixed port mapping when starting the container, the :8000 of the container will be assigned to a random port on the host. Check docker ps to see where it goes to. Commented Jan 8, 2018 at 11:33
  • docker ps gives the image port is 0.0.0.0:800->8000/tcp Commented Jan 9, 2018 at 7:17

1 Answer 1

3

You can start your container by specifying a port mapping

docker run -p 8001:8000 <image-name>.

Then is the port 8000 of your container mapped to the port 8001 of your local machine.

So that you can access it through localhost:8001

See also the docker run reference for more details.

Sign up to request clarification or add additional context in comments.

7 Comments

You can also use docker run -p 8000 image-name to expose it on a random port and and afterwards use docker port image-name to get the port that was used.
@DanielEder indeed. but you have to use the container-name (docker port container) then.
True. That's what I meant :)
still i am getting same problem. site cannot be reached @Jehof
Yes @Jehof. I attached the command prompt image with the commands which i used to run the application
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.