Build-Docker-Image-from-Zero
荣涛
2022-03-11
文档修改日志
日期
修改内容
修改人
备注
2022-03-11
创建
荣涛
引言
本文档主要讲构建基础Docker(Podman)镜像。
从零构建docker镜像示例
参考连接为: Creating container base image of CentOS 和 GitHub: Creating minimal CentOS docker image from scratch。
-
首先为docker创建rootfs
export centos_root="
pwd
/centos_image/rootfs"
mkdir $centos_root
rpm --root $centos_root --initdb
sudo dnf reinstall --downloadonly --downloaddir . centos-stream-release
sudo dnf reinstall --downloadonly --downloaddir . centos-stream-repos
sudo dnf reinstall --downloadonly --downloaddir . centos-gpg-keys
rpm --root $centos_root -ivh centos-*.rpm
rpm --root $centos_root --import c e n t o s r o o t / e t c / p k i / r p m − g p g / R P M − G P G − K E Y − r e d h a t − r e l e a s e s u d o d n f − y − − i n s t a l l r o o t = centos_root/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release sudo dnf -y --installroot= centosroot/etc/pki/rpm−gpg/RPM−GPG−KEY−redhat−releasesudodnf−y−−installroot=centos_root --setopt=tsflags=‘nodocs’ --setopt=override_install_langs=en_US.utf8 install dnf
#sed -i “/distroverpkg=centos-release/a override_install_langs=en_US.utf8
tsflags=nodocs” $centos_root/etc/yum.conf
sudo cp /etc/resolv.conf $centos_root/etc
sudo chroot $centos_root /bin/bash <<EOF
dnf install -y procps-ng iputils
dnf clean all
EOF
sudo rm -f $centos_root/etc/resolv.conf -
创建docker镜像
这步的前提是已经安装了docker,安装方法:
sudo dnf install -y docker
sudo systemctl start docker
然后生成docker镜像文件:
# 生成Docker镜像并导入
sudo tar -C $centos_root -c . | docker import - deleteme
# 查看镜像
docker images
# 运行镜像
docker run --rm deleteme cat /etc/redhat-release
镜像制作成功。
使用buildah构建
参考 Getting started with Buildah。
使用Dockerfile从基础镜像构建
首先,buildah可以从Dockerfile构建,如下Dockerfile:
FROM fedora:28
LABEL maintainer Chris Collins <collins.christopher@gmail.com>
RUN dnf install -y tar gzip gcc make
&& dnf clean all
ADD http://ftpmirror.gnu.org/hello/hello-2.10.tar.gz /tmp/hello-2.10.tar.gz
RUN tar xvzf /tmp/hello-2.10.tar.gz -C /opt
WORKDIR /opt/hello-2.10
RUN ./configure
RUN make
RUN make install
RUN hello -v
ENTRYPOINT "/usr/local/bin/hello"
然后,构建即可:
sudo buildah bud -t hello .
# 查看
sudo buildah images
# 推送
sudo buildah push hello:latest docker-daemon:hello:latest
从scratch
构建
参考链接
- 红帽:How to build tiny container images
- GitHub: Creating minimal CentOS docker image from scratch
- 构建基础镜像
- Creating container base image of CentOS
- Getting started with Buildah
Copyright © CESTC Com.