可以通过使用docker pull命令从网络上下载镜像,该命令格式为:
docker pull NAME[:TAG]
例如从Docker Hub的Ubuntu仓库下载一个最新的Ubuntu操作系统的镜像
sudo docker pull ubuntu # ubuntu:latest
sudo docker pull ubuntu: 16.04 # ubuntu 16.04镜像
sudo docker pull registry.hub.docker.com/ubuntu:latest 相当于第一条镜像下载命令
用户也可以从其他注册服务器的仓库下载,需要在仓库名称前指定完整的仓库注册服务器地址。例如从DockerPool社区的镜像源dl.dockerpool.com下载最新的Ubuntu镜像
sudo docker pull dl.dockerpool.com:5000/ubuntu
下载镜像到本地后,即可随时使用该镜像了,例如利用该镜像创建一个容器,在其中运行bash应用。
sudo docker run -t -i ubuntu /bin/bash
使用docker images 命令可以列出本地主机上已有的镜像
zss@zss-All-Series:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 5cdf8562de78 3 days ago 3.69GB
nvidia/cuda 10.0-cudnn7-devel bdc0497c2295 13 days ago 3.07GB
在列出的信息中,可以看到几个字段信息:
为方便后续工作中使用这个镜像,还可以使用docker tag命令为本地镜像添加一个新的标签。例如添加REPOSITORY为docker-ubuntu,TAG为16.04
zss@zss-All-Series:~$ sudo docker tag 5cdf8562de78 docker-ubuntu:16.04
zss@zss-All-Series:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-ubuntu 16.04 5cdf8562de78 3 days ago 3.69GB
nvidia/cuda 10.0-cudnn7-devel bdc0497c2295 13 days ago 3.07GB
同一个镜像文件可以有多个标签(别名),标签起到了引用或快捷方式的作用。
使用docker inspect 命令可以获取该镜像的详细信息。
zss@zss-All-Series:~$ sudo docker inspect 5cdf8562de78
[
{
"Id": "sha256:5cdf8562de78509db3e3b9fb6c4d99accf057de16aa48cc56d45973616f9092e",
"RepoTags": [],
"RepoDigests": [],
"Parent": "sha256:5cc47f7a1e17bdb3e047fb1b55b0f2912d35ddc0b19bc808fc5f265ea6d23cb6",
"Comment": "",
"Created": "2019-08-09T06:02:27.361676227Z",
"Container": "ca73951cdbb3d7061d0ad7954317d253c3ea6aefc35114f5c1901ae63d484d84",
"ContainerConfig": {
"Hostname": "",
"Domainname": "",
"User": "keras",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"CUDA_VERSION=10.0.130",
"CUDA_PKG_VERSION=10-0=10.0.130-1",
"LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64",
"NVIDIA_VISIBLE_DEVICES=all",
"NVIDIA_DRIVER_CAPABILITIES=compute,utility",
"NVIDIA_REQUIRE_CUDA=cuda>=10.0 brand=tesla,driver>=384,driver<385 brand=tesla,driver>=410,driver<411",
"NCCL_VERSION=2.4.7",
"LIBRARY_PATH=/usr/local/cuda/lib64/stubs",
"CUDNN_VERSION=7.6.2.24",
"CONDA_DIR=/opt/conda",
"NB_USER=keras",
"NB_UID=1000"
],
"Cmd": [
"|1",
"python_version=3.5",
"/bin/sh",
"-c",
"conda config --append channels conda-forge"
],
"ArgsEscaped": true,
"Image": "sha256:5cc47f7a1e17bdb3e047fb1b55b0f2912d35ddc0b19bc808fc5f265ea6d23cb6",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.nvidia.cudnn.version": "7.6.2.24",
"maintainer": "NVIDIA CORPORATION <cudatools@nvidia.com>"
}
},
"DockerVersion": "18.09.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "keras",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"CUDA_VERSION=10.0.130",
"CUDA_PKG_VERSION=10-0=10.0.130-1",
"LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64",
"NVIDIA_VISIBLE_DEVICES=all",
"NVIDIA_DRIVER_CAPABILITIES=compute,utility",
"NVIDIA_REQUIRE_CUDA=cuda>=10.0 brand=tesla,driver>=384,driver<385 brand=tesla,driver>=410,driver<411",
"NCCL_VERSION=2.4.7",
"LIBRARY_PATH=/usr/local/cuda/lib64/stubs",
"CUDNN_VERSION=7.6.2.24",
"CONDA_DIR=/opt/conda",
"NB_USER=keras",
"NB_UID=1000"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:5cc47f7a1e17bdb3e047fb1b55b0f2912d35ddc0b19bc808fc5f265ea6d23cb6",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.nvidia.cudnn.version": "7.6.2.24",
"maintainer": "NVIDIA CORPORATION <cudatools@nvidia.com>"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 3687552951,
"VirtualSize": 3687552951,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/f1f4262cd65f2c6e2e60c70349f8959b61dfa8e8c0c6f26fe13818af89497aaa/diff:/var/lib/docker/overlay2/845664810b812632ee3d85cd6da75e8d9fc4284fafe483c9868b751d12cf5fac/diff:/var/lib/docker/overlay2/4203bc7b23cb21646b037cfd8c12182b210da16fa9c9bd60acce2a23664b4160/diff:/var/lib/docker/overlay2/dc3ab227fc8aa47e4bf94df0b205783a4118ef6e4e262324014557b762828609/diff:/var/lib/docker/overlay2/1538399e303b3b03c898cf087943b0e530e773d8b8bf636da58bb22c7b973e94/diff:/var/lib/docker/overlay2/69a3b593229d2abec52f130f7d07815225ce4f138be029fa788d970eb460d63d/diff:/var/lib/docker/overlay2/434ca52e041e722a8ec9f0bfb774965d6c75e90c1cd6cd1dd24b1ea11e6f0960/diff:/var/lib/docker/overlay2/d4fcefde968e1d4b8f4baff3ef20be4a9a63a04c94c12a3125b9c60804e3b274/diff:/var/lib/docker/overlay2/b9906632fe8931b170c6127bab625e8ba3ea14bbcbc3d1bbb5f7e8f815495992/diff:/var/lib/docker/overlay2/90605f5080661350a5755253927845400f6f27150b33f19a7ecf6d0337d8e1b2/diff:/var/lib/docker/overlay2/3c90a6de19690300cce9cbd7c5e0fa7f40acc6dd6e294e96ae60cdea06f7b7da/diff:/var/lib/docker/overlay2/a70692f210829526a42d70df96d3a57da07c68f9b4e55a11259861921e15e29c/diff:/var/lib/docker/overlay2/3472931341f16ab36c179149851777ccd1dfed74eb82d5207420f29c9018fccc/diff",
"MergedDir": "/var/lib/docker/overlay2/a763094d4a711f1e3a8ea713905581b72474f9d90279d47be73fdff08f87572e/merged",
"UpperDir": "/var/lib/docker/overlay2/a763094d4a711f1e3a8ea713905581b72474f9d90279d47be73fdff08f87572e/diff",
"WorkDir": "/var/lib/docker/overlay2/a763094d4a711f1e3a8ea713905581b72474f9d90279d47be73fdff08f87572e/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:543791078bdb84740cb5457abbea10d96dac3dea8c07d6dc173f734c20c144fe",
"sha256:c56e09e1bd18e5e41afb1fd16f5a211f533277bdae6d5d8ae96a248214d66baf",
"sha256:a31dbd3063d77def5b2562dc8e14ed3f578f1f90a89670ae620fd62ae7cd6ee7",
"sha256:b079b3fa8d1b4b30a71a6e81763ed3da1327abaf0680ed3ed9f00ad1d5de5e7c",
"sha256:e804f86593d8c76e6f48b2f1661f632f28b0011e1a9f1b99e2e44a80a9c985bf",
"sha256:5840a38239d1ef3c01ec381a863bd4218182ba753c054e0d8e2710e8d64ce6dc",
"sha256:bd50dcfc2cdc707689c8df48be09bd7d55781871369cfe8dbaaedb626fba45ce",
"sha256:0f3b12fd3cdefc9b554a042c853e4a9fa22be18a36aa246f88656784383bd210",
"sha256:5d780eb345bf815e6839242998cc3face08780b91c987068d77283c656f71a93",
"sha256:a3d40aea439374f04448076b771b8870e150012c83435ab90dbe3ace4dc47d44",
"sha256:e60b7844249663ba7ab9b6540285402e7c5f099f51bb48b326e96f12a483d9d2",
"sha256:e0801a172939da45adf45382d27fd84a0ca3b59aa518e4382ab1b1ce8610936c",
"sha256:be61387ac01b814ac8610627ddee46ea765b7c54164ad7f6cc71647b7a9cd5c5",
"sha256:caa6ad888a75772876fd2b2770af7be1763e231532eed4a87f688b825d1a19b7"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
docker inspect 命令返回的是一个JSON格式的消息,如果我们只要其中一项内容,可以使用 -f 参数来指定:
zss@zss-All-Series:~$ sudo docker inspect -f {{".Created"}} 5cd
2019-08-09T06:02:27.361676227Z
在指定镜像ID的时候,通常使用该ID的前若干个字符组成的可区分字串来替代完整的ID。
使用 docker search 命令可以搜索远端仓库中共享的镜像,默认搜索Docker Hub官方仓库中的镜像。用法为docker search TERM,支持的参数包括:
--automated=false # 仅显示自动创建的镜像
--no-trunc=false #输出信息不截断显示
-s, --stars=0 #指定仅显示评价为指定星级以上的镜像
例如搜索带mysql关键字的镜像如下显示:
zss@zss-All-Series:~$ sudo docker search mysql
[sudo] password for zss:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 8485 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2931 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 626 [OK]
percona Percona Server is a fork of the MySQL relati… 441 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 60
centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 50
deitch/mysql-backup Automated and scheduled mysql database dumps… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 32
bitnami/mysql Bitnami MySQL Docker Image 30 [OK]
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 21
prom/mysqld-exporter 20 [OK]
centos/mysql-56-centos7 MySQL 5.6 SQL database server 15
circleci/mysql MySQL is a widely used, open-source relation… 13
mysql/mysql-router MySQL Router provides transparent routing be… 12
arey/mysql-client Run a MySQL client from a docker container 10 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
imega/mysql-client Size: 36 MB, alpine:3.5, Mysql client: 10.1.… 6 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 4 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 1 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
可以看到返回很多包含关键字的镜像,其中包括镜像名字、描述、星级、是否官方创建、是否自动创建等。
默认的输出结果将按照星级评价进行排序。官方的镜像说明是官方项目组创建和维护的,automated资源则允许用户验证镜像的来源和内容。
使用镜像的标签删除镜像
使用docker rmi 命令可以删除镜像,命令格式为:
docker rmi IMAGE[IMAGE...] # IMAGE可以为标签或ID
当同一个镜像拥有多个标签的时候,docker rmi 命令只是删除了该镜像多个标签中的指定标签而已,并不影响镜像文件。但当镜像只剩下一个标签的时候就要小心了,此时再使用docker rmi 命令会彻底删除该镜像。
使用镜像ID删除镜像
当使用docker rmi 命令后面跟上镜像的ID时,会先尝试删除所有指向该镜像的标签,然后删除该镜像文件本身。
注意,当有该镜像创建的容器存在时,镜像文件默认是无法被删除的,例如:
先利用ubuntu镜像创建一个简单的容器,输出一句话“hello”:
zss@zss-All-Series:~$ sudo docker run ubuntu echo 'hello'
hello
zss@zss-All-Series:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-ubuntu 16.04 5cdf8562de78 3 days ago 3.69GB
nvidia/cuda 10.0-cudnn7-devel bdc0497c2295 13 days ago 3.07GB
ubuntu latest 3556258649b2 2 weeks ago 64.2MB
使用docker ps -a命令查看本机上存在的所有容器:
zss@zss-All-Series:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ad3d39941cc ubuntu "echo hello" 4 minutes ago Exited (0) 4 minutes ago admiring_booth
2b63aef56a10 5cdf8562de78 "/bin/sh -c 'conda i…" 3 days ago Exited (1) 3 days ago practical_keldysh
365c9dc17672 5cdf8562de78 "/bin/sh -c 'conda i…" 3 days ago Exited (2) 3 days ago compassionate_matsumoto
7af09d33da0f 5cdf8562de78 "/bin/sh -c 'conda i…" 3 days ago Exited (1) 3 days ago pedantic_leakey
可以看到,后台存在几个退出状态的容器,第一个是刚基于ubuntu:latest 镜像创建的。
试图删除该镜像,Docker会提示有容器正在运行,无法删除;
如果强行删除镜像,可以使用-f 参数:
sudo docker rmi -f ubuntu # 不推荐这样删除,会存在遗留问题,强制删除的镜像换了新的ID继续存在系统中
正确的做法是,先删除依赖该镜像的所有容器,再来删除镜像首先删除容器3ad3d39941cc
sudo docker rm 3ad3d39941cc
然后再删除镜像
创建镜像的方法有三种:基于已有镜像的容器创建、基于本地模板导入、基于Dockerfile创建。
基于已有镜像的容器创建
该方法主要是使用docker commit 命令,其命令格式为docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]],主要选项包括:
-a, --author=" " # 作者信息
-m, --message=" " # 提交信息
-p, --pause=true # 提交时暂停容器运行
下面创建一个新镜像,首先,启动一个镜像,并在其中进行修改操作,例如创建一个test文件,之后退出:
zss@zss-All-Series:~$ sudo docker run -ti ubuntu /bin/bash
root@8a21294d9bb7:/# touch test
root@8a21294d9bb7:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
root@8a21294d9bb7:/# rm test
root@8a21294d9bb7:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@8a21294d9bb7:/# exit
exit
记住容器的ID为8a21294d9bb7
该容器与原来的ubuntu镜像相比,已经发生了改变,可以使用docker commit 命令来提交一个新的镜像。提交时可以使用ID或名称来指定容器
zss@zss-All-Series:~$ sudo docker commit -m "Added a new file" -a "Docker Newbee" 5108340a3fca test
sha256:c6afddbb5ab774c3f27f92138dd26a2ce701f7b3817e8f3b481c714891ce183e
zss@zss-All-Series:~$ sudo docker images # 查看本地镜像列表,即可看到新创建的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest c6afddbb5ab7 4 seconds ago 64.2MB
nvidia/cuda 10.0-cudnn7-devel bdc0497c2295 2 weeks ago 3.07GB
ubuntu latest 3556258649b2 2 weeks ago 64.2MB
基于本地模板导入
也可以直接从一个操作系统模板文件导入一个镜像。在这里,推荐使用OpenVZ提供的模板来创建。OpenVZ模板的下载地址为http://openvz.org/Download/templates/precreated
比如,下载一个ubuntu-16.04的模板压缩包后,可以使用一下命令导入:
sudo cat ubuntu-16.04-x86_64-minimal.tar.gz | docker import - ubuntu:16.04
然后查看新导入的镜像,已经在本地存在了
zss@zss-All-Series:~$ sudo docker images
可以使用docker save 和 docker load 命令来存入和载出镜像
存入镜像
如果要存入镜像到本地文件,可以使用docker save 命令,例如存出本地的ubuntu:latest镜像为文件ubuntu_16.04.tar
zss@zss-All-Series:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest c6afddbb5ab7 15 minutes ago 64.2MB
nvidia/cuda 10.0-cudnn7-devel bdc0497c2295 2 weeks ago 3.07GB
ubuntu latest 3556258649b2 2 weeks ago 64.2MB
zss@zss-All-Series:~$ sudo docker save -o ubuntu_16.04.tar ubuntu:latest
载入镜像
可以使用docker load 从存在的本地文件中再导入到本地镜像库,例如从文件ubuntu_16.04.tar导入镜像到本地镜像列表
$ sudo docker load --input ubuntu_16.04.tar
$ sudo docker load < ubuntu_16.04.tar
两个语句等价,这将导入镜像以及其相关的元数据信息(包括标签等),可以使用 docker images 命令查看
可以使用docker push 命令上传镜像到仓库,默认上传到DockerHub官方仓库(需要登录),命令格式为docker push NAME[:TAG]
用户在DockerHub网站注册后,即可上传自制的镜像,例如用户user上传本地的test:latest 镜像,可以先添加新的标签user/test:latest,然后用docker push 命令上传镜像
sudo docker tag test :latest user/test:latest
sudo docker push user/test:latest
第一次使用,会提示输入登录信息或进行注册