Docker三个核心之镜像

1、获取镜像

       镜像是Docker容器的前提。

       可以通过命令:docker pull从网络上下载镜像,格式为:docker pull NAME[:TAG],如果不指定TAG,则默认下载仓库的最新镜像。

        下面将演示从Docker仓库下载一个Ubuntu操作系统的镜像。

        输入命令:sudo docker pull ubuntu:14.04这样就可以从Docker仓库下载Ubuntu镜像到本地,输入命令如果出现如下错误:Cannot connect to the Docker daemon. Is the docker daemon running on this host?,可能是docker没有启动,先启动docker,如:service docker start。

        有些镜像可能比较大,需要等待一会,下载完成后我们就可以使用该镜像了,如我们运行bash应用:

        sudo docker run -ti ubuntu:14.04 /bin/bash

        root@iZ231y3atu0Z:/#

2、查看镜像信息

        通过使用命令:docker images就可以列出本地主机上已有的所有镜像,包括镜像名、标签、镜像ID、创建时间、镜像大小。

          注意镜像ID是唯一的,通过ID就可以确定唯一的镜像。

         我们还可以通过命令docker inspect查看镜像的详细信息,如:

         sodu docker inspect 1e0c3dd64ccd

3、搜索镜像

      通过使用命令docker search就可以搜索docker远程仓库中共享的镜像,默认搜索Docker Hub官方仓库中的镜像。例如,我们要搜索带ubuntu关键字的镜像:

      [root@iZ231y3atu0Z ~]# sodu docker search ubuntu
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/ubuntu                            Ubuntu is a Debian-based Linux operating s...   5002      [OK]       
docker.io   docker.io/ubuntu-upstart                    Upstart is an event-based replacement for ...   68        [OK]       
docker.io   docker.io/rastasheep/ubuntu-sshd            Dockerized SSH service, built on top of of...   47                   [OK]
docker.io   docker.io/consol/ubuntu-xfce-vnc            Ubuntu container with "headless" VNC sessi...   28                   [OK]
docker.io   docker.io/ubuntu-debootstrap                debootstrap --variant=minbase --components...   28        [OK]       
docker.io   docker.io/torusware/speedus-ubuntu          Always updated official Ubuntu docker imag...   27                   [OK]
docker.io   docker.io/ioft/armhf-ubuntu                 [ABR] Ubuntu Docker images for the ARMv7(a...   19                   [OK]
docker.io   docker.io/nickistre/ubuntu-lamp             LAMP server on Ubuntu                           10                   [OK]
docker.io   docker.io/nuagebec/ubuntu                   Simple always updated Ubuntu docker images...   9                    [OK]
docker.io   docker.io/nickistre/ubuntu-lamp-wordpress   LAMP on Ubuntu with wp-cli installed            7                    [OK]
docker.io   docker.io/nimmis/ubuntu                     This is a docker images different LTS vers...   5                    [OK]
docker.io   docker.io/maxexcloo/ubuntu                  Base image built on Ubuntu with init, Supe...   2                    [OK]
docker.io   docker.io/admiringworm/ubuntu               Base ubuntu images based on the official u...   1                    [OK]
docker.io   docker.io/darksheer/ubuntu                  Base Ubuntu Image -- Updated hourly             1                    [OK]
docker.io   docker.io/jordi/ubuntu                      Ubuntu Base Image                               1                    [OK]
docker.io   docker.io/datenbetrieb/ubuntu               custom flavor of the official ubuntu base ...   0                    [OK]
docker.io   docker.io/esycat/ubuntu                     Ubuntu LTS                                      0                    [OK]
docker.io   docker.io/konstruktoid/ubuntu               Ubuntu base image                               0                    [OK]
docker.io   docker.io/labengine/ubuntu                  Images base ubuntu                              0                    [OK]
docker.io   docker.io/lynxtp/ubuntu                     https://github.com/lynxtp/docker-ubuntu         0                    [OK]
docker.io   docker.io/teamrock/ubuntu                   TeamRock's Ubuntu image configured with AW...   0                    [OK]
docker.io   docker.io/ustclug/ubuntu                    ubuntu image for docker with USTC mirror        0                    [OK]
docker.io   docker.io/vcatechnology/ubuntu              A Ubuntu image that is updated daily            0                    [OK]
docker.io   docker.io/webhippie/ubuntu                  Docker images for ubuntu                        0                    [OK]
docker.io   docker.io/widerplan/ubuntu                  Our basic Ubuntu images.                        0                    [OK]

默认是按STARS(星级,受欢迎程序)倒序来排列,OFFICIAL标识是否是官方创建。

4、删除镜像

      使用docker rmi就可以删除命令,参数可以是标签或者ID,如:

        sodu socker rmi ubuntu:14.04

        如果镜像下面创建了容器,则默认不允许删除镜像,可以使用-f强制删除,不过不推荐使用,因为可能会造成一些遗留问题。

5、创建镜像

      创建镜像的方法有三种:基于已有镜像的容器创建、基于本地模板导入、基于Dockerfile创建。

        这里介绍第一种方法。

        首选启动一个镜像,并在其中进行修改操作。

        sodu docker run -ti ubuntu:14.04 /bin/bash

      root@iZ231y3atu0Z/#touch test

        root@iZ231y3atu0Z:/#exit

        记住容器ID为:iZ231y3atu0Z

         此时该容器跟原ubuntu:14.04镜像相比,已经发生了改变,可以使用docker commit命令来提交为一个新的镜像。提交时可以使用ID或名称来指定容器。

      sodu docker commit -m "添加一个新的文件" -a "Docker Newbee" iZ231y3atu0Z test

        此时查看本地镜像列表,即可看到新创建的镜像。

        sodu docker images

6、存出和载入镜像

      存出镜像:sudo docker save -o ubuntu_14.04.tar.gz ubuntu:14.04

      载入镜像:sodu docker load --input ubuntu_14.04.tar.gz或sodu docker load < ubuntu_14.04.tar.gz

      导入的镜像可以使用docker images命令进行查看。

7、上传镜像

     使用docker push命令上传镜像到仓库,默认上传到DockerHub官方仓库(需要登录),命令格式为:docker push NAME[:TAG]。

        先到DockerHub注册后,即可上传自制的镜像。第一次使用时,会提示输入用户名和密码。

转载于:https://my.oschina.net/u/2323379/blog/880723

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值