LoginSignup
14
13

More than 5 years have passed since last update.

Dockerfileのコーディング規約を考えてみた

Last updated at Posted at 2016-04-27

Dockerのコーディング規約はあるけど、Dockerfileのものはなくて、公式サイトのサンプルを見比べても決まったルールがなさそうだったので自分用のルールをまとめた。

インデント

  • スペース2つ(タブ文字は使わない)
RUN apt-get update && apt-get install -y \
  bzr \
  cvs \
  git \
  mercurial \
  subversion

コメント

  • 書かなくてもいいけど分かりにくいなと思ったら書く
# Jenkins home directory is a volume, so configuration and build history 
# can be persisted and survive image upgrades
VOLUME /var/jenkins_home

改行

  • 処理のまとまり毎に入れる
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"

WORKDIR $GOPATH

RUNコマンドの連結

  • 行末は \
  • 改行後は2つスペースを空けて & で始める
RUN groupadd -g ${gid} ${group} \
  && useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}

ABC順にする

  • インストールするモジュールを複数書く場合、同じものを間違えて指定しないように
RUN apt-get update && apt-get install -y \
  bzr \
  cvs \
  git \
  mercurial \
  subversion

ADDは使わない

  • COPYを使おう
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY . /tmp/

ベストプラクティス

コーディング規約じゃないけど

英語
https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
日本語
http://docs.docker.jp/engine/articles/dockerfile_best-practice.html

14
13
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
14
13