赞
踩
目录
docker commit 构建新镜像三部曲:
1运行容器
2修改容器
3将容器保存为新的镜像
交互式:运行容器的同时进入容器,交互式,并且在容器内创建文件
[root@k8s1 ~]# docker run -it --name demo busybox
/ # touch file1
/ # touch file2
ctrl+d: 退出容器后容器自动关闭
ctrl+pq: 退出容器后继续在后台运行
显示所有容器
[root@k8s1 ~]# docker ps -a
启动容器
[root@k8s1 ~]# docker start demo
进入容器
[root@k8s1 ~]# docker attach demo
提交容器变更到新的镜像
[root@k8s1 ~]# docker commit -m "add files" demo demo:v1
[root@k8s1 ~]# docker history demo:v1
IMAGE CREATED CREATED BY SIZE COMMENT
2b25ea847e4e 9 seconds ago sh 39B add files
827365c7baf1 13 days ago /bin/sh -c #(nop) CMD ["sh"] 0B
<missing> 13 days ago /bin/sh -c #(nop) ADD file:3f2f2548e5ddec788… 4.86MB
这种方式不利于安全审计
删除镜像
[root@k8s1 docker]# docker rmi demo:v1
在7.6里封装nginx应用
Dockerfile是一个创建镜像所有命令的文本文件, 包含了一条条指令和说明, 每条指令构建一层, 通过docker build命令,根据Dockerfile的内容构建镜像,因此每一条指令的内容, 就是描述该层如何构建.有了Dockefile, 就可以制定自己的docker镜像规则,只需要在Dockerfile上添加或者修改指令, 就可生成docker 镜像
一旦建立,随处运行
准备rhel7.tar和nginx-1.20.1.tar.gz安装包
创建目录
[root@k8s1 ~]# mkdir docker
编写Dockerfile文件
[root@k8s1 ~]# cd docker/
[root@k8s1 docker]# vim Dockerfile
FROM centos:7
ADD nginx-1.22.1.tar.gz /mnt
EXPOSE 80 #如果容器中运行应用服务,暴露服务端口为80出去
WORKDIR /mnt/nginx-1.22.1 #进入目录里执行一下命令
RUN yum install -y gcc make openssl-devel pcre-devel #源码编译所需依赖包
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc #关掉debug,编译完内存小一些
RUN ./configure --with-http_ssl_module --with-http_stub_status_module #编译
RUN make
RUN make install
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"] #运行镜像时所需执行命令
构建镜像并给名字
[root@k8s1 docker]# docker build -t webserver:v1 . # "." 代表当前目录
查询所构建的镜像,内存有点大,之后将对其进行优化
[root@k8s1 docker]# docker images webserver
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver v1 d8e7a0b9c180 6 seconds ago 489MB
测试镜像是否可运行
[root@k8s1 docker]# docker run -d --name demo webserver:v1
查询ip
[root@k8s1 docker]# docker inspect demo
成功访问
下面就来优化镜像的大小。主要有下面几种办法:
1、选择最精简的基础镜像
2、减少镜像的层数
3、清理镜像构建的中间产物
4、注意优化网络请求
5、尽量去用构建缓存
6、使用多阶段构建镜像
合并指令,执行减少层数,并且删掉缓存和源码,如gcc等工具,前面用的缓存作为一层,复制最终的nginx二进制程序到新的一层,这样内存占用就非常小
[root@k8s1 docker]# vim Dockerfile
FROM centos:7 as build
ADD nginx-1.22.1.tar.gz /mnt
EXPOSE 80
WORKDIR /mnt/nginx-1.22.1
RUN yum install -y gcc make openssl-devel pcre-devel && yum clean all
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && cd .. && rm -fr /mnt/nginx-1.22.1
FROM centos:7
COPY --from=build /usr/local/nginx /usr/local/nginx
EXPOSE 80
VOLUME ["/usr/local/nginx/html"] #数据目录挂载
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
构建镜像
[root@k8s1 new]# docker build -t webserver:v2 .
查看镜像,只有两百多,比之前小很多
了进一步压缩,考虑到rhel7中组件过多,很多东西用不上,还占空间,所以想要更换轻量级源镜像
导入镜像
[root@k8s1 ~]# docker load -i base-debian11.tar
因为时轻量级,我们需要给他安装nignx所需要的函数库
[root@k8s1 new]# vim Dockerfile
FROM nginx:latest as base
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
ARG Asia/Shanghai
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libpcre* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian11
COPY --from=base /opt /
EXPOSE 80 443
ENTRYPOINT ["nginx", "-g", "daemon off;"]
构建镜像
[root@k8s1 new]# docker build -t webserver:v3 .
比最开始,镜像大小缩小了很多。
[root@k8s1 new]# docker images webserver
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver v3 69f7a1c5cee7 8 minutes ago 34MB
webserver v2 0cb27a7083e0 26 minutes ago 205MB
webserver v1 d8e7a0b9c180 22 hours ago 489MB
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。