我很難嘗試使用vagrant託管的Docker容器中運行簡單的節點應用程序。運行節點應用程序的Vagrantfile/Dockerfile的簡單示例
此頁介紹的基本方法:https://www.vagrantup.com/blog/feature-preview-vagrant-1-6-docker-dev-environments.html
什麼我不能做的就是從我的計算機訪問節點的應用程序 - 在理論上我相信我應該能夠看到我的「Hello World」式的節點/快遞應用在localhost:8181
...
下面是我到目前爲止有:
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.build_dir = "."
d.ports = ["8080:5000"]
# Is this necessary if EXPOSE is used in Dockerfile?
d.expose = ["5000"]
d.remains_running = true
d.volumes = ["/shared"]
end
config.vm.network "forwarded_port", guest: 8080, host: 8181
config.vm.synced_folder "~/Documents/shared", "/shared"
end
Dockerf ile:
# DOCKER-VERSION 0.8.0
FROM centos:6.4
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RUN yum install -y npm ImageMagick
ADD . /app
# Not necessary because node_modules are bundled
# RUN cd /app; npm install
EXPOSE 5000
CMD ["node", "/app/index.js"]
我已經按照儘可能接近的文檔,但似乎無法實現我所追求的。有任何想法嗎?
index.js:
var express = require('express'),
http = require('http');
var app = express();
var port = 5000;
app.get('/', function(req, res) {
res.send('Hello World');
});
var server = http.createServer(app);
server.on('listening', function() {
console.log('Express server listening on port ' + port);
});
server.listen(port, '0.0.0.0');
setInterval(function(){
console.log('running');
}, 5000);
的package.json
注:正常運行,即$ node index
,並提前在localhost:5000
感謝訪問
編輯在節點應用工作:
{
"name": "vagrant-docker-node",
"version": "0.0.0",
"dependencies": {
"express": "^4.1.0"
},
"main": "index.js"
}
感謝監聽TCP套接字生效,我已經嘗試了所有端口:(每個組合我都可以想出來,雖然我在任何地方都找不到任何新版本的例子, – homerjam