N15
N15

Reputation: 305

How to run the image /container in docker

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

Upvotes: 2

Views: 252

Answers (1)

Jehof
Jehof

Reputation: 35544

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.

Upvotes: 3

Related Questions