将容器的文件系统打包成tar文件,也就是把正在运行的容器直接导出为tar
包
查看当前正在运行的容器:
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d7d6057e177 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp chen4
18d6604e45d9 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp chen2
方法一:export将正在运行的chen2容器导出为一个镜像tar包
[root@docker ~]# docker export -o chen2.tar chen2
[root@docker ~]# ls
anaconda-ks.cfg chen2.tar
方法二:export将正在运行的chen4容器导出为一个镜像tar包
[root@docker ~]# docker export chen4 > chen4.tar
[root@docker ~]# ls
anaconda-ks.cfg chen2.tar chen4.tar
import导出镜像tar包到本地:
[root@docker ~]# ls
anaconda-ks.cfg chen2.tar chen4.tar
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
admin2 v2 8947f3134ced 2 hours ago 663MB
chen1 latest a2b97e3e4c7c 12 hours ago 140MB
nginx latest de2543b9436b 9 days ago 142MB
[root@docker ~]# ls
anaconda-ks.cfg chen2.tar chen4.tar
传送到远端并用import解包成镜像:
[root@localhost ~]# ls
anaconda-ks.cfg nginx-chen1.tar
[root@localhost ~]# docker import nginx-chen1.tar nginx:v1
sha256:037e18e4c0e40d9f0a825c8d940b048dea82d55824782db47783f7c3806c730f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 037e18e4c0e4 15 seconds ago 140MB
[root@localhost ~]# docker run -it --name chen1 -p 90:80 nginx:v1 /bin/bash
root@29125be5a722:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@29125be5a722:/# cd /root
root@29125be5a722:~# ls
a.txt
root@29125be5a722:~# cat a.txt
111
root@29125be5a722:~#
也可以直接解包到本地成一个新的镜像
[root@docker ~]# docker import chen2.tar nginx-chen2:v2
sha256:3d9835011c89931b3777ac76b347beb1f9077593116ca828bd353a5670ccb3e6
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-chen2 v2 3d9835011c89 8 seconds ago 140MB
admin2 v2 8947f3134ced 2 hours ago 663MB
chen1 latest a2b97e3e4c7c 12 hours ago 140MB
nginx latest de2543b9436b 9 days ago 142MB
语法:tag 镜像名加版本 新镜像名加版本
[root@docker ~]# docker tag nginx-chen2:v2 nginx-chen2.bak:v2
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-chen2.bak v2 3d9835011c89 2 minutes ago 140MB
nginx-chen2 v2 3d9835011c89 2 minutes ago 140MB
admin2 v2 8947f3134ced 2 hours ago 663MB
chen1 latest a2b97e3e4c7c 12 hours ago 140MB
nginx latest de2543b9436b 9 days ago 142MB
注意:改名类似新增加了一个别名备份,镜像id不变
1,创建并运行容器
[root@docker ~]# docker run -itd --name chen5 -p 90:80 nginx:latest
4e27a198dd1b00c37471734957b26461f04399c1c718cedb6c3ca622c2b87e8a
2,登录容器内
[root@docker ~]# docker exec -it chen5 /bin/bash
root@4e27a198dd1b:/# pwd
/
3,创建一个文件,增加数据
root@4e27a198dd1b:/# touch haha.txt
root@4e27a198dd1b:/# ls
bin docker-entrypoint.d haha.txt lib64 opt run sys var
boot docker-entrypoint.sh home media proc sbin tmp
dev etc lib mnt root srv usr
root@4e27a198dd1b:/# exit
exit
4,将正在运行的容器做成一个镜像(注意不是镜像tar包哦)
[root@docker ~]# docker commit chen5 chen/nginx:v5
sha256:e80e4dca67ce0391c19ef68da59d7cb1c7ea96ae2ccc9ba27a0db19ecd84c8cd
[root@docker ~]# docker commit -m "my version1" -a "chenshuai" chen5 daocloud.io/linux:v5
sha256:196e88ec13407e443a5d865d1644caac5f6ed48f119cc0e1666c59c4d78a5441
-m 添加注释
-a 作者
108a85b1ed99 容器环境id
daocloud.io/ubuntu:v2 镜像名称:hub的名称/镜像名称:tag
-p,–pause=true 提交时暂停容器运行
5,查看本地镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/linux v5 196e88ec1340 52 seconds ago 142MB
chen/nginx v5 e80e4dca67ce 2 minutes ago 142MB
nginx-chen2.bak v2 3d9835011c89 15 minutes ago 140MB
nginx-chen2 v2 3d9835011c89 15 minutes ago 140MB
admin2 v2 8947f3134ced 2 hours ago 663MB
chen1 latest a2b97e3e4c7c 12 hours ago 140MB
nginx latest de2543b9436b 9 days ago 142MB
1,将生成的镜像打包成tar包
[root@docker ~]# docker save -o nginx.tar chen/nginx:v5
[root@docker ~]# ls
anaconda-ks.cfg chen2.tar chen4.tar nginx.tar
2,远程拷贝到另一台主机
[root@docker ~]# scp nginx.tar 10.0.0.220:/root/
[email protected]'s password:
nginx.tar 100% 139MB 167.2MB/s 00:00
3,到另外一台主机中将镜像tar包解压成镜像
[root@localhost ~]# docker load < nginx.tar
d7573b9f64b6: Loading layer 12.29kB/12.29kB
Loaded image: chen/nginx:v5
4,在远程拷贝的主机上创建并运行一个nginx容器
[root@localhost ~]# docker run -itd --name haha -p 90:80 chen/nginx:v5
75c4d5358d55f27b47da6358ec070dbd82b835a5b396c19be279f48cc9d9e890
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75c4d5358d55 chen/nginx:v5 "/docker-entrypoint.…" 5 seconds ago Up 5 seconds 0.0.0.0:90->80/tcp, :::90->80/tcp haha
创建nginx目录
[root@localhost ~]#mkdir /root/nginx
切换目录
[root@localhost nginx]#cd /root/nginx
创建Dockerfile文件
[root@localhost nginx]#touch Dockerfile
去nginx官网复制tar包链接
[root@localhost nginx]#wget http://nginx.org/download/nginx-1.22.0.tar.gz
解压的目的为了后面写Dockerfil文件的需要用到
[root@localhost nginx]#tar xfz nginx-1.22.0.tar.gz
[root@localhost nginx]#ls
Dockerfile nginx-1.22.0 nginx-1.22.0.tar.gz
指定daocloud路径就是国内下载,速度比较快
[root@localhost nginx]#docker pull daocloud.io/library/centos:centos7
不指定路径默认是国外下载最新版本,速度可能比较慢
[root@localhost nginx]#docker pull centos
[root@localhost nginx]#docker images 查看已经下载的nginx镜像
daocloud.io/library/centos centos7 7e6257c9f8d8 21 months ago 203MB
[root@localhost nginx]# vim Dockerfile
FROM daocloud.io/library/centos:7 告诉docker我们的镜像什么,这个镜像是
MAINTAINER "xingdian" chen13665026727@163.com 是描述镜像的创建人,固定格式
RUN rm -rf /etc/yum.repos.d/* 删除国外的仓库
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo 更换国内仓库
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 下载epel仓库
RUN yum clean all && yum makecache
RUN yum -y install gcc openssl openssl-devel pcre-devel zlib-devel make
ADD nginx-1.22.0.tar.gz /usr/local/ 解压
WORKDIR /usr/local/nginx-1.22.0 指定工作目录
RUN ./configure --prefix=/usr/local/nginx
RUN make && make install
WORKDIR /usr/local/nginx
RUN rm -rf /usr/local/nginx-1.22.0
ENV NGINX_HOME=/usr/local/nginx
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/nginx/sbin
EXPOSE 80 暴露端口
CMD ["nginx", "-g", "daemon off;"] 运行
在当前目录下构建(构建注意要自定义命名和加版本号)
[root@localhost nginx]#docker build -t xingdian/nginx:v1 .
需要上传jdk+tomcat+maven+jenkins.war包,自己找官网下载
[root@localhost ~]#mkdir jenkins && cd jenkins
[root@localhost jenkins]# ls
apache-maven-3.8.1 apache-tomcat-8.5.69 Dockerfile jdk-8u211-linux-x64.tar.gz
apache-maven-3.8.1-bin.tar.gz apache-tomcat-8.5.69.tar.gz jdk1.8.0_211 jenkins.war
[root@localhost jenkins]#vim Dockerfile
FROM daocloud.io/library/centos:7
ADD jdk-8u211-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.69.tar.gz /usr/local/
ADD apache-maven-3.8.1-bin.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV TOMCAT_HOME /usr/local/apache-tomcat-8.5.69
ENV MAVEN_HOME /usr/local/apache-maven-3.8.1
ENV PATH $JAVA_HOME/bin:$JAVA_TOMCAT_HOME/bin:$MAVEN_HOME/bin:$PATHRUN
RUN rm -rf /usr/local/apache-tomcat-8.5.69/webapps/*
ADD jenkins.war /usr/local/apache-tomcat-8.5.69/webapps/
EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.69/bin/catalina.sh","run"]
[root@localhost jenkins]#docker build -t jenkins:v1 . 构建注意命名和版本好
[root@localhost jenkins]#docker run -itd --name jenkins -p 8080:8080 jenkins:v1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9381f67be636 6206ce7a99ee "/usr/local/apache-t…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp jenkins