7

docker コンテナー内に単純な hello world express.js アプリケーションがあります。ポート 8080 で実行するように設定されており、docker ファイルはこのポートをイメージで公開します。さらに、イメージを実行するときにポートを公開します。しかし、単純な curl リクエストを作成しようとすると、接続が拒否されます。このテストのセットアップ方法は次のとおりです。

私のDockerfileは非常に単純です。

FROM node

ADD ./src /src
WORKDIR /src

# install your application's dependencies
RUN npm install

# replace this with your application's default port
EXPOSE 8080

# replace this with your main "server" script file
CMD [ "node", "server.js" ]

./srcディレクトリ内には、次のようなserver.jsファイルがあります。

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

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

var server = app.listen(8080, function() {
    console.log('Listening on port %d', server.address().port);
});

次のような基本的なpackage.jsonと同様に:

{
  "name": "hello-world",
  "description": "hello world test app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "4.7.2"
  }
}

イメージは問題なくビルドされます。

→ docker build -t jimjeffers/hello-world .
Sending build context to Docker daemon 1.126 MB
Sending build context to Docker daemon 
Step 0 : FROM node
 ---> 6a8a9894567d
Step 1 : ADD ./src /src
 ---> 753466503fbf
Removing intermediate container 135dab70dfff
Step 2 : WORKDIR /src
 ---> Running in 12257ff3f990
 ---> 010ce4140cdc
Removing intermediate container 12257ff3f990
Step 3 : RUN npm install
 ---> Running in 1a9a0eb9d188
 ---> 5dc97c79281e
Removing intermediate container 1a9a0eb9d188
Step 4 : EXPOSE 8080
 ---> Running in abbaadf8709d
 ---> 9ed540098ed2
Removing intermediate container abbaadf8709d
Step 5 : CMD [ "node", "server.js" ]
 ---> Running in 63b14b5581cd
 ---> eababd51b50e
Removing intermediate container 63b14b5581cd
Successfully built eababd51b50e

そして、問題なく開始します:

→ docker run -P -d jimjeffers/hello-world
ee5024d16a679c10131d23c1c336c163e9a6f4c4ebed94ad4d2a5a66a64bde1d

→ docker ps
CONTAINER ID        IMAGE                           COMMAND                CREATED             STATUS              PORTS                     NAMES
ee5024d16a67        jimjeffers/hello-world:latest   node server.js         About an hour ago   Up 11 seconds       0.0.0.0:49158->8080/tcp   jovial_engelbart    
5d43b2dee28d        mongo:2.6                       /usr/src/mongo/docke   5 hours ago         Up 3 hours          27017/tcp                 some-mongo          

サーバーがコンテナー内で実行されていることを確認できます。

→ docker logs ee5024d16a67
Listening on port 8080

しかし、リクエストをしようとすると、接続が拒否されます。

→ curl -i 0.0.0.0:49158
curl: (7) Failed connect to 0.0.0.0:49158; Connection refused

私がここに欠けているものはありますか?docker を使用せずにアプリケーションを実行すると、期待どおりに動作します。

→ node src/server.js 
Listening on port 8080

→ curl -i 0.0.0.0:8080
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
ETag: W/"b-1243066710"
Date: Mon, 04 Aug 2014 05:11:58 GMT
Connection: keep-alive

Hello World
4

2 に答える 2

0

dockerfile でポートを公開した後、Docker にホスト システムからコンテナーへのマッピング方法を指示する必要もあります。

Docker 実行コマンドを次のように変更してみてください。

docker run -p 127.0.0.1:49158:8080 -d jimjeffers/hello-world

次に試してください:

curl -i 127.0.0.1:49158

于 2014-08-04T05:41:28.930 に答える