如何在docker中运行图像/容器

时间:2018-01-08 11:31:28

标签: node.js docker

我创建了示例泊坞窗图像。当我尝试运行图像时,它显示在 http://0.0.0.0:8000 上运行,但它实际上并未在localhost中运行

如何解决该问题?

这是我的泊坞文件:

     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 <nitikishu@gmail.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}
运行图片时

docker run nitikishu / sampledocker 输出:

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

localhost:8000

中未找到该网站

运行图像的Docker命令 docker command

我已使用此命令运行并检查主机8000和8001未到达

1 个答案:

答案 0 :(得分:3)

您可以通过指定端口映射

来启动容器
  

docker run -p 8001:8000&lt; image-name&gt;。

然后是容器的端口8000映射到本地计算机的端口8001。

这样您就可以通过localhost:8001

访问它

有关更多详细信息,另请参阅docker run参考。