Docker 镜像常用命令

一 列出镜像列表
使用docker images来列出本地主机上的镜像。
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
各个选项说明:
REPOSTITORY:表示镜像的仓库源。
TAG:镜像的标签。
IMAGE ID:镜像ID。
CREATED:镜像创建时间。
SIZE:镜像大小。
同一仓库源可以有多个TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、latest等多个不同的版本,我们使用REPOSTITORY:TAG来定义不同的镜像。
所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:
[root@localhost ~]# docker run -t -i ubuntu:15.10 /bin/bash
root@e26a0172a0aa:/#  
如果要使用版本为latest的ubuntu系统镜像来运行容器时,命令如下:
[root@localhost ~]# docker run -t -i ubuntu:latest /bin/bash
root@bf67651b183f:/# 
如果不指定一个镜像的版本标签,例如只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。
 
二 获取一个新的镜像
当在本地主机上使用一个不存在的镜像时,Docker就会自动下载这个镜像。如果想预先下载这个镜像,可以使用docker pull命令来下载它。
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
d54efb8db41d: Pull complete 
f8b845f45a87: Pull complete 
e8db7bf7c39f: Pull complete 
9654c40e9079: Pull complete 
6d9ef359eaaa: Pull complete 
Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535
 
三 查找镜像
我们可以从Docker Hub网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
我们也可以使用docker search命令来搜索镜像。
比如我们需要一个mysql的镜像来作为我们的数据库服务。我们可以通过docker search命令搜索mysql来寻找适合我们的镜像。
[root@localhost ~]# docker search mysql
INDEX       NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                           MySQL is a widely used, open-source relati...   4035      [OK]       
docker.io   docker.io/mysql/mysql-server              Optimized MySQL Server Docker images. Crea...   279                  [OK]
docker.io   docker.io/centurylink/mysql               Image containing mysql. Optimized to be li...   49                   [OK]
docker.io   docker.io/sameersbn/mysql                                                                 44                   [OK]
docker.io   docker.io/zabbix/zabbix-server-mysql      Zabbix Server with MySQL database support       29                   [OK]
docker.io   docker.io/zabbix/zabbix-web-nginx-mysql   Zabbix frontend based on Nginx web-server ...   15                   [OK]
docker.io   docker.io/appcontainers/mysql             Centos/Debian Based Customizable MySQL Con...   8                    [OK]
docker.io   docker.io/marvambass/mysql                MySQL Server based on Ubuntu 14.04              7                    [OK]
docker.io   docker.io/dnhsoft/mysql-utf8              Inherits the official MySQL image configur...   5                    [OK]
docker.io   docker.io/alterway/mysql                  Docker Mysql                                    3                    [OK]
docker.io   docker.io/bitnami/mysql                   Bitnami MySQL Docker Image                      3                    [OK]
docker.io   docker.io/frodenas/mysql                  A Docker Image for MySQL                        3                    [OK]
docker.io   docker.io/debezium/example-mysql          Example MySQL database server with a simpl...   2                    [OK]
docker.io   docker.io/drupaldocker/mysql              MySQL for Drupal                                2                    [OK]
docker.io   docker.io/yfix/mysql                      Yfix docker built mysql                         2                    [OK]
docker.io   docker.io/coscale/mysql                   CoScale custom configuration of the offici...   1                    [OK]
docker.io   docker.io/lysender/mysql                  MySQL base image using Ubuntu 16.04 Xenial      1                    [OK]
docker.io   docker.io/newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL data...   1                    [OK]
docker.io   docker.io/tozd/mysql                      MySQL (MariaDB fork) Docker image.              1                    [OK]
docker.io   docker.io/captomd/mysql                   CaptoMD mysql configuration                     0                    [OK]
docker.io   docker.io/cloudposse/mysql                Improved `mysql` service with support for ...   0                    [OK]
docker.io   docker.io/datajoint/mysql                 MySQL image pre-configured to work smoothl...   0                    [OK]
docker.io   docker.io/nanobox/mysql                   MySQL service for nanobox.io                    0                    [OK]
docker.io   docker.io/projectomakase/mysql            Docker image for MySQL                          0                    [OK]
docker.io   docker.io/treenity/mysql                  Mysql5.7 with OSx permission fixs               0                    [OK]
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
STARS:星级,表示该镜像的欢迎程度
OFFICIAL:是否docker官方发布
AUTOMATED:允许用户验证镜像的来源和内容。
 
四 基于已有镜像的容器创建镜像
下面演示如何创建一个新镜像。
1、启动一个镜像,并在其中进行修改操作,例如创建一个test文件,之后退出:
[root@localhost ~]# docker run -t -i ubuntu:15.10 /bin/bash
root@e422f73a5651:/# touch test
root@e422f73a5651:/# ll
total 20
drwxr-xr-x  21 root root 4096 Mar 19 08:21 ./
drwxr-xr-x  21 root root 4096 Mar 19 08:21 ../
-rwxr-xr-x   1 root root    0 Mar 19 08:20 .dockerenv*
drwxr-xr-x   2 root root 4096 Jul  6  2016 bin/
drwxr-xr-x   2 root root    6 Oct 19  2015 boot/
drwxr-xr-x   5 root root  380 Mar 19 08:20 dev/
drwxr-xr-x  42 root root 4096 Mar 19 08:20 etc/
drwxr-xr-x   2 root root    6 Oct 19  2015 home/
drwxr-xr-x   8 root root   90 Sep 13  2015 lib/
drwxr-xr-x   2 root root   33 Jul  6  2016 lib64/
drwxr-xr-x   2 root root    6 Jul  6  2016 media/
drwxr-xr-x   2 root root    6 Oct 19  2015 mnt/
drwxr-xr-x   2 root root    6 Jul  6  2016 opt/
dr-xr-xr-x 237 root root    0 Mar 19 08:20 proc/
drwx------   2 root root   35 Jul  6  2016 root/
drwxr-xr-x   6 root root   68 Mar 19 08:20 run/
drwxr-xr-x   2 root root 4096 Jul 22  2016 sbin/
drwxr-xr-x   2 root root    6 Jul  6  2016 srv/
dr-xr-xr-x  13 root root    0 Mar 19 08:20 sys/
-rw-r--r--   1 root root    0 Mar 19 08:21 test
drwxrwxrwt   2 root root    6 Jul  6  2016 tmp/
drwxr-xr-x  10 root root   97 Jul 22  2016 usr/
drwxr-xr-x  11 root root  128 Jul 22  2016 var/
root@e422f73a5651:/# exit
exit
2、利用容器ID创建镜像
此时容器ID为e422f73a5651,我们可以通过命令 docker commit来提交容器副本,创建镜像。
[root@localhost ~]# docker commit -m "Added a nre file" -a "cakin24" e422f73a5651 test
sha256:c489bd9009f2ee4ab1b07bd3f33965a8ea0f38a789f12e16bb374542b1b63087
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
test                    latest              c489bd9009f2        12 seconds ago      137.2 MB
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
 
五 设置镜像标签
我们可以使用docker tag命令,为镜像添加一个新的标签。
[root@localhost ~]# docker tag c489bd9009f2 cakin24/test
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
cakin24/test            latest              c489bd9009f2        9 minutes ago       137.2 MB
test                    latest              c489bd9009f2        9 minutes ago       137.2 MB
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
 
六 删除镜像
1、使用镜像标签删除镜像
[root@localhost ~]# docker rmi cakin24/test
Untagged: cakin24/test:latest
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
test                    latest              c489bd9009f2        42 minutes ago      137.2 MB
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
2、使用镜像ID删除镜像
[root@localhost ~]# docker rmi c489bd9009f2
Untagged: test:latest
Deleted: sha256:c489bd9009f2ee4ab1b07bd3f33965a8ea0f38a789f12e16bb374542b1b63087
Deleted: sha256:de1dfcf9ac460c33761370793862858c98c8713d038bd0f67088969183316852
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
 
七 存出镜像
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/httpd         2.2                 3624a4b77da8        2 weeks ago         169.9 MB
docker.io/ubuntu        latest              0ef2e08ed3fa        2 weeks ago         130 MB
docker.io/hello-world   latest              48b5124b2768        9 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        7 months ago        137.2 MB
[root@localhost ~]# docker save -o ubuntar.tar docker.io/ubuntu:15.10
 

猜你喜欢

转载自cakin24.iteye.com/blog/2372334