使用debootstrap制作debian-jessie系统docker镜像

时间:2022-11-06 19:31:46

先看一下Docker官网提示:In general, you’ll want to start with a working machine that is running the distribution you’d like to package as a parent image. 意思是你制作镜像的机器运行的系统版本要与你要制作的镜像的系统版本相同。

1、安装debootstrap

# aptitude update
# aptitude install -y debootstrap

2、查看帮助,debootstrap的使用方法

# debootstrap --help

debootstrap [OPTION]... <suite> <target> [<mirror> [<script>]]

--arch=A:set the architecture to install (use if no dpkg) [ --arch=powerpc ]
--include=A,B,C:adds specified names to the list of base packages
--no-check-gpg:avoid checking Release file signatures

3、建立debian系统到指定目录

# debootstrap --no-check-gpg --arch=amd64 --include=vim,ca-certificates,net-tools,iputils-ping,iputils-tracepath,iproute,bash,aptitude,locales-all,openssh-server,openssh-client jessie debian-jessie http://mirrors.ustc.edu.cn/debian

4、打包系统目录,并导入到docker(这里仅作测试,可以省略这一步)

# tar -C debian-jessie -c . | docker import - debian-jessie:0.0.1
sha256:10aad891ca3f1db689459b8a230146107eeb6e6f7264051b4adafff25303a3f1

5、打包系统目录,放到指定位置

# cd debian-jessie/
# tar cvzf ../jessie.tar.gz ./
# mkdir -pv /docker/imagesbuilder/opera/debian8-0.0.1/
# mv jessie.tar.gz /docker/imagesbuilder/opera/debian8-0.0.1/

6、编写dockerfile

# cd /docker/imagesbuilder/opera/debian8-0.0.1/
# tree
.
├── Dockerfile
├── jessie.tar.gz
└── source
├── authorized_keys
├── autoboot.sh
├── oupeng-alias.sh
├── oupeng-history.sh
├── sources.list
└── sshd_config # vim Dockerfile
FROM scratch
MAINTAINER KeithTt shengyongp@oupeng.com ADD jessie.tar.gz /
ADD source/sources.list /etc/apt/
ADD source/sshd_config /etc/ssh/
ADD source/authorized_keys /root/.ssh/
ADD source/oupeng-alias.sh /etc/profile.d/
ADD source/oupeng-history.sh /etc/profile.d/
ADD source/autoboot.sh /root/bin/autoboot.sh ENTRYPOINT /root/bin/autoboot.sh && /bin/bash
# cat autoboot.sh
#!/bin/bash
. /etc/profile.d/oupeng-alias.sh
. /etc/profile.d/oupeng-history.sh
services=($(grep 'Default-Start' /etc/init.d/* | grep -v '^[ ]*#' | awk -F: '{print $1":"$NF}' |sed -e 's/[\t ]\+//g'))
for((i=0;i<${#services[@]};i++))
do
levels=$(echo ${services[$i]} | awk -F: '{print $2}')
if [[ $(echo $levels |grep -c '3') -eq 0 ]];then
continue
fi
service=$(echo ${services[$i]} | awk -F: '{print $1}')
if [[ -x $service ]];then
echo "$service" >> /$$
fi
done
for p in $(cat /$$)
do
$p start
done
rm -f /$$

7、build镜像

# docker build -t oupeng/debian8:0.0.1 .
# docker images

8、启动镜像,安装项目所需要的基础程序包

# docker run -it oupeng/debian8:0.0.1
# aptitude update
# aptitude install -y lrzsz vim bash-completion nagios-nrpe-server nagios-plugins-basic supervisor nginx libc6-dev
# aptitude clean

9、安装好基础环境后,提交一个新的镜像

# docker commit -a "KeithTt <shengyongp@oupeng.com>" -m "debian-jessie-transcoder" tender_bardeen oupeng/o12_transcoder:0.1.0
# docker images
# docker save -o o12_transcoder.0.1.0.tar oupeng/o12_transcoder:0.1.0

10、将镜像加载到线上环境

# scp o12_transcoder.0.1.0.tar 117.119.33.166:/root/
# ssh 117.119.33.166
# docker load -i o12_transcoder.0.1.0.tar

11、启动容器,分配IP

# docker run -it -v /proc:/wproc -v /docker/vdisk/v01:/data -m 8G -h $(hostname)-v08 --name $(hostname)-v08 --net=none oupeng/o12_transcoder:0.1.0
# /root/bin/mkipinner.sh uy-s-123-v08 192.168.10.179 20 192.168.1.248
# ssh 192.168.10.179
# aptitude install -y locales-all

这样,一个docker虚拟机就运行起来了。

参考:

https://docs.docker.com/develop/develop-images/baseimages/

https://github.com/KingBing/blog-src/blob/master/使用 debootstrap 建立完整的 Debian 系統.org

https://www.jianshu.com/p/47c4a06a84a4