三、【Docker笔记】Docker镜像

镜像是Docker的三大核心概念之一。Docker在运行容器之前,本地需要存有镜像,若不存在则Docker会首先尝试从默认的镜像仓库中去下载,当然我们也可以去配置自己的仓库,如此就会从我们配置的仓库中去下载。

一、获取镜像

​ 镜像是Docker运行容器的前提条件,我们可以通过 docker pull 命令来下载镜像,语法格式为:docker pull name[:tag]。若我们不指定具体的tag版本,则默认下载最新的版本即 latest标签的镜像。

# 1. 下载一个最新版本的 ubuntu 镜像
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
5bed26d33875: Pull complete
f11b29a9c730: Pull complete
930bda195c84: Pull complete
78bf9a5ad49e: Pull complete
Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

# 2. 下载一个指定的 ubuntu 镜像版本
$ sudo docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pull complete
30bb187ac3fc: Pull complete
b7a5bcc4a58a: Pull complete
Digest: sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4
Status: Downloaded newer image for ubuntu:14.04
docker.io/library/ubuntu:14.04

# 3. 以上两种都是从默认的Docker镜像仓库下载镜像,当然我们也可以从指定仓库下载
$ sudo docker pull dl.aliyun.com:1000/ubuntu

# 4. 镜像下载之后就可以运行了,使用镜像来创建容器
$ sudo docker run -t -i ubuntu /bin/bash
root@95e3c40a7b3b:/# 
root@95e3c40a7b3b:/# ll               # 这里是在容器中执行的命令
total 28
drwxr-xr-x.  21 root root 4096 Apr  5 10:30 ./
drwxr-xr-x.  21 root root 4096 Apr  5 10:30 ../
-rwxr-xr-x.   1 root root    0 Apr  5 10:30 .dockerenv*
drwxr-xr-x.   2 root root 4096 Mar 11 21:05 bin/
drwxr-xr-x.   2 root root    6 Apr 24  2018 boot/
drwxr-xr-x.   5 root root  360 Apr  5 10:30 dev/
drwxr-xr-x.  29 root root 4096 Apr  5 10:30 etc/
drwxr-xr-x.   2 root root    6 Apr 24  2018 home/
drwxr-xr-x.   8 root root   96 May 23  2017 lib/
drwxr-xr-x.   2 root root   34 Mar 11 21:03 lib64/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 media/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 mnt/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 opt/
dr-xr-xr-x. 388 root root    0 Apr  5 10:30 proc/
drwx------.   2 root root   37 Mar 11 21:05 root/
drwxr-xr-x.   5 root root   58 Mar 20 19:20 run/
drwxr-xr-x.   2 root root 4096 Mar 20 19:20 sbin/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 srv/
dr-xr-xr-x.  13 root root    0 Apr 11  2018 sys/
drwxrwxrwt.   2 root root    6 Mar 11 21:05 tmp/
drwxr-xr-x.  10 root root 4096 Mar 11 21:03 usr/
drwxr-xr-x.  11 root root 4096 Mar 11 21:05 var/

二、查看镜像信息

我们可以使用 docker images 命令来列出本地主机上已有的镜像。

# 1. 列出本机上都有哪些镜像
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4e5021d210f6        2 weeks ago         64.2MB
ubuntu              14.04               6e4f1fe62ff1        3 months ago        196MB
# 说明:从上面我们可以看到,在第一节我们下载的镜像都在本机中
# REPOSITORY	来自于哪个仓库
# TAG			镜像的标签信息
# IMAGE ID		镜像的ID号,这是唯一的
# CREATED		镜像的创建时间
# SIZE			镜像大小

除了从仓库下载的镜像文件,我们也可以对已有的本地镜像进行重新打标签,来适应自己项目命名。

# 将 ubuntu:latest 镜像打上自己的 ubuntu 镜像
$ sudo docker tag 192.168.0.128:5000/ubuntu:latest ubuntu:latest
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              14.04              4e5021d210f6        2 weeks ago         64.2MB
192.168.0.128:5000/ubuntu latest       6e4f1fe62ff1        3 months ago        196MB
ubuntu              latest              6e4f1fe62ff1        3 months ago        196MB

我们查看以上的三个本地镜像,其中有两个镜像的ID是相同的,这说明这两个镜像指向的是同一个镜像文件,只是它们有着不同的别名。

# 查看某一个镜像的详细信息
$ sudo docker inspect 6e4f1fe62ff1(镜像ID)
[
    {
        "Id": "sha256:6e4f1fe62ff14a4c8119781d47a3739fa97c190e1df38e868794ad7a7cf50a48",
        "RepoTags": [
            "ubuntu:14.04",
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-12-19T04:23:45.705096855Z",
        "Container": "accd5cd52ef3ece3d4115e456824c3a37749ec10de75c3ad187b74c518b7890a",
        "ContainerConfig": {
            "Hostname": "accd5cd52ef3",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:f23179da08e323ef4bef58a9425152eb39ed802c1b6f8f597113c9fe950e0b24",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:f23179da08e323ef4bef58a9425152eb39ed802c1b6f8f597113c9fe950e0b24",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 196466523,
        "VirtualSize": 196466523,
        "GraphDriver": {
            "Data": {
                "DeviceId": "8",
                "DeviceName": "docker-253:0-67518837-e6323ccccc77b0b03785b672a7de35dd14baa660562dd07f7e9889041c905868",
                "DeviceSize": "10737418240"
            },
            "Name": "devicemapper"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7",
                "sha256:48dc77435ad5c63ea60d91e6ad4828c70e7e61755f99982b0505abb8aaa00872",
                "sha256:3da511183950aa462f667f43fcda0bb5484c5c73eaa94fcd0a94bbd4db396e1c"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-04-05T18:42:30.861232446+08:00"
        }
    }
]

# 查看具体某个参数
$ sudo docker inspect -f {{".Architecture"}} 6e4(镜像ID,可以只使用前面几个字母会自动定位)
amd64

三、搜索镜像

若我们不确定仓库是否有我们需要版本的镜像,我们也可以使用 docker search 命令先搜寻一下。语法为 docker search TERM,其中包含以下参数:

​ --automated=false 仅显示自动创建的镜像

​ --no-trunc=false 输出信息不截断显示

​ -s,--stars=0 指定仅显示评价为指定星级以上的镜像

# 以搜寻 MySQL 镜像为例
$ sudo docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9335                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3347                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   683                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   74
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   66
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      37                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation…   19
mysql/mysql-router                MySQL Router provides transparent routing be…   15
databack/mysql-backup             Back up mysql databases to... anywhere!         14
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0

# 仅显示自动创建的 mysql 镜像
$ sudo docker search --automated=false mysql
Flag --automated has been deprecated, use --filter=is-automated=true instead
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9335                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3347                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   683                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   74
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   66
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      37                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation…   19
mysql/mysql-router                MySQL Router provides transparent routing be…   15
databack/mysql-backup             Back up mysql databases to... anywhere!         14
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0

# 不截断显示镜像
$ sudo docker search --no-trunc=false mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9335                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3347                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   683                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   74
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   66
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      37                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              27                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation…   19
mysql/mysql-router                MySQL Router provides transparent routing be…   15
databack/mysql-backup             Back up mysql databases to... anywhere!         14
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0

# 显示 stars 大于10 的镜像
$ sudo docker search -s=10 mysql
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                         DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                        MySQL is a widely used, open-source relation…   9335                [OK]
mariadb                      MariaDB is a community-developed fork of MyS…   3347                [OK]
mysql/mysql-server           Optimized MySQL Server Docker images. Create…   683                                     [OK]
centos/mysql-57-centos7      MySQL 5.7 SQL database server                   74
mysql/mysql-cluster          Experimental MySQL Cluster Docker images. Cr…   66
centurylink/mysql            Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup          REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                Bitnami MySQL Docker Image                      37                                      [OK]
tutum/mysql                  Base docker image to run a MySQL database se…   34
schickling/mysql-backup-s3   Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                         27                                      [OK]
linuxserver/mysql            A Mysql container, brought to you by LinuxSe…   25
centos/mysql-56-centos7      MySQL 5.6 SQL database server                   19
circleci/mysql               MySQL is a widely used, open-source relation…   19
mysql/mysql-router           MySQL Router provides transparent routing be…   15
databack/mysql-backup        Back up mysql databases to... anywhere!         14
arey/mysql-client            Run a MySQL client from a docker container      13                                      [OK]

四、删除镜像

对于不需要的镜像我们可以将其删除,删除镜像语法为:docker rmi IMAGE[IMAGE...]

# 删除一个镜像
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              <none>              4e5021d210f6        2 weeks ago         64.2MB
ubuntu              14.04               6e4f1fe62ff1        3 months ago        196MB
ubuntu              latest              6e4f1fe62ff1        3 months ago        196MB
$ sudo docker rmi ubuntu:14.04(6e4f1fe62ff1)
# 可为标签或ID删除,需要注意若指定镜像ID删除,则所有的指向该镜像的标签也都删除,此时需要强制删除才可否则删除失败
Error response from daemon: conflict: unable to delete 6e4f1fe62ff1 (must be forced) - image is referenced in multiple repositories
$ sudo docker rmi -f 6e4f1fe62ff1
Untagged: ubuntu:14.04
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4
Deleted: sha256:6e4f1fe62ff14a4c8119781d47a3739fa97c190e1df38e868794ad7a7cf50a48
Deleted: sha256:bf1b2d677f21703e5be92de8b094fbf5cceeea666885053b40b8a5b2f20638b3
Deleted: sha256:9b22c82c6d5a884491639891b0eb053e6e58d6da35ad2d73655d7dd2707d6d73
Deleted: sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              <none>              4e5021d210f6        2 weeks ago         64.2MB

在删除镜像之前,我们建议先查看该镜像是否有一个容器在引用,还有尽量不要使用 -f 强制删除。

# 查看本地有哪些容器
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
95e3c40a7b3b        4e5021d210f6        "/bin/bash"         46 minutes ago      Exited (127) 43 minutes ago                       determined_rubin
# 说明:通过查看Docker容器,我们可知镜像4e5021d210f6被使用,在删除该镜像时就需要注意了。不要强制去删除

若我们发现该镜像以后也不在使用了,正确的做法也不是强制删除,而是先解除容器和镜像之间的依赖,再删除镜像文件。

# 0. 查看本机有哪些容器
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
95e3c40a7b3b        4e5021d210f6        "/bin/bash"         51 minutes ago      Exited (127) 48 minutes ago                       determined_rubin
# 1. 删除不需要的的容器
$ sudo docker rm 95e3c40a7b3b
95e3c40a7b3b
# 2. 再查看发现容器已删除
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# 3. 最后再删除不需要的镜像文件
$ sudo docker rmi 4e5021d210f6
# 4. 查看本地镜像已被删除
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

五、创建镜像

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

1、基于已有镜像的容器创建

使用 docker commit 命令,语法为:docker commit [options] container [repository[:tag]],其中 options 参数可为:

​ -a,--author=“” 作者信息

​ -m,--message=“” 提交消息

​ -p,--pause=true 提交时暂停容器运行

下面我们来创建一个镜像:

# 0. 查看当前镜像
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4e5021d210f6        2 weeks ago         64.2MB

# 1. 启动一个已有的镜像
$ sudo docker run -ti ubuntu /bin/bash
root@56a5d871e739:/# touch test.txt				# 在容器中创建一个文件,用来与当前的镜像做区分
root@56a5d871e739:/# exit						# 退出当前容器

# 2. 提交,创建新的镜像
$ sudo docker commit -m "This is a new file" -a "gongziqi"  56a5d871e739 test
sha256:e5bc93985af09811230818a6905ce21b3428aebfeb6bae48f86772846b324be3

# 3. 再查看镜像
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              e5bc93985af0        26 seconds ago      64.2MB
ubuntu              latest              4e5021d210f6        2 weeks ago         64.2MB

# 4. 查看这两个镜像运行的容器内容
$ sudo docker run -ti ubuntu /bin/bash
root@2ee2858e4b28:/# ll
total 28
drwxr-xr-x.  21 root root 4096 Apr  5 13:07 ./
drwxr-xr-x.  21 root root 4096 Apr  5 13:07 ../
-rwxr-xr-x.   1 root root    0 Apr  5 13:07 .dockerenv*
drwxr-xr-x.   2 root root 4096 Mar 11 21:05 bin/
drwxr-xr-x.   2 root root    6 Apr 24  2018 boot/
drwxr-xr-x.   5 root root  360 Apr  5 13:07 dev/
drwxr-xr-x.  29 root root 4096 Apr  5 13:07 etc/
drwxr-xr-x.   2 root root    6 Apr 24  2018 home/
drwxr-xr-x.   8 root root   96 May 23  2017 lib/
drwxr-xr-x.   2 root root   34 Mar 11 21:03 lib64/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 media/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 mnt/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 opt/
dr-xr-xr-x. 395 root root    0 Apr  5 13:07 proc/
drwx------.   2 root root   37 Mar 11 21:05 root/
drwxr-xr-x.   5 root root   58 Mar 20 19:20 run/
drwxr-xr-x.   2 root root 4096 Mar 20 19:20 sbin/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 srv/
dr-xr-xr-x.  13 root root    0 Apr 11  2018 sys/
drwxrwxrwt.   2 root root    6 Mar 11 21:05 tmp/
drwxr-xr-x.  10 root root 4096 Mar 11 21:03 usr/
drwxr-xr-x.  11 root root 4096 Mar 11 21:05 var/
root@2ee2858e4b28:/# exit
exit
$ sudo docker run -ti test /bin/bash
root@0e4030bfbad3:/# ll
total 28
drwxr-xr-x.  21 root root 4096 Apr  5 13:07 ./
drwxr-xr-x.  21 root root 4096 Apr  5 13:07 ../
-rwxr-xr-x.   1 root root    0 Apr  5 13:07 .dockerenv*
drwxr-xr-x.   2 root root 4096 Mar 11 21:05 bin/
drwxr-xr-x.   2 root root    6 Apr 24  2018 boot/
drwxr-xr-x.   5 root root  360 Apr  5 13:07 dev/
drwxr-xr-x.  29 root root 4096 Apr  5 13:07 etc/
drwxr-xr-x.   2 root root    6 Apr 24  2018 home/
drwxr-xr-x.   8 root root   96 May 23  2017 lib/
drwxr-xr-x.   2 root root   34 Mar 11 21:03 lib64/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 media/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 mnt/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 opt/
dr-xr-xr-x. 395 root root    0 Apr  5 13:07 proc/
drwx------.   2 root root   58 Apr  5 13:05 root/
drwxr-xr-x.   5 root root   58 Mar 20 19:20 run/
drwxr-xr-x.   2 root root 4096 Mar 20 19:20 sbin/
drwxr-xr-x.   2 root root    6 Mar 11 21:03 srv/
dr-xr-xr-x.  13 root root    0 Apr 11  2018 sys/
-rw-r--r--.   1 root root    0 Apr  5 13:05 test.txt
drwxrwxrwt.   2 root root    6 Mar 11 21:05 tmp/
drwxr-xr-x.  10 root root 4096 Mar 11 21:03 usr/
drwxr-xr-x.  11 root root 4096 Mar 11 21:05 var/

# 观察:test镜像创建的容器中是有 test.txt 文件的

2、基于本地模板导入

可以从操作系统模板文件导入一个镜像。假设我们本地已经有了一个 ubuntu.tar.gz 文件,我们可通过以下命令来导入:

# 导入本地模板
$ sudo ll
$ sudo docker images
$ sudo cat ubuntu-16.04-x86_64.tar.gz | docker import - ubuntu:16.04
$ sudo docker images

3、基于Dockerfile创建

这种创建方式,我们后面专门用一节来介绍。

六、存出和载入镜像

关于Docker镜像的存出和载入,我们可以使用以下两个命令:docker save / docker load。

# 1. 存出镜像,将某个镜像存出(注意观察当前工作目录前后的文件变化)
	# 1.1 查看镜像,和当前工作目录文件
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              4e5021d210f6        2 weeks ago         64.2MB

$ sudo ll
总用量 242148
drwxr-xr-x.  4 root root      4096 4月   2 19:09 apache
drwxr-xr-x. 11 root root      4096 3月  29 15:24 apache-activemq-5.15.12
-rw-r--r--.  1 root root  62666908 3月  29 14:40 apache-activemq-5.15.12-bin.tar.gz
drwxr-xr-x.  8   10  143      4096 12月 13 2016 jdk1.8.0_121
-rw-r--r--.  1 root root 183246769 3月  29 15:17 jdk-8u121-linux-x64.tar.gz
-rw-r--r--.  1 root root   1208398 4月   5 21:16 ubuntu-16.04-x86_64.tar.gz

	# 1.2 存出镜像,并查看当前工作目录
$ sudo docker save -o ubuntu_latest.tar ubuntu:latest
$ sudo ll
总用量 307152
drwxr-xr-x.  4 root root      4096 4月   2 19:09 apache
drwxr-xr-x. 11 root root      4096 3月  29 15:24 apache-activemq-5.15.12
-rw-r--r--.  1 root root  62666908 3月  29 14:40 apache-activemq-5.15.12-bin.tar.gz
drwxr-xr-x.  8   10  143      4096 12月 13 2016 jdk1.8.0_121
-rw-r--r--.  1 root root 183246769 3月  29 15:17 jdk-8u121-linux-x64.tar.gz
-rw-r--r--.  1 root root   1980762 4月   5 21:18 ubuntu-16.04-x86_64.tar.gz
-rw-------.  1 root root  66612224 4月   5 21:25 ubuntu_latest.tar

# 2. 载入镜像,将某个镜像载入系统
$ sudo docker load --input ubuntu_latest.tar
# 或
$ sudo docker load < ubuntu_latest.tar
Loaded image: ubuntu:latest

七、上传镜像

若我们希望将自己的镜像上传到仓库供别人使用,可使用 docker push 命令,默认上传到官方仓库。语法:docker push NAME[:TAG]

# 将我们上面的 test 镜像上传到仓库
$ sudo docker tag test:latest user/test:latest		# 创建一个镜像标签
$ sudo docker push user/test:latest

# 说明:在第一次上传时,需要我们输入登录信息
上一篇:从mysql8.0.15升级到8.0.16


下一篇:Java IO (1) - InputStream