Docker 使用方法总结之:镜像

  1. 查找
    命令:docker search [OPTIONS] TERM
    相关参数:
    Search the Docker Hub for images
      --automated=false    Only show automated builds
      --no-trunc=false     Don't truncate output
      -s, --stars=0        Only displays with at least x stars
    例子:
    [root@localhost ~]# docker search mysql
    NAME                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mysql                        MySQL is a widely used, open-source relati...   655       [OK]       
    tutum/mysql                  MySQL Server image - listens in port 3306....   115                  [OK]
    orchardup/mysql                                                              37                   [OK]
    centurylink/mysql            Image containing mysql. Optimized to be li...   21                   [OK]
    ...................
  2. 获取
    命令:docker pull [OPTIONS] NAME[:TAG]
    参数:  -a, --all-tags=false    Download all tagged images in the repository
    例子:
    [root@localhost ~]# docker pull tutum/mysql
    Pulling repository tutum/mysql
    26c0c52dc79a: Pulling dependent layers
    511136ea3c5a: Download complete
    27d47432a69b: Download complete
    5f92234dcf1e: Download complete
    51a9c7c1f8bb: Download complete
    5ba9dab47459: Download complete
    d26d9affc74e: Download complete
    c8420faa4614: Download complete
    .........
  3. 运行image
    命令:docker run -t -i tutum/mysql /bin/bash
  4. 查看列表
    命令:docker images [OPTIONS] [NAME]
    参数
      -a, --all=false      Show all images (by default filter out the intermediate image layers)
      -f, --filter=[]      Provide filter values (i.e. 'dangling=true')
      --no-trunc=false     Don't truncate output
      -q, --quiet=false    Only show numeric IDs
    例子
    [root@localhost ~]# docker images -a
    REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
                                  6941bfcbbfca        10 days ago         0 B
    eric/ubuntu           latest              52671e55fb86        6 weeks ago         270.3 MB
                                  c8420faa4614        3 months ago        192.7 MB
                                  d26d9affc74e        3 months ago        192.7 MB
                                  5ba9dab47459        3 months ago        192.7 MB
  5. 更改TAG
    更改之前:
    [root@localhost ~]# docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    centos                7                   fd44297e2ddb        10 days ago         229.6 MB
    centos                centos7             fd44297e2ddb        10 days ago         229.6 MB
    centos                latest              fd44297e2ddb        10 days ago         229.6 MB
    eric/ubuntu           latest              52671e55fb86        6 weeks ago         270.3 MB
    tutum/mysql           latest              26c0c52dc79a        3 months ago        322.5 MB
    centos_6_5            ansible             f01c1b138488        10 months ago       322.4 MB
    chug/ubuntu12.10x64   latest              0b96c14dafcd        13 months ago       270.3 MB
    执行命令:
    docker tag tutum/mysql:latest localrepo:newtag
    更改之后:
    [root@localhost ~]# docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    centos                7                   fd44297e2ddb        10 days ago         229.6 MB
    centos                centos7             fd44297e2ddb        10 days ago         229.6 MB
    centos                latest              fd44297e2ddb        10 days ago         229.6 MB
    eric/ubuntu           latest              52671e55fb86        6 weeks ago         270.3 MB
    tutum/mysql           latest              26c0c52dc79a        3 months ago        322.5 MB
    localrepo             newtag              26c0c52dc79a        3 months ago        322.5 MB

    centos_6_5            ansible             f01c1b138488        10 months ago       322.4 MB
    chug/ubuntu12.10x64   latest              0b96c14dafcd        13 months ago       270.3 MB
  6. 查看明细
    命令:docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
    参数:-f, --format=""    Format the output using the given go template.
    例子:
    [root@localhost ~]# docker inspect f01c1b138488
  7. 删除
    命令:docker rmi [OPTIONS] IMAGE [IMAGE...]
    参数: 
    -f, --force=false    Force removal of the image
    --no-prune=false     Do not delete untagged parents
    例子:
    [root@localhost ~]# docker rmi 26c
    Error response from daemon: Conflict, cannot delete 26c0c52dc79a because the container 002ca178e599 is using it, use -f to force
    上面出错的原因是因为该镜像正在被002ca178e599的容器使用,所以不能删除,所以首先需要使用docker rm删除容器,然后再删除,相关的AUFS文件也会被删除
    [root@localhost ~]# docker rm 002
    002
    [root@localhost ~]# docker rmi 26c
    Untagged: localrepo:newtag
    Deleted: 26c0c52dc79af8acf07a18cd1693bfae91926f496ee4d1c1dbe29a014394b7c0
    Deleted: ba600267bf1a82675eeee78a2e18c49306c46888ca3cf33f372be4b80334d4f0
    Deleted: 6d79a1795e744a9c2c4409526f90c9711d96d0e944a7f485fc31acf7e44fb210
    Deleted: cb6a06a808d4329b1bba405b9e3e733b1c379fb55979e8631e918f6c14c6023a
    Deleted: 8d466d27bccad3502d579a2e0e6fc28fa8a46e32b5b453e7387a61eef7c1be8a
    ......
  8. 创建
    由于
    1. 基于Container创建
      1. 启动一个容器
        [root@localhost ~]# docker run -ti f01 /bin/bash
      2. 在启动的容器中安装ansible,git等工具
      3. 查询Container ID
        [root@localhost ~]# docker ps
        CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
        52ec71824717        centos_6_5:ansible   "/bin/bash"         31 seconds ago      Up 30 seconds                           high_mcclintock
      4. 执行创建命令,返回值就是生成的Image ID
        [root@localhost ~]# docker commit -m 'Ansible Image,and git, sample yml files' -a 'Eric.sunah' 52ec71824717 eric/ansible
        6e3832e7c6e1aba47aefe1430c346a1dbf9a0454c37a907d293eaf7056d11d91
      5. 查看新的列表
        [root@localhost ~]# docker images
        REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
        eric/ansible          latest              6e3832e7c6e1        2 minutes ago       629.7 MB
        eric/ubuntu           latest              52671e55fb86        6 weeks ago         270.3 MB
        centos_6_5            ansible             f01c1b138488        10 months ago       322.4 MB
        chug/ubuntu12.10x64   latest              0b96c14dafcd        13 months ago       270.3 MB
  9. 另存为
    命令:docker save [OPTIONS] IMAGE [IMAGE...]
    参数:  -o, --output=""    Write to a file, instead of STDOUT
    例子:
    docker save -o centos6_ansible.tar eric/ansible
  10. 导入
     命令:docker load [OPTIONS]
    参数:   -i, --input=""     Read from a tar archive file, instead of STDIN
    例子:
    docker load --input centos6_ansible.tar
  11. 上传
    首先取个便于识别的TAG
    [root@localhost ~]# docker tag 6e38 sun7545526/template:ansible
    [root@localhost ~]# docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    sun7545526/template   ansible             6e3832e7c6e1        17 minutes ago      629.7 MB
    eric/ansible          latest              6e3832e7c6e1        17 minutes ago      629.7 MB

    执行上传命令,第一次上传的时候会要求输入用户名,密码,邮箱信息:
    [root@localhost ~]# docker push sun7545526/template:ansible
    The push refers to a repository [sun7545526/template] (len: 1)
    Sending image list
    Pushing repository sun7545526/template (1 tags)
    511136ea3c5a: Image already pushed, skipping
    1d6ebc5c68b1: Image already pushed, skipping
    f01c1b138488: Image already pushed, skipping
    6e3832e7c6e1: Image successfully pushed
    Pushing tag for rev [6e3832e7c6e1] on {https://cdn-registry-1.docker.io/v1/repositories/sun7545526/template/tags/ansible}

你可能感兴趣的:(云计算介绍与实战)