Dockerfile结合commit

Dockerfile+commit

镜像构建
	镜像格式:仓库地址/用户名/镜像名:版本号  latest
	镜像基于分层文件系统结合的 UFS - UNION,overlay (128 层)
	
	镜像(	tomcat )
		系统生态:centos7.9
		应用环境:tomcat + JDK
		
		
基础镜像的由来:打包操作系统文件

自定义镜像
	容器 > 镜像
		启动一个容器(要求镜像必须能够正常启动)
		进入容器,进行环境部署
		docker commit cName imageName (可以当做备份工具用)
	
	Dockerfile Build 镜像
		编写 Dockerfile 镜像制作逻辑
		当前的上下文打包发送至 DockerDaemon
		DockerDaemon 按照自上而下一次解释内容,转换成为镜像层

commit 与 Build 的优缺点
	commit
		优点:所见所得
		缺点
			功能不完善,无法封装元数据级别的信息定义
			重用性差
		
		
	build
		优点
			功能完善
			重用性高
		缺点:需要有较强的逻辑
		

如何选择构建镜像的方式

​ 如果一个应用程序的构建步骤不熟悉:commit
如果为了后期稳定:build

如果有一个业务场景 实在不熟悉, 而且没有时间, 那么用接下来的方法

mkdir commit && cd commit && vim Dockerfile
	FROM centos:centos7.9.2009
	RUN touch /root/startup.sh && chmod +x /root/startup.sh
	CMD tail -f /root/startup.sh 

docker build -t joe/centos:v0.1 .
docker rm -f $(docker ps -q)
docker run --name nginx -p 80:80 -d joe/centos:v0.1




进入容器
cd && yum -y install gcc gcc-c++ make lrzsz pcre pcre-devel zlib zlib-devel 
# 传入软件包
tar -xf nginx-1.13.8.tar.gz  && cd nginx-1.13.8  &&  ./configure --prefix=/usr/local/nginx && make && make install && echo "helloNginx" > /usr/local/nginx/html/index.html  &&  /usr/local/nginx/sbin/nginx 
## 测试访问成功

# 做镜像大小优化
kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid)  &&  rm -rf nginx-1.13.8*  && yum clean all
exit # 退出容器

docker commit nginx joe/web:v0.1 # 封装成镜像

#为镜像封装nginx 启动命令

mkdir build &&  cd build/
vim Dockerfile
    FROM joe/web:v0.1
    MAINTAINER joe xin_2018@163.com
    LABEL nginxversion="1.13.8"
    EXPOSE 80
    EXPOSE 443
    WORKDIR /usr/local/nginx
    RUN rm -rf /root/startup.sh
    CMD /usr/local/nginx/sbin/nginx && tail -f /usr/local/nginx/logs/access.log
docker build -t joe/web:v0.2 .
docker rm -f $(docker ps -q)
docker run --name nginx -p 80:80 -d joe/web:v0.2 
## 测试访问成功
docker logs nginx #查看日志

不建议使用此方式

因为在Dockerfile中 每一行都是一个可共享的层 ,每一层都可以与其他镜像共享 , 但是在commit 中可共享性太低了

ENV 的优先级

docker run --name db --env MYSQL_ROOT_PASSWORD=example -d mariadb 
# MYSQL_ROOT_PASSWORD设定mysql 的密码

镜像的动态化

​ 系统变量+ 预处理脚本 -实现镜像动态化

变量的优先级

Dockerfile ENV		#有优先级第三
docker run --env	# 优先级第二
ducker exec container set xx=  # 优先级第一

实际案例

vim /root/startup.sh
#预处理脚本
#!/bin/bash
echo $DATA_INDEX > /usr/local/nginx/html/index.html
/usr/local/nginx/sbin/nginx
tail -f /usr/local/nginx/logs/access.log


chmod +x /root/startup.sh

vim /root/build/Dockerfile
    FROM joe/web:v0.1
    MAINTAINER joe xin_2018@163.com
    LABEL nginxversion="1.13.8"
    EXPOSE 80
    EXPOSE 443
    WORKDIR /usr/local/nginx
    RUN rm -rf /root/startup.sh
    ADD ./startup.sh /root 
    RUN chmod +x /root/startup.sh
    CMD /bin/bash /root/startup.sh
 
docker build -t joe/web:v0.3 .  (加 --no-cache 表示不加载缓存, 防止startup文件名相同时, 修改 startup 不生效)(幂等性)
docker rm -f $(docker ps -q)
docker run --name nginx -p 80:80 -d joe/web:v0.3 
## 访问测试
docker rm -f $(docker ps -q)
docker run --name nginx4 -p 80:80 --env DATA_INDEX="hahaha" -d joe/web:v0.3
## 再次访问测试

posted on 2022-02-09 23:53  joe_HelloWorld  阅读(91)  评论(0编辑  收藏  举报

导航