我想要一个Docker上的好用的Debian镜像

我希望有一个很好的 Debian 镜像,可以在 Docker 中使用。

Docker和Debian都很棒呢,如果能將最棒的最棒結合起來,那Docker上的Debian映像肯定也會很棒哦。

让我们来制作吧

用Shell脚本轻松地完成。

可以使用提供的官方Shell脚本进行操作,链接如下:
https://github.com/dotcloud/docker-debian/blob/master/contrib/mkimage-debian.sh

我希望去那里,但似乎不再支持,部分指令已经过时。

通过 tdtds · 发起请求 #6 · dotcloud/docker-debian 避免使用 docker tag 命令(在 docker 0.7 版本中已被弃用)的废弃警告信息。

由于有一个拉取请求,所以我们将应用它。

在应用了该拉取请求的同时,我还进行了缩进修正,得到的结果如下所示。

#!/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"

我将介绍如何使用这个Shell脚本。

执行命令示例

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

这样做的话,Mirror服务器将会以catatsuy/wheezy的名称注册,包含Debian 7 (wheezy)版本的日本镜像http://ftp.jp.debian.org/debian。

包装不够

我认为,使用这个镜像会注意到它比普通的 Debian 缺少一些软件包。

之所以如此,是因为设置了 debootstrap 的 –variant 参数。

如果你手上有Debian环境,请执行man debootstrap。

debootstrap(8):启动基本的Debian系统 – Linux手册页面

如果你想要一个普通的Debian镜像,只需不指定 –variant,所以建议你删除debootstrap的–variant选项和variant变量。

如果希望在创建镜像时安装最基本的软件(例如 git),可以在 include 中添加软件包(就像使用 preseed 安装软件包时的方式)。

我想要一个图像文件。

在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的映像,因此我准备了以下的shell脚本。

如果能对您有所帮助,我会感到非常幸运。

#!/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"