1
4

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-composeでNode.js環境構築で出たエラー

Last updated at Posted at 2018-05-30

インターン面接行ってきた

環境構築の話題になった。
Dockerを少し触っていたのだが、結構評判が良かったのでもっと勉強しようと思った。
ということでDockerを使ってNode.jsの環境構築をしてみる。

先人がいた

"docker-compose.ymlを使うdockerビルド方法"と使わずに"コマンドだけでビルドする方法"の二つがあるらしい。
上記はdocker-compose.ymlを使う方が面白そうだったので前者を選択。

docker-compose.ymlとDockerfileって何

両者ともに設定ファイルだけ渡されて、さあ環境構築してねということになり兼ねないので何をやっているのかを述べて行こうと思います。

自分でイメージをカスタマイズできる-Dockerfile

Dockerの公式サイトから、様々なフレームワークやプラットフォームのイメージをとってくるできますが、これはあくまでベースイメージです。このベースイメージに自分の必要なソフトウェア(例えば、Vimとか)をイメージ作成段階で設定できます。

以下のページにて非常に丁寧に載っています。
https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part2.html

Dockerfile
# ベースイメージを指定
# alpine は 軽量の linux OS
FROM node:8.9.4-alpine

# node.js の環境(今回は開発環境)
ENV NODE_ENV=development

# イメージのビルドに必要なコマンド
# 必要なソフトウェア
RUN npm install -g express-generator@4.16.0

# ディレクトリを移動する
WORKDIR /app

# ポート3000番を開放する
EXPOSE 3000

# vimの導入
RUN apk update
RUN apk add vim

軽量alpineではyumやapt-getは使えず、apkを用いることになるのでご注意ください。

Dockerfileのあるディレクトリでビルドしないといけないのではないか?と思いましたが、こちらではそうでもないらしい。

コンテナに関する設定ファイル-docker-compose.yml

Docker技術を用いる以上、コンテナ技術との関係性は不可欠ですね。コンテナとはここでいう環境となるので、環境に関する詳細の設定をしているものかと思います。

docker-compose.yml

# Docker-Compose ファイルフォーマットのバージョンは 3
version: '3'

services:
  webserver:
    build: node
    image: node-express-dev:1.0
    container_name: node
    tty: true
    volumes:
      - ./node/app:/app
    ports:
      - "8080:5000"

エラーが出た

上の記事に倣って環境構築していく。

Terminal
/app # express -f --view=pug app

   create : app/
   create : app/public/
   create : app/public/javascripts/
   create : app/public/images/
   create : app/public/stylesheets/
   create : app/public/stylesheets/style.css
   create : app/routes/
   create : app/routes/index.js
   create : app/routes/users.js
   create : app/views/
   create : app/views/error.pug
   create : app/views/index.pug
   create : app/views/layout.pug
   create : app/app.js
   create : app/package.json
   create : app/bin/
   create : app/bin/www

   change directory:
     $ cd app

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=app:* npm start

下はnpm installした時にエラー。
「package.jsonないぞ」みたいな警告。

Terminal
/app # npm install
npm WARN saveError ENOENT: no such file or directory, open '/app/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/app/package.json'
npm WARN app No description
npm WARN app No repository field.
npm WARN app No README data
npm WARN app No license field.

up to date in 0.093s
/app # npms start
/bin/sh: npms: not found
/app # npm start
npm ERR! path /app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-05-30T06_05_42_518Z-debug.log

やり直しが効くので2回目

Terminal
/app # express -f --view=pug /app

   create : /app/
   create : /app/public/
   create : /app/public/javascripts/
   create : /app/public/images/
   create : /app/public/stylesheets/
   create : /app/public/stylesheets/style.css
   create : /app/routes/
   create : /app/routes/index.js
   create : /app/routes/users.js
   create : /app/views/
   create : /app/views/error.pug
   create : /app/views/index.pug
   create : /app/views/layout.pug
   create : /app/app.js
   create : /app/package.json
   create : /app/bin/
   create : /app/bin/www

   change directory:
     $ cd /app

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=app:* npm start
Terminal
/app # npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
added 120 packages in 11.262s
/app # npm start

> app@0.0.0 start /app
> node ./bin/www

起動には成功したみたい。でもなぜ1回目で失敗して2回目で成功したのかはわからない...。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?