LoginSignup
22
18

More than 3 years have passed since last update.

Vue.jsプロジェクトにおけるnginxの設定とDockerによるコンテナ化の例

Posted at

概要

Vue.jsで作成したプロジェクトをvue-cliでビルドし,得られるHTMLファイル等をDockerコンテナ化する例を記載する。
今回は、Docker for Macで作成したイメージを、docker runでコンテナ化しhttp://localhost:80 でアクセスできるところまで説明する。

git: k-washi/example-vue-cli

DockerによるBuildとコンテナ化

Vueで作成されたプロジェクトに対して、以下のようなDockerfileにより、イメージを作成できる。
処理は、以下の通りである。

  1. ホストに置かれたnpmのパッケージファイルをコピーし、使用するモジュールをインストール
  2. ビルド
  3. コンテナ軽量化のため、イメージ化するステージを移動
  4. ビルドした環境から、ビルドした結果のファイル(/app/dist配下)をコピー
  5. ホストからnginxの設定ファイルをコピー
  6. 公開するポートを設定
  7. コンテナ化した際のプロセスを設定
# ビルド環境
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# 本番環境
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html

COPY nginx_config/nginx.conf /etc/nginx/nginx.conf
COPY nginx_config/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

以下のコマンドでDockerをイメージを作成し、その後、コンテナ化できる。

docker build -t kwashizaki/example-vue-cli:v1.0.0 .
docker run -it -p 80:80 --rm --name example-vue-cli kwashizaki/example-vue-cli:v1.0.0

nginxの設定

distの配下に、以下のような構成でHTML, CSS, JS, 画像ファイルが置かれていると仮定する。


# tree
├── css
│   └── chunk-vendors.58db5b44.css
├── img
│   └── logo.63a7d78d.svg
├── index.html
└── js
    ├── about.602f5095.js
    ├── about.602f5095.js.map
    ...

上記のDockerfileでコピーしていた、nginxの設定ファイルは以下は以下のように設定している。
httpに設定したincludeで設定したパスにあるdefault.confでルーティングを設定している。
ルーティングは過剰な設定となっているが、以下で説明する。

  • indexディレクティブ
    URIが"/"で終わっているときにインデックスとして扱われるファイル名を設定する。
    index ファイル名 [ファイル名 ...]

  • try_files ファイルの存在チェック
    指定したファイルやディレクトリの存在を順番に調べ、存在しなかった場合、最後に記述したパスに内部リダイレクトする。
    try_files ファイル ... パス;

  • error_page エラーページ
    error_pageディレクティブには指定したエラーコードが発生したときに表示するページのURIを指定する。

  • location = /
    = で完全一致のURIをルーティングする

  • location /
    一致する条件がなかった場合、最後に処理される。

  • location ~ ^/css/*.(css)$
    ^/css/で/css/から始まるURIをルーティング。(css)$で後方一致。

  • location ~ ^/[^/].(css)$
    [^/]は補集合で、[^/]
    .(css)$は、/を含まない.cssファイルに後方マッチするURIをルーティングする。
    もし、/css/*.cssのようなURIの場合、^/css/を持つ方のルーティングが優先されるため、こちらは選択されない。

プレフィックス 説明
= 完全一致
~ 正規表現(大文字・小文字を区別する)
~* 正規表現(大文字・小文字を区別しない)
^~ 前方一致(正規表現より優先)
なし 前方一致 正規表現の方が優先
nginx.conf

user nginx;
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;

}
default.conf
server {
  listen 80;

  index index.html index.htm;
  location = / {
    root /usr/share/nginx/html;
  }

  location / {
    root /usr/share/nginx/html;
    try_files $uri $uri/ /index.html;
  }

  # redirect server error pages to the static page /50x.html
        #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   html;
  }
  location ~ ^/css/*\.(css)$ {
    root /usr/share/nginx/html;

  }
  location ~ ^/[^/]*\.(css)$ {
    root /usr/share/nginx/html/css;

  }

  location ~ ^/js/*\.(js)$ {
    root /usr/share/nginx/html;

  }
  location ~ ^/[^/]*\.(js)$ {
    root /usr/share/nginx/html/js;

  }

  location ~ ^/img/*\.(jpg|jpeg|png|gif|ico|woff|woff2|ttf)$ {
    root /usr/share/nginx/html;

  }
  location ~ ^/[^/]*\.(jpg|jpeg|png|gif|ico|woff|woff2|ttf)$ {
    root /usr/share/nginx/html/img;

  }

}

Appendix

Dockerのイメージ作成時にビルドした場合、ビルドに時間がかかることが多い。開発環境でビルドし、ビルドされたhtmlファイルなどをコピーすることでビルド時間を短縮する方法を簡単に説明する。

開発環境でVue.jsプロジェクトをビルドして、Dockerイメージを作成する。ここでは、以下に記載したDockerfileを指定している。

npm run build

docker build -t kwashizaki/example-vue-cli:v1.0.0 -f ./DockerfileNoBuild .
DockerfileNobuild
# 本番環境
FROM nginx:stable-alpine as production-stage

COPY nginx_config/nginx.conf /etc/nginx/nginx.conf
COPY nginx_config/default.conf /etc/nginx/conf.d/default.conf
COPY dist/ /usr/share/nginx/html

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
22
18
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
22
18