先看一下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