LoginSignup
11
11

More than 5 years have passed since last update.

Dockerでいい感じのDebianのイメージが欲しい

Posted at

Docker でいい感じの Debian のイメージが欲しい

Docker も Debian も最高ですね
最高に最高を合わせたら最高なのでいい感じの Debian イメージが Docker でも欲しいですね

ということで作りましょう

シェルスクリプトで簡単に

https://github.com/dotcloud/docker-debian/blob/master/contrib/mkimage-debian.sh
公式でシェルスクリプトが用意されているのでこれを使います

…と行きたいところですが,サポートされなくなったようで,一部のコマンドが古いです

avoid deprecated message of docker tag command (in docker 0.7) by tdtds · Pull Request #6 · dotcloud/docker-debian

プルリクエストが出ているのでこれを適用します

このプルリクエストを適用しつつ,インデントも直したものが以下のものになります

mkimage-debian.sh
#!/bin/bash

# cf: https://github.com/dotcloud/docker-debian/blob/master/contrib/mkimage-debian.sh

set -e

# these should match the names found at http://www.debian.org/releases/
stableSuite='wheezy'
testingSuite='jessie'
unstableSuite='sid'

variant='minbase'
include='iproute,iputils-ping'

repo="$1"
suite="${2:-$stableSuite}"
mirror="${3:-}" # stick to the default debootstrap mirror if one is not provided

if [ ! "$repo" ]; then
  echo >&2 "usage: $0 repo [suite [mirror]]"
  echo >&2 "   ie: $0 tianon/debian squeeze"
  exit 1
fi

target="/tmp/docker-rootfs-debian-$suite-$$-$RANDOM"

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
returnTo="$(pwd -P)"

set -x

# bootstrap
mkdir -p "$target"
sudo debootstrap --verbose --variant="$variant" --include="$include" "$suite" "$target" "$mirror"

cd "$target"

# create the image
img=$(sudo tar -c . | docker import -)

# tag suite
docker tag $img $repo:$suite

# test the image
docker run -i -t $repo:$suite echo success

if [ "$suite" = "$stableSuite" -o "$suite" = 'stable' ]; then
  # tag latest
  docker tag $img $repo:latest
  # tag the specific debian release version
  ver=$(docker run $repo:$suite cat /etc/debian_version)
  docker tag $img $repo:$ver
fi

# cleanup
cd "$returnTo"
sudo rm -rf "$target"

このシェルスクリプトの使い方を紹介します

コマンド実行例

sudo ./mkimage-debian.sh catatsuy/wheezy wheezy http://ftp.jp.debian.org/debian

このようにするとミラーサーバーが日本の http://ftp.jp.debian.org/debian な wheezy (Debian7) のイメージが catatsuy/wheezy という名前で登録されます

パッケージが足りない

このイメージを使うと普通の Debian よりもパッケージが足りないことに気付くと思います

原因は debootstrap--variant を指定しているからです

ここで Debian 環境が手元にある人は man debootstrap を実行しましょう

debootstrap(8): Bootstrap basic Debian system - Linux man page

普通の Debian のイメージが欲しい場合は --variant を指定しなければいいようなので debootstrap--variant オプションと variant 変数を消すのがおすすめです

またイメージの時点で git など最低限のものをインストールしておきたい場合(preseed でパッケージを入れているような場合)は include にパッケージを足しておきます

イメージをファイルで欲しい

Docker には push/pull というコマンドがありますが,これを使うと全体に公開されてしまいますし,日本から使うと実用するのが不可能な程度の速度しか出ません

ということでファイルで用意する方法を紹介します

container_id=`docker run -d catatsuy/wheezy ls`
docker export $container_id > image.tar

こんな感じにするとファイルが出来ます

インポートしたい場合は以下のようにします

cat image.tar | docker import - catatsuy/newwheezy

イメージの形式は .tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz に対応しているとのことなので,この辺りの形式で圧縮しておくのがおすすめです

実例

私はパッケージが普通の Debian と同程度に入っていてかつ,sudo,git,subversion,build-essential,openssh-server が入っているイメージが欲しかったので以下のシェルスクリプトを用意しました

参考になれば幸いです

my-mkimage-debian.sh
#!/bin/bash
# cf: https://github.com/dotcloud/docker-debian/blob/upstream/contrib/mkimage-debian.sh
# rm variant

set -e

# these should match the names found at http://www.debian.org/releases/
stableSuite='wheezy'
testingSuite='jessie'
unstableSuite='sid'

include='iproute,iputils-ping,sudo,subversion,git,build-essential,openssh-server'

repo="$1"
suite="${2:-$stableSuite}"
mirror="${3:-}" # stick to the default debootstrap mirror if one is not provided

if [ ! "$repo" ]; then
  echo >&2 "usage: $0 repo [suite [mirror]]"
  echo >&2 "   ie: $0 tianon/debian squeeze"
  exit 1
fi

target="/tmp/docker-rootfs-debian-$suite-$$-$RANDOM"

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
returnTo="$(pwd -P)"

set -x

# bootstrap
mkdir -p "$target"
sudo debootstrap --verbose --include="$include" "$suite" "$target" "$mirror"

cd "$target"

# create the image
img=$(sudo tar -c . | docker import -)

# tag suite
docker tag $img $repo:$suite

# test the image
docker run -i -t $repo:$suite echo success

if [ "$suite" = "$stableSuite" -o "$suite" = 'stable' ]; then
  # tag latest
  docker tag $img $repo:latest
  # tag the specific debian release version
  ver=$(docker run $repo:$suite cat /etc/debian_version)
  docker tag $img $repo:$ver
fi

# cleanup
cd "$returnTo"
sudo rm -rf "$target"

11
11
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
11
11