LoginSignup
1
1

More than 1 year has passed since last update.

Nodejsのコンテナを作成してみた。

Last updated at Posted at 2020-11-26

はじめに

Nodejsのバックエンドサーバーをコンテナで作成する際、ベースイメージをnodejs(node:10) にしてDockerfileを作成したが、コンテナにアタッチができず、中の構成などを直接見る方法が分からなかった。
困ったときにはnodejsの設定などを見れるようにしたかったので、試しにUbuntuベースにnodejsをインストールする方法でDockerfileを作成してみた。
(記事の内容は、試しに動かすところまで記載あり。)

実行環境(前提)

【Docker導入環境】
  ・Ubuntu 20.04 LTS(GCP上)
  ・docker 19.03.13

今回やった事のメモ

今回の作業のためにテストフォルダを作成して、そこに移動。

$ sudo mkdir ./test_container
$ cd ./test_container

Dockerfileの作成

$ sudo nano ./Dockerfile
Dockerfile
# ベースイメージ
FROM ubuntu:20.04

# 必要パッケージのインストール
RUN apt update
RUN apt install -y tzdata
RUN apt install -y \
  nodejs \
  npm

# Nodejs関連のパッケージインストール
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json ./
RUN npm install

# index.jsファイルの設置
COPY ./index.js ./

# ポート開放
EXPOSE 8080

CMD ["node", "index.js"]

nodejsモジュールをインストールするためのpackage.jsonを作成
※上記のnpm installのタイミングで、package.jsonの情報を元にインストールが行われる。

$ sudo nano ./package.json
package.json
{
  "name": "test",
  "description": "test",
  "version": "0.0.1",
  "main": "index.js",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "10"
  },

  "dependencies": {
    "express": "^4.17.1",
    "moment-timezone": "^0.5.31",
    "body-parser": "^1.19.0"
  }
}

index.jsを作成

$ sudo nano ./index.js
index.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 8080;


app.use(bodyParser.urlencoded({ extended: true }));

app.get('/get_test1', function(req, res) {
  res.send('GET1パラメータ取得: ' + req.query.get1)
});

app.get('/get_test2', function(req, res) {
  res.send('GET2パラメータ取得: ' + req.query.get2)
});

app.post('/post_test1', function(req, res) {
  res.send('POST-URLへの送信です。')
});

app.post('/post_test2', function(req, res) {
  res.send('POSTパラメータ取得: ' + req.body.data1)
});

app.listen(port)

ビルド実行

$ docker image build -t test_container:v1 ./

コンテナ作成 & 起動

$ docker container run -it -d -p 80:8080 --name con1 test_container:v1

動作確認

GETの動きを確認
以下に接続して、上記で設定したレスポンスが返ってくればOK。

 外部IP/get_test1?get1=10
 外部IP/get_test2?get2=20

POSTの動きを確認
ローカル環境(自分のPC)に適当なhtmlファイルを作成して、それをブラウザで開きアクセス。

post_test1.html(POST送信のテスト用)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>POSTテスト用</title>
</head>

<body>

<form action="http://[外部IP]/post_tset" method="post">
  <input type="hidden" name="data1" value="aiueo">
  <input type="submit" value="送信">
</form>

</body>
</html>
post_test2.html(POSTパラメータ取得の確認用)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>POSTテスト用</title>
</head>

<body>

<form action="http://[外部IP]/post_tset2" method="post">
  <input type="hidden" name="data1" value="aiueo">
  <input type="submit" value="送信">
</form>

</body>
</html>

おまけ

はじめの背景の部分で 『コンテナにアタッチができず、中の構成などを直接見る方法が分からなかった』 と書いたが、コンテナ作成時のシェルコマンドで [CMD] のコマンドを /bin/bash で上書きできていなかっただけだった・・・

1
1
0

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
1
1