使用debootstrap製做debian-jessie系統docker鏡像

先看一下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. 意思是你製做鏡像的機器運行的系統版本要與你要製做的鏡像的系統版本相同。ios

一、安裝debootstrapnginx

# aptitude update
# aptitude install -y debootstrap

二、查看幫助,debootstrap的使用方法git

# 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

三、創建debian系統到指定目錄github

# 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

四、打包系統目錄,並導入到docker(這裏僅做測試,能夠省略這一步)docker

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

五、打包系統目錄,放到指定位置bootstrap

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

六、編寫dockerfilevim

# 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 /$$

七、build鏡像bash

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

八、啓動鏡像,安裝項目所須要的基礎程序包ssh

# 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

九、安裝好基礎環境後,提交一個新的鏡像測試

# 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

十、將鏡像加載到線上環境

# 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

十一、啓動容器,分配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/使用%20debootstrap%20創建完整的%20Debian%20系統.org

相關文章
相關標籤/搜索