Dockerfileを使ってみた
新たなimageを作ってみた。
前提
参考
- https://qiita.com/nl0_blu/items/1de829288db2670276e8
- https://qiita.com/pottava/items/452bf80e334bc1fee69a
- http://docs.docker.jp/engine/articles/dockerfile_best-practice.html
方法
- Dockerfileファイル作成
- 1からimageファイルを
build
する - 2のimageを
run
する
手順
1. Dockerfileファイル作成
$ leafpad Dockerfile
# 元image FROM resin/rpi-raspbian # docker buildするときに実行される RUN echo "now building..." # CMD: docker runするときに実行される CMD echo "now running..."
設定する内容はおおよそ以下。
- aptミラーサーバ設定
- スワップ無効化
- RAMディスク化
- update
- 日本語化
テキストファイル/tmp/work/docker/Dockerfile
として以下を作成した。なお、ディレクトリ配下にはDockerfile
だけを配置すること。
FROM resin/rpi-raspbian MAINTAINER ytyaru RUN sudo sh -c "echo 'deb http://ftp.jaist.ac.jp/raspbian stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ stretch main ui' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ \ && sudo swapoff --all \ && sudo systemctl stop dphys-swapfile \ && sudo systemctl disable dphys-swapfile \ \ && sudo sh -c "echo 'tmpfs /tmp tmpfs defaults,size=500m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /home/pi/.cache/chromium/Default/ tmpfs defaults,size=768m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /home/pi/.cache/lxsession/LXDE-pi tmpfs defaults,size=1m,noatime,mode=1777 0 0' >> /etc/fstab" \ \ && sudo apt -y update \ && sudo apt -y upgrade \ && sudo apt -y dist-upgrade && apt install -y \ fonts-ipafont \ fonts-ipaexfont \ fcitx-mozc \ && apt-get clean \ && rm -rf /var/lib/apt/lists/*
- 前のコマンドを次のコマンドに反映させるために
&&
で繋ぐ - スクリプトを改行で見やすくするために
\
で区切る
以下、個別に説明。
A. aptミラーサーバ設定
sudo sh -c "echo 'deb http://ftp.jaist.ac.jp/raspbian stretch main contrib non-free rpi' >> /etc/apt/sources.list" sudo sh -c "echo 'deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" sudo sh -c "echo 'deb http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" sudo sh -c "echo 'deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ stretch main ui' >> /etc/apt/sources.list" sudo sh -c "echo 'deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list"
RUN sudo sh -c "echo 'AAA' >> /etc/apt/sources.list"
B. スワップ無効化
sudo swapoff --all sudo systemctl stop dphys-swapfile sudo systemctl disable dphys-swapfile
C. RAMディスク化
sudo sh -c "echo 'tmpfs /tmp tmpfs defaults,size=500m,noatime,mode=1777 0 0' >> /etc/fstab" sudo sh -c "echo 'tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0' >> /etc/fstab" sudo sh -c "echo 'tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755 0 0' >> /etc/fstab" sudo sh -c "echo 'tmpfs /home/pi/.cache/chromium/Default/ tmpfs defaults,size=768m,noatime,mode=1777 0 0' >> /etc/fstab" sudo sh -c "echo 'tmpfs /home/pi/.cache/lxsession/LXDE-pi tmpfs defaults,size=1m,noatime,mode=1777 0 0' >> /etc/fstab"
以下のディレクトリがRAMディスク化される。
/tmp
/var/tmp
/var/log
/home/pi/.cache/chromium/Default/
/home/pi/.cache/lxsession/LXDE-pi
なお、ログ出力を抑制するのはコメントアウトする位置の確認を自動化するのが難しいため断念。
D. update
sudo apt-get -y update sudo apt-get -y upgrade sudo apt-get -y dist-upgrade
E. 日本語化
sudo apt-get install fonts-ipafont fonts-ipaexfont sudo apt-get install fcitx-mozc
2. imageファイルをbuild
する
カレントディレクトリにあるDockerfile
からイメージファイルを作成する。
$ cd /tmp $ sudo docker build -t ytyaru/raspbian-setup .
(Dockerファイルパスを指定したいときは-f
引数を使う)
$ docker build -t -f /tmp/Dockerfile ytyaru/raspbian-setup .
エラー
$ sudo docker build -t ytyaru/raspbian-setup . ERRO[0001] Can't add file /tmp/.X11-unix/X0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.1 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/fcitx-socket-:0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/ssh-ASuIuIrDJGWn/agent.622 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/ssh-wXSY9AP4d3Va/agent.971 to tar: archive/tar: sockets not supported Sending build context to Docker daemon 9.728kB Step 1/3 : FROM resin/rpi-raspbian latest: Pulling from resin/rpi-raspbian 9cf525b21ed2: Extracting 44.04MB/49.29MB cd1edca1e5aa: Download complete c301ba35a4bd: Download complete f33eacc968f4: Download complete 1cdf1ce1a4f4: Download complete 4ceedda90347: Download complete 1b3bd148f0b5: Download complete c24944d33051: Download complete ca03646cb11b: Download complete aa165c0852d1: Download complete 140461433c0f: Download complete
pi@raspberrypi:/tmp $ sudo docker build -t ytyaru/raspbian-setup . ERRO[0001] Can't add file /tmp/.X11-unix/X0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.1 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/fcitx-socket-:0 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/ssh-ASuIuIrDJGWn/agent.622 to tar: archive/tar: sockets not supported ERRO[0001] Can't add file /tmp/ssh-wXSY9AP4d3Va/agent.971 to tar: archive/tar: sockets not supported Sending build context to Docker daemon 9.728kB Step 1/3 : FROM resin/rpi-raspbian latest: Pulling from resin/rpi-raspbian 9cf525b21ed2: Pull complete cd1edca1e5aa: Pull complete c301ba35a4bd: Pull complete f33eacc968f4: Pull complete 1cdf1ce1a4f4: Pull complete 4ceedda90347: Pull complete 1b3bd148f0b5: Pull complete c24944d33051: Pull complete ca03646cb11b: Pull complete aa165c0852d1: Pull complete 140461433c0f: Pull complete Digest: sha256:55a63d7d7678cf35ecaf289902f67a546d202b9250217ccaa915012e7e94d4e0 Status: Downloaded newer image for resin/rpi-raspbian:latest ---> 365783567b86 Step 2/3 : MAINTAINER ytyaru ---> Running in 14c8a5ccd732 Removing intermediate container 14c8a5ccd732 ---> c051127fb087 Step 3/3 : RUN sudo sh -c "echo 'deb http://ftp.jaist.ac.jp/raspbian stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ stretch main ui' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo swapoff --all && sudo systemctl stop dphys-swapfile && sudo systemctl disable dphys-swapfile && sudo sh -c "echo 'tmpfs /tmp tmpfs defaults,size=500m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /home/pi/.cache/chromium/Default/ tmpfs defaults,size=768m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /home/pi/.cache/lxsession/LXDE-pi tmpfs defaults,size=1m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo apt -y update && sudo apt -y upgrade && sudo apt -y dist-upgrade && apt install -y fonts-ipafont fonts-ipaexfont fcitx-mozc && apt-get clean && rm -rf /var/lib/apt/lists/* ---> Running in 1ab9056f10b5 Failed to get D-Bus connection: Unknown error -1 The command '/bin/sh -c sudo sh -c "echo 'deb http://ftp.jaist.ac.jp/raspbian stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ stretch main ui' >> /etc/apt/sources.list" && sudo sh -c "echo 'deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" && sudo swapoff --all && sudo systemctl stop dphys-swapfile && sudo systemctl disable dphys-swapfile && sudo sh -c "echo 'tmpfs /tmp tmpfs defaults,size=500m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /home/pi/.cache/chromium/Default/ tmpfs defaults,size=768m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo sh -c "echo 'tmpfs /home/pi/.cache/lxsession/LXDE-pi tmpfs defaults,size=1m,noatime,mode=1777 0 0' >> /etc/fstab" && sudo apt -y update && sudo apt -y upgrade && sudo apt -y dist-upgrade && apt install -y fonts-ipafont fonts-ipaexfont fcitx-mozc && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 1
失敗の原因がわからない。ネットワーク設定ができていないからupdate
やinstall
ができないのか?
ERRO[0001] Can't add file ... to tar: archive/tar: sockets not supported
Failed to get D-Bus connection: Unknown error -1
image確認するとそれらしきものがc051127fb087
として存在している。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> c051127fb087 3 minutes ago 126MB resin/rpi-raspbian stretch 208686d17787 4 weeks ago 137MB resin/rpi-raspbian latest 365783567b86 4 weeks ago 126MB
container確認するとそれらしきものが1ab9056f10b5
として存在している。
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ab9056f10b5 c051127fb087 "/bin/sh -c 'sudo sh…" 3 minutes ago Exited (1) 19 minutes ago quizzical_mccarthy 2501191ef1aa resin/rpi-raspbian:stretch "/usr/bin/entry.sh /…" 18 hours ago Exited (255) 15 hours ago rpi0
container削除。
$ docker rm 1ab9056f10b5 1ab9056f10b5
image削除。
$ docker rmi c051127fb087 Deleted: sha256:c051127fb087dd12d4707df26e253559db19e6ec546d195b6bc710e6f1d10aae
以下の内容のみだと成功した。 Dockerfile
FROM resin/rpi-raspbian MAINTAINER ytyaru
$ sudo docker build -t ytyaru/raspbian-setup . ERRO[0000] Can't add file /tmp/.X11-unix/X0 to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/.org.chromium.Chromium.fFejpa/SingletonSocket to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.0 to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/dhcpcd-pi/libdhcpcd-wpa-960.1 to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/fcitx-socket-:0 to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/ssh-ASuIuIrDJGWn/agent.622 to tar: archive/tar: sockets not supported ERRO[0000] Can't add file /tmp/ssh-wXSY9AP4d3Va/agent.971 to tar: archive/tar: sockets not supported Sending build context to Docker daemon 11.78kB Step 1/2 : FROM resin/rpi-raspbian ---> 365783567b86 Step 2/2 : MAINTAINER ytyaru ---> Running in 4a456409d0fd Removing intermediate container 4a456409d0fd ---> 7bac6937a865 Successfully built 7bac6937a865 Successfully tagged ytyaru/raspbian-setup:latest
以下の内容で成功する。原因はスワップ停止コマンドだった。
FROM resin/rpi-raspbian MAINTAINER ytyaru RUN sudo sh -c "echo 'deb http://ftp.jaist.ac.jp/raspbian stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ stretch main ui' >> /etc/apt/sources.list" \ && sudo sh -c "echo 'deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi' >> /etc/apt/sources.list" \ \ && sudo swapoff --all \ && sudo systemctl stop dphys-swapfile \ && sudo systemctl disable dphys-swapfile \ \ && sudo sh -c "echo 'tmpfs /tmp tmpfs defaults,size=500m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /home/pi/.cache/chromium/Default/ tmpfs defaults,size=768m,noatime,mode=1777 0 0' >> /etc/fstab" \ && sudo sh -c "echo 'tmpfs /home/pi/.cache/lxsession/LXDE-pi tmpfs defaults,size=1m,noatime,mode=1777 0 0' >> /etc/fstab" \ \ && sudo apt -y update \ && sudo apt -y upgrade \ && sudo apt -y dist-upgrade && apt install -y \ fonts-ipafont \ fonts-ipaexfont \ fcitx-mozc \ && apt-get clean \ && rm -rf /var/lib/apt/lists/*
また、apt-get
でなくapt
にすると以下エラーが出た。
WARNING: apt does not have a stable CLI interface yet. Use with caution in scripts.
image, containerを確認してみる。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ytyaru/raspbian-setup latest 80f26f994320 2 minutes ago 405MB sh latest 225dd19d172a 17 hours ago 137MB resin/rpi-raspbian stretch 208686d17787 4 weeks ago 137MB resin/rpi-raspbian latest 365783567b86 4 weeks ago 126MB
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ff03c4e3cdc da4c5bde5a07 "/bin/sh -c 'sudo sh…" About an hour ago Exited (0) 6 minutes ago unruffled_banach 798ffdc4ac2f resin/rpi-raspbian:stretch "/usr/bin/entry.sh /…" 17 hours ago Exited (0) 17 hours ago rpi-gitea de1e92f38a93 resin/rpi-raspbian:stretch "/usr/bin/entry.sh /…" 19 hours ago Exited (165) 18 hours ago keen_wing 2501191ef1aa resin/rpi-raspbian:stretch "/usr/bin/entry.sh /…" 20 hours ago Exited (255) 16 hours ago rpi0
3. imageをrun
する
$ sudo docker run ytyaru/raspbian-setup