LoginSignup
5
5

More than 5 years have passed since last update.

snippet: Dockerfile

Last updated at Posted at 2015-08-22

自分用

自分用コンテナ
docker run -it -d --name dev -h dev.local tukiyo3/ubuntu-dev
秘密鍵を取得
wget -o ~/.ssh/id_rsa.vagrant \
 https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant
~/.ssh/config
Host dev 
    User vagrant
    HostName dev.local
    IdentityFile ~/.ssh/id_rsa.vagrant

で接続ができる。

WARNING: Failed to contact D-Bus daemon.

  • コンテナ内でdbusサービスが起動していないとこのエラーが出る。
Dockerfile
CMD service ssh start &&\
    service dbus start &&\
    service avahi-daemon start &&\
    ip a | grep "inet " &&\
    tail -f -n0 /var/log/dmesg

apt-get install時の入力待ちを無効にする

DEBIAN_FRONTEND=noninteractive \
 apt-get install -y [packagename]

コンテナで/etc/rc2.d/S〜を実行させる

  • ubuntu 14.04で確認。
Dockerfile
CMD /etc/init.d/rc 2 && tail -f /var/log/dmesg

Dockerfile内で >を使う方法

Dockerfile
RUN (echo /etc/init.d/ssh start > /.bashrc)

CMD でipアドレスを表示

  • ip a s | grep "inet "
Dockerfile
FROM tukiyo3/ubuntu-debootstrap-ja:14.10

EXPOSE 22 80 443 8000 8080 3389 5901 5902
WORKDIR /workspace
VOLUME ["/workspace"]

CMD /etc/init.d/rc 2 && ip a s | grep "inet " && tail -f -n0 /var/log/dmesg

kitematicでEXECする時用にSHELL変数指定しておくと便利

Dockerfile
ENV SHELL /bin/bash

指定しておかないと /bin/sh が実行される様子

kitematic用にVOLUME指定しておくとmountできるので便利

Dockerfile
WORKDIR /workspace
VOLUME ["/workspace"]

スクリーンショット 2015-08-22 10.19.41.png

sshしたい

Githubから公開鍵を取得
RUN mkdir /root/.ssh && \
    (curl https://github.com/tukiyo.keys > /root/.ssh/authorized_keys) &&\
    chmod 600 /root/.ssh/authorized_keys
Vagrantの公開鍵を取得したい場合
(curl -q https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub > /root/.ssh/authorized_keys)
sshのポートを変更
RUN sed -i.org -e 's/Port 22/Port 10022/' /etc/ssh/sshd_config
--net host
docker run -it --net host イメージ
boot2dockerのipで接続ができる
ssh root@192.168.99.100 -p 10022

不要になったimageをまとめて削除

  • 2週間以上過ぎたnoneなimageを削除する
clean_none_images.sh
for i in $(docker images | awk /weeks/'{print $3}')
do
  docker rmi $i
done
cronに以下追加
10 10 * * 1 $HOME/cron/clean_none_images.sh

不要になったimageをまとめて削除2

docker-cleanup.sh
docker rm `docker ps -a | grep Exit | awk '{print $1}'`
docker rmi `docker images | grep none | awk '{print $3}'`

docker save/load

  • 公式document イメージを保存/復元する場合に使用。
  • export/import というものもあるが、こちらはレイヤー階層を無視するので注意。
docker save sample-image > sample-image.tar
docker load < sample-image.tar
5
5
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
5
5