Dockerファイル:【ファイルなし】でbuildを実行
はじめに
- Dockerファイルとは
- イメージ(Dockerイメージ)作成に必要なファイル
- イメージとして構築する環境を記載する
- docker buildコマンド実行時にDockerデーモンが参照する
- ポイント
- Dockerファイル作成が基本
- Dockerファイルに関する理解は必要
- Dockerファイルなしは簡易実行と理解して使う
- 参考
- Dockerファイルとは
方法2つ
①echoのエスケープシーケンス利用
echo -e 'FROM busybox\nRUN echo "hello world"' | sudo docker build -t myimage:latest -
sudo docker images
$ echo -e 'FROM busybox\nRUN echo "hello world"' | sudo docker build -t myimage:latest - Sending build context to Docker daemon 2.048kB Step 1/2 : FROM busybox latest: Pulling from library/busybox 009932687766: Pull complete Digest: sha256:afcc7f1ac1b49db317a7196c902e61c6c3c4607d63599ee1a82d702d249a0ccb Status: Downloaded newer image for busybox:latest ---> ec3f0931a6e6 Step 2/2 : RUN echo "hello world" ---> Running in 5569f7d2f3cc hello world Removing intermediate container 5569f7d2f3cc ---> cdd578207988 Successfully built cdd578207988 Successfully tagged myimage:latest $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE myimage latest cdd578207988 1 second ago 1.24MB busybox latest ec3f0931a6e6 13 days ago 1.24MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB ubuntu 12.04 5b117edd0b76 4 years ago 104MB dockersamples/static-site latest f589ccde7957 5 years ago 191MB
- echo -e xxx
- エスケープシーケンスでecho文を実行
- 「\n」(改行コード)が使えるようになる
- 「 | 」
- Linuxのパイプ記号
- echo文をdocker buildに渡す
- docker build
- イメージ作成コマンド
- FROM ……「busybox」がベースイメージ
- RUN ……「echo -e xxx」を実行する
- -t
- タグ指定のオプション
- 「myimage:latest」を指定
- 「-」
- Dockerファイルなしの記号
- docker images
- 作成したイメージを一覧表示する
- REPOSITORY列で作成した「myimage」を確認
②ヒアドキュメント利用
- ①に続けて実行する場合は削除してから
- 「$ sudo docker rmi myimage」
sudo docker build -t myimage:latest -<<EOF
FROM busybox
RUN echo "hello world"
EOF
sudo docker images
$ sudo docker rmi myimage Untagged: myimage:latest Deleted: sha256:cdd5782079884891614770c39209dfe73aec8d52f53311078c4da184ef6f1f14 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest ec3f0931a6e6 13 days ago 1.24MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB ubuntu 12.04 5b117edd0b76 4 years ago 104MB dockersamples/static-site latest f589ccde7957 5 years ago 191MB $ sudo docker build -t myimage:latest -<<EOF > FROM busybox > RUN echo "hello world" > EOF Sending build context to Docker daemon 2.048kB Step 1/2 : FROM busybox ---> ec3f0931a6e6 Step 2/2 : RUN echo "hello world" ---> Running in 43c35880e08f hello world Removing intermediate container 43c35880e08f ---> 7f948f9d3cba Successfully built 7f948f9d3cba Successfully tagged myimage:latest $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE myimage latest 7f948f9d3cba 3 seconds ago 1.24MB busybox latest ec3f0931a6e6 13 days ago 1.24MB hello-world latest feb5d9fea6a5 4 months ago 13.3kB ubuntu 12.04 5b117edd0b76 4 years ago 104MB dockersamples/static-site latest f589ccde7957 5 years ago 191MB
- docker rmi myimage
- イメージの削除
- ①で作成した「myimage」の削除
- docker images
- イメージの確認
- docker build
- イメージを作成する
- -t
- タグ名のオプション
- 「myimage:latest」を指定
- 「-」
- Dockerファイルなしの記号
- 続けてヒアドキュメントを記述
- <<EOF
- ヒアドキュメントの開始(Linuxコマンド)
- EOFまでをひとまとまりをファイルと同等に読み込む
リファレンス
- Best practices for writing Dockerfiles | Docker Documentation
- Dockerファイルのベストプラクティス
