LoginSignup
20
21

More than 5 years have passed since last update.

Dockerfileでincludeを使う

Posted at

Dockerfileにincludeを使う方法

INCLUDE?

Dockerfileには定型文が続くので include 文があればまとまり単位で整理することが出来て良いのですが、現状ありません。
それに対する案としてGNU C PreProcessorを使う方法がこちらのコメントにあったので、備忘録として。

Makefileを作成

Dockerfile: Dockerfile.in *.docker
  cpp -P -o Dockerfile Dockerfile.in

build: Dockerfile
  sudo docker build --no-cache -t RepositoryName .
  • 2行目と5行目の先頭はハードタブである必要がありますので注意。

Dockerfile.in を作成

  • これはどんなファイル名でもいいですが、Makefileに合わせる必要があります。
  • 例えば以下のような感じで作成。
FROM debian:stable

MAINTAINER d6rkaiz

#include "env.docker"
#include "apt.docker"
#include "locale.docker"
#include "adduser.docker"
#include "sudo.docker"
#include "supervisor.docker"
#include "nginx.docker"
#include "cleanup.docker"

EXPOSE 80
CMD /usr/bin/supervisord

*.docker作成

  • 上記の例で言えば、env.dockerやnginx.dockerなどを区切りの良いところで作成していきます。
  • 私は apt.docker を以下のような感じで作成しています。
ADD conf/apt/sources.list /etc/apt/
RUN apt-get update -qq
RUN apt-get upgrade -qqy
RUN apt-get install -qqy locales openssh-server ca-certificates wget sudo supervisor

Dockerfile生成

$ make

とすればDockerfileが作成されます。同様にイメージのビルドも同時に行なうには

$ make build

とすることでイメージのビルドも同時に実行するようになります。

制限事項など

Dockerfile生成に GNU C PreProcessor を使っているので、以下のような制限があります。

  • // はコメントとして解釈されるのでURLを記載する場合にはシングルクォートかダブルクォートで囲む必要があります。
  • 翻って#はコメントと見なされません。#include, #if, #define 等以外のcppで解釈できない文言を入れた場合にはエラーとなります。
20
21
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
20
21