実行中のドッカーコンテナにアクセスできないようです。最近のhello-world3は、ポート8080を使用するノードアプリケーションです。私はノードアプリケーションがprocess.env.PORT経由でそのポートでリッスンしています。私はPORT=8080
をnpm起動スクリプトで設定し、ドッカーファイルで8080を表示しました。コンテナをビルドしたら、ポートを指定します。この場合8082 by docker run -p 8082:8080 hello-world3
Windowsでdockerコンテナにアクセスできないlocalhost
私のコンソールからこのimgを見る私はlocalhostに行くことによって私のアプリの応答を見ることができるはずです:8082ええ?
私のドッキングウィンドウのファイル
FROM jkilbride/node-npm-alpine:8
WORKDIR /src
COPY package.json .
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm","start"]
package.json:
{
"name": "service",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"set PORT=8080 && node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
index.js:
const http = require('http');
const server = http.createServer((req,res) => {
const data = {
'data': 'Hello World',
'hostname': require('os').hostname()
};
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(data));
});
server.listen(process.env.PORT, (err) => {
if (err)
return console.log(err);
console.log('API is running on ' + process.env.PORT);
})