docker制作镜像

发布时间:2018-01-05 22:37:27编辑:admin阅读(5257)

    从 rootfs 压缩包导入

    格式:docker import [选项] <文件>|<URL>|- [<仓库名>[:<标签>]]


    压缩包可以是本地文件、远程 Web 文件,甚至是从标准输入中得到。压缩包将会在镜像 / 目录展开,并直接作为镜像第一层提交。


    比如我们想要创建一个 OpenVZ 的 centos7 模板的镜像:

    访问连接:http://download.openvz.org/template/precreated/

    blob.png

    由于文件比较大,我直接用迅雷下载了

    将文件用xftp上传到/root目录


    导入镜像

    # docker import centos-7-x86_64-minimal.tar.gz  openvz/centos7
    sha256:dc0fbd3e8b30b73c6556e6392b74f4d168e283dc80bd5a367e342a030e814b12

    这条命令将 tar.gz文件作为根文件系统展开导入,并保存为镜像 openvz/centos7

    导入成功后,我们可以用 docker image ls 看到这个导入的镜像:

    # docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    openvz/centos7      latest              dc0fbd3e8b30        13 seconds ago      434.5 MB

    Docker 还提供了 docker load 和 docker save 命令,用以将镜像保存为一个 tar 文件,然后传输到另一个位置上,再加载进来。这是在没有 Docker Registry 时的做法,现在已经不推荐,镜像迁移应该直接使用 Docker Registry,无论是直接使用 Docker Hub 还是使用内网私有 Registry 都可以。


    用镜像启动一个容器看看

    [root@localhost ~]# docker run -it --name test-centos7 openvz/centos7 /bin/bash
    [root@252022847ea0 /]# ifconfig
    bash: ifconfig: command not found

    mini版的centos7是没有ifconfig命令的,可以使用yum whatprovides命令来查找对应的包

    [root@252022847ea0 /]# yum whatprovides ifconfig
    net-tools-2.0-0.22.20131004git.el7.x86_64 : Basic networking tools
    Repo        : base
    Matched from:
    Filename    : /sbin/ifconfig

    上面提示的信息,表示这个命令是属于net-tools软件包的

    # yum -y install net-tools

    使用ifconfig就可以查看IP地址了

    [root@252022847ea0 /]# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
            inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20<link>
            ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
            RX packets 3486  bytes 23040096 (21.9 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 3244  bytes 179432 (175.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    当然了,还可以安装一些基本命令

    # yum -y install vim wget nscd telnet
    # yum clean all

    使用exit命令退出

    [root@252022847ea0 /]# exit
    exit
    [root@localhost ~]#

    查看docker 运行进程

    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    252022847ea0        openvz/centos7      "/bin/bash"         7 minutes ago       Exited (0) 10 seconds ago                test-centos7

    提交(commit)修改并保存这个容器成为一个new_centos7镜像

    [root@localhost ~]# docker commit 252022847ea0 new_centos7
    sha256:ee7737270cc787e2669379f2e15a4621f429e506454cd906c032303951cf4245

    将new_centos7的tar.gz文件保存在/opt目录下,查看大小

    root@localhost ~]# docker save new_centos7 > /opt/new_centos7.tar.gz
    [root@localhost ~]# ll /opt/new_centos7.tar.gz
    -rw-r--r-- 1 root root 633089024 1月   5 22:03 /opt/new_centos7.tar.gz
    [root@localhost ~]# du -sh /opt/new_centos7.tar.gz
    604M	/opt/new_centos7.tar.gz

    导入tar.gz包到本地docker镜像

    [root@localhost ~]# docker load < /opt/new_centos7.tar.gz
    Loaded image: new_centos7:latest

    查看本地镜像

    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    new_centos7         latest              ee7737270cc7        3 minutes ago       619 MB
    openvz/centos7      latest              dc0fbd3e8b30        13 minutes ago      434.5 MB

    使用导入的镜像,启动一个容器,并查看IP地址

    [root@localhost ~]# docker run -it --name base_centos7 new_centos7 /bin/bash
    [root@d407b7faa878 /]# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
            inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20<link>
            ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
            RX packets 6  bytes 508 (508.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6  bytes 508 (508.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    发现没有报错了,说明刚才安装的软件还在

关键字