---
lang: ja-jp
breaks: true
---
# Node.js Webアプリを Dockerコンテナ化する Ubuntu 2021-04-30
> Node.js Web アプリケーションを Docker 化する
> https://nodejs.org/ja/docs/guides/nodejs-docker-webapp/
## テスト用のプログラム
#### package.json
```json=
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
```
#### server.js
```javascript=
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
```
## Dockerfile を作成する
```shell=
$ touch Dockerfile
```
```dockerfile=
FROM node:12
# アプリケーションディレクトリを作成する
WORKDIR /usr/src/app
# アプリケーションの依存関係をインストールする
# ワイルドカードを使用して、package.json と package-lock.json の両方が確実にコピーされるようにします。
# 可能であれば (npm@5+)
COPY package*.json ./
RUN npm install
# 本番用にコードを作成している場合
# RUN npm install --only=production
# アプリケーションのソースをバンドルする
COPY . .
# アプリケーションは 8080 ポートにバインドされているので EXPOSE 命令を使って docker デーモンによってマッピングされるでしょう:
EXPOSE 8080
# ここでサーバを起動するために node server.js を実行する 基本的な npm start を使います:
CMD [ "node", "server.js" ]
```
```shell=
$ touch .dockerignore
```
```shell=
$ cat .dockerignore
node_modules
npm-debug.log
```
## Docker イメージを構築
```shell=
$ docker build . -t <your username>/node-web-app
```
:::warning
:warning:
※"<your username>"は、DockreアカウントIDを設定する。
:::
```shell=
$ docker build . -t xxxxxxxx/node-web-app
Sending build context to Docker daemon 69.12kB
Step 1/7 : FROM node:12
12: Pulling from library/node
76b8ef87096f: Pull complete
2e2bafe8a0f4: Pull complete
b53ce1fd2746: Pull complete
84a8c1bd5887: Pull complete
7a803dc0b40f: Pull complete
b800e94e7303: Pull complete
a5e0c18be249: Pull complete
e0ba08bf689e: Pull complete
20f8d3e06d47: Pull complete
Digest: sha256:609103746810535f5a3a987a26ba4ce95d96225d28e9d6228faa5aa331980f37
Status: Downloaded newer image for node:12
---> 29be39bd6917
Step 2/7 : WORKDIR /usr/src/app
---> Running in 7dde341c526b
Removing intermediate container 7dde341c526b
---> e62820001687
Step 3/7 : COPY package*.json ./
---> cf4f4c82fc70
Step 4/7 : RUN npm install
---> Running in 118c253ca958
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN docker_web_app@1.0.0 No repository field.
npm WARN docker_web_app@1.0.0 No license field.
added 50 packages from 37 contributors and audited 50 packages in 1.974s
found 0 vulnerabilities
Removing intermediate container 118c253ca958
---> 28c620c7a3b5
Step 5/7 : COPY . .
---> fe89a71e9cf7
Step 6/7 : EXPOSE 8080
---> Running in 72e9c386cad7
Removing intermediate container 72e9c386cad7
---> 149071c37994
Step 7/7 : CMD [ "node", "server.js" ]
---> Running in 92bd913e7126
Removing intermediate container 92bd913e7126
---> 43f160ca0425
Successfully built 43f160ca0425
Successfully tagged xxxxxxxx/node-web-app:latest
```
## Dockerイメージの確認
```shell=
$ docker images
```
## イメージの実行
```shell=
$ docker run -p 49160:8080 -d xxxxxxxx/node-web-app
```
## 実行の確認
```shell=
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4d7e264efa7 xxxxxxxx/node-web-app "docker-entrypoint.s…" 10 seconds ago Up 9 seconds 0.0.0.0:49160->8080/tcp, :::49160->8080/tcp relaxed_matsumoto
```
## コンテナに入る
```shell=
$ docker exec -it c4d7e264efa7 /bin/bash
```
## Webアプリの確認
#### コンテナ内から確認
```shell=
# curl -i localhost:8080
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
Date: Mon, 03 May 2021 16:28:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
```
#### ホストから確認
```shell=
$ curl -i localhost:49160
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
Date: Mon, 03 May 2021 16:29:49 GMT
Connection: keep-alive
Keep-Alive: timeout=5
```
###### tags: `Node.js` `Webアプリ` `Docker` `Ubuntu`