# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos7u6.tar /
4.2.3 把打包后文件加载至本地文件系统生成本地容器镜像
# ls centos7u6.tar
# docker import centos7u6.tar centos7u6:v1
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7u6 v1 130cb005b2dc 7 seconds ago 1.09GB
# docker run -it centos7u6:v1 bash [root@50f24f688b4d /]# ip a s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever
4.3 把正在运行的容器打包为容器镜像
4.3.1 运行一个容器
# docker run -it centos7u6:v1 bash
4.3.2 在容器中安装应用
[root@064aace45718 /]# yum -y install httpd
4.3.3 把正在运行的容器打包为容器镜像
[root@064aace45718 /]# ctrl + p +q
# docker commit 064aace45718 centos7u6-httpd:v1
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7u6-httpd v1 30ec9d728880 6 seconds ago 1.29GB
RUN yum install httpd httpd-devel-y RUN echo test > /var/www/html/index.html 可以改成 RUN yum install httpd httpd-devel-y && echo test > /var/www/html/index.html 或者改成 RUN yum install httpd httpd-devel-y \ && echo test > /var/www/html/index.html
================================================================================ Package Arch Version Repository Size ================================================================================ Installing: wget x86_64 1.14-18.el7_6.1 base 547 k
Removing intermediate container 4bdfc0a1c844 ---> 1d73faa62447 第五步:安装Nginx Step 5/9 : RUN yum -y install nginx ---> Running in51b50c2ce841 Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn Resolving Dependencies --> Running transaction check ---> Package nginx.x86_64 1:1.20.1-9.el7 will be installed --> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: nginx-filesystemfor package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: openssl for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: redhat-indexhtmlfor package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: system-logosfor package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64 --> Running transaction check ---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed ---> Package nginx-filesystem.noarch 1:1.20.1-9.el7 will be installed ---> Package openssl.x86_64 1:1.0.2k-24.el7_9 will be installed --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-24.el7_9 for package: 1:openssl-1.0.2k-24.el7_9.x86_64 --> Processing Dependency: make for package: 1:openssl-1.0.2k-24.el7_9.x86_64 ---> Package openssl11-libs.x86_64 1:1.1.1k-2.el7 will be installed --> Running transaction check ---> Package make.x86_64 1:3.82-24.el7 will be installed ---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated ---> Package openssl-libs.x86_64 1:1.0.2k-24.el7_9 will be an update --> Finished Dependency Resolution
Dependencies Resolved
================================================================================ Package Arch Version Repository Size ================================================================================ Installing: nginx x86_64 1:1.20.1-9.el7 epel 587 k Installing for dependencies: centos-indexhtml noarch 7-9.el7.centos base 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M gperftools-libs x86_64 2.6.1-1.el7 base 272 k make x86_64 1:3.82-24.el7 base 421 k nginx-filesystem noarch 1:1.20.1-9.el7 epel 24 k openssl x86_64 1:1.0.2k-24.el7_9 updates 494 k openssl11-libs x86_64 1:1.1.1k-2.el7 epel 1.5 M Updating for dependencies: openssl-libs x86_64 1:1.0.2k-24.el7_9 updates 1.2 M
FROM centos:latest MAINTAINER www.kubemsb.com RUN yum install epel-release-y RUN yum install -y gcc gcc-c++ make -y RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz RUN tar zxf php-5.6.36.tar.gz RUN cd php-5.6.36 RUN ./configure --prefix=/usr/local/php RUN make -j4 RUN make install EXPOSE 9000 CMD ["php-fpm"]
优化内容如下:
FROM centos:latest MAINTAINER www.kubemsb.com RUN yum install epel-release-y && \ yum install -y gcc gcc-c++ make
RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \ tar zxf php-5.6.36.tar.gz && \ cd php-5.6.36 && \ ./configure --prefix=/usr/local/php && \ make -j4 && make install EXPOSE 9000 CMD ["php-fpm"]
RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \ tar zxf php-5.6.36.tar.gz && \ cd php-5.6.36 && \ ./configure --prefix=/usr/local/php \ make -j4 && make install && \ cd / && rm-rf php*