1. 概述

nerdctl想要构建镜像需要安装buildkit插件,且不能基于本地镜像进行构建,所以建议构建镜像和镜像仓库仍使用docker作为运行时。

2. 安装docker-ce

1
2
3
[root@localhost ~]# rm -rf /etc/yum.repos.d/* && wget ftp://ftp.rhce.cc/k8s/* -P /etc/yum.repos.d/
[root@harbor ~]# yum install docker-ce -y
[root@harbor ~]# systemctl enable docker --now

3. 添加配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@harbor ~]# cat /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}

4. 创建Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@harbor ~]# cat Dockerfile
FROM hub.c.163.com/library/centos
MAINTAINER ren
RUN rm -rf /etc/yum.repos.d/*
ADD repo.tar /etc/yum.repos.d/
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-* && \
yum install net-tools iproute nginx -y && \
yum clean all && \
rm -rf /var/cache/yum/x86_64/*
COPY index.html /usr/share/nginx/html/
ENV myname xxx
VOLUME ["/data1","/data2"]

CMD ["nginx", "-g","daemon off;"]

5. 构建镜像

1
docker build -t centos:v1 .

6. 容器构建层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@harbor ~]# docker history nginx:v1
IMAGE CREATED CREATED BY SIZE COMMENT
2d27d9685011 2 minutes ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
78910337674c 2 minutes ago /bin/sh -c #(nop) USER ren 0B
a08ee1b47ccb 2 minutes ago /bin/sh -c #(nop) VOLUME [/data1 /data2] 0B
b113d86db0f8 2 minutes ago /bin/sh -c #(nop) ENV myname=xxx 0B
3850648e9adc 2 minutes ago /bin/sh -c #(nop) COPY file:a633a7048410ec7d… 9B
c4b8c4b6bb2f 2 minutes ago /bin/sh -c rpm --import /etc/pki/rpm-gpg/RPM… 57MB
921bf2371e22 54 minutes ago /bin/sh -c #(nop) ADD file:7f9bc919601a811e4… 3.13kB
478e859d1eb5 54 minutes ago /bin/sh -c rm -rf /etc/yum.repos.d/* 0B
88d815c24789 About an hour ago /bin/sh -c #(nop) MAINTAINER ren 0B
328edcd84f1b 4 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 4 years ago /bin/sh -c #(nop) LABEL name=CentOS Base Im… 0B
<missing> 4 years ago /bin/sh -c #(nop) ADD file:63492ba809361c51e… 193MB

7. 测试镜像

1
docker run -d --name nginx -p 80:80 nginx:v1