javascript - Docker运行,无法访问应用程序

标签 javascript node.js docker

我正在构建一个在端口 3000 上运行的 Express (nodejs) 应用程序,带有一个简单的 hello world,并连接到公共(public) github 存储库。

实际上,它效果很好,代码如下所示:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

我正在研究 docker,我想让这个小代码在容器内工作,所以我创建了这个 dockerfile :

FROM phusion/baseimage:0.9.17

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# Install corresponding packages
# Git, pm2, curl
RUN apt-get update && apt-get install -y \
  git \
  curl

# Downloading NodeJs v0.12
RUN curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
RUN apt-get install -y nodejs

# Cloning repository
RUN git clone https://github.com/User/hello-world.git

# Install global dependencies
RUN npm install -g pm2

# Move inside of the project
RUN cd hello-world && npm install  && pm2 start app.js


# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

然后我运行这些命令:

docker build -t hello-world
docker run -p 3000:3000

在此之后(一切都成功),当我尝试访问http://localhost:3000/时, 页面未找到。

我需要一些帮助来理解我的错误。

谢谢大家

最佳答案

您应该登录到正在运行的容器:

docker exec -it <container-id> /bin/bash

进入容器后,您可以在那里运行应用程序并直接在容器内进行调试。

类似于:

cd your-directory/hello-world

然后:

node app.js

控制台会像您本地的开发环境一样吐出错误。

关于javascript - Docker运行,无法访问应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283575/

相关文章:

javascript - NodeJS parseStream,定义一个 block 的起点和终点

docker - 容器化应用程序 (Docker) 和 Jenkins Slave CICD

javascript - 如何替换div中的数字?

javascript - 检查[i]是否存在

javascript - 无法加载资源: the server responded with a status of 404 (Not Found) in koa2 & nodejs

php - 使用 docker-compose 保持容器事件并链接

docker - 环境属性未注入(inject)到卷映射

javascript - 在 contenteditable div 中用 html 替换某些最后输入的单词

javascript - Jquery 选择前一个元素与类

javascript - 如何在新行中将 JS 对象推送到 JSON?