27
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dockerでpm2

Last updated at Posted at 2014-07-03

pm2でNode.jsをプロセスを起動すると、pm2自身はすぐに終了してしまう。
なのでDockerのCMDでpm2を開始してもコンテナは停止してしまう。

こまったーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー。

全然困らなかった。

pm2に --no-daemon というオプションが追加されていたので、それでOKでした。

Dockerfileやら何やら

Dockerfile

FROM node

WORKDIR /nodeapp

ADD package.json /nodeapp/
ADD index.js /nodeapp/

RUN npm install

EXPOSE  1337

CMD ["node_modules/pm2/bin/pm2", "--no-daemon", "start", "index.js"]

package.json

{
  "name": "docker-pm2",
  "version": "0.0.1",
  "description": "Docker pm2 example",
  "main": "index.js",
  "author": "5t111111",
  "dependencies": {
    "pm2": "latest"
  }
}

index.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337);

起動

$ docker build .
$ docker run -P -d <イメージID>
$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
f89174a4ac15        392c405d84f6        node_modules/pm2/bin   48 seconds ago      Up 47 seconds       0.0.0.0:49169->1337/tcp   sad_shockley
$ curl -i localhost:49169
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Thu, 03 Jul 2014 14:57:45 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World
27
23
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?