Create Ubuntu 18.04 LTS docker image
一: 环境说明
- 物理机器 macbook pro
- 虚拟机 VirtualBox
- 宿主系统 Ubuntu 18.04 LTS
- 宿主系统 Docker 版本 Docker version 18.06.1-ce, build e68fc7a
首先在虚拟机中安装好宿主系统,配置好基础网络环境,并切换到root账户下.
二: 制作 Ubuntu 18.04 LTS docker镜像
1. 安装 debootstrap
1 |
# apt install debootstrap |
2. 用 debootstrap 构建 Ubuntu 18.04 LTS 的 rootfs
1 2 3 4 5 |
# mkdir -p /data0/docker/images/ubuntu-18.04-lts # debootstrap --verbose --arch=amd64 bionic /data0/docker/images/ubuntu-18.04-lts http://mirrors.aliyun.com/ubuntu # chroot /data0/docker/images/ubuntu-18.04-lts /bin/bash # 切换到基础系统中 |
三: 配置基础系统
1.修改默认的源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# mv /etc/apt/sources.list /etc/apt/sources.list.bak # vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse |
2. 安装基础包
1 |
# apt -y update && apt -y upgrade && apt -y install vim locales lsof |
3. 配置字符集为 en_US.UTF-8 UTF-8
1 2 3 |
# dpkg-reconfigure locales # echo 'export LC_ALL=C' >> /etc/profile |
4. 配置时区
1 |
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime |
5. 清理系统,退出当前 rootfs
1 |
# rm -Rf /tmp/* && apt-get autoremove && apt-get clean && apt clean && exit |
四: 打包并创建 Docker 镜像
需要宿主机安装配置好docker运行环境,如果未安装,则使用以下方法安装:
1 2 3 |
# apt -y install docker.io # systemctl start docker && systemctl enable docker |
1. 打包镜像
1 |
# tar -C /data0/docker/images/ubuntu-18.04-lts -c . | sudo docker import - ubuntu-18.04-lts |
2. 查看是否打包成功
1 2 3 4 5 |
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kernelstudio/ubuntu latest 1d0b29b1a99f 34 minutes ago 551MB |
五: 测试打包的镜像
1 2 3 |
# docker run ubuntu-18.04-lts /bin/sh -c "echo hello" hello |
六: 发布到官方仓库,需自提前注册好账户
1. 输入用户名密码登录
1 |
# docker login |
2. 创建一个名为的 kernelstudio/ubuntu 标签
1 |
# docker tag ubuntu-18.04-lts kernelstudio/ubuntu |
3. 推送到官方仓库
1 |
# docker push kernelstudio/ubuntu |
七: 备注
如果要把当前的系统环境(宿主系统)打包为容器基础镜像,主要是 rootfs 的处理,则使用以下命令
1 |
# tar --numeric-owner --exclude=/proc --exclude=/sys -C / -c . | sudo docker import - image-name-version |
No Comments