二.CentOS 7下Docker镜像操作
1.获取镜像
从 Docker 镜像仓库获取镜像的命令是 docker pull 。其命令格式为:
docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
比如:docker pull ubuntu:16.04
相当于docker pull registry.hub.docker.com/ubuntu:16.04
[root@VM_0_15_centos ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
bf5d46315322: Pull complete
9f13e0ac480c: Pull complete
e8988b5b3097: Pull complete
40af181810e7: Pull complete
e6f7c7e5c03e: Pull complete
Digest: sha256:147913621d9cdea08853f6ba9116c2e27a3ceffecf3b492983ae97c3d643fbbe
Status: Downloaded newer image for ubuntu:16.04
2.列出镜像 docker images
或者 docker image ls
列表包含了 仓库名 、 标签 、 镜像 ID 、 创建时间 以及 所占用的空间 。
[root@VM_0_15_centos ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 16.04 7aa3602ab41e 2 weeks ago 115 MB
docker.io/ubuntu latest 735f80812f90 2 weeks ago 83.5 MB
docker.io/hello-world latest 2cb0d9787c4d 5 weeks ago 1.85 kB
[root@VM_0_15_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu 16.04 7aa3602ab41e 2 weeks ago 115 MB
docker.io/ubuntu latest 735f80812f90 2 weeks ago 83.5 MB
docker.io/hello-world latest 2cb0d9787c4d 5 weeks ago 1.85 kB
3.搜索镜像 docker search --automated -s 3 nginx
- --automated=true|false:仅显示自动创建的对象,默认为否;
- --no-trunc=true|false:输出信息不截断显示,默认为否;
- -s,--stars=x:指定仅显示评价为指定星级以上的镜像,默认为0,即全部;
[root@VM_0_15_centos ~]# docker search --automated -s 3 nginx
Flag --automated has been deprecated, use --filter=automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1384 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 610 [OK]
docker.io docker.io/jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as... 394 [OK]
docker.io docker.io/webdevops/php-nginx Nginx with PHP-FPM 111 [OK]
docker.io docker.io/zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 62 [OK]
4.删除镜像 docker rmi hello-world:latest
报错:(原因:容器正在运行,无法删除)
Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container 403c92061c98 is using its referenced image 2cb0d9787c4d
解决办法一:
docker rmi -f hello-world:latest
强制删除(不推介)
解决办法二:先删除容器,在删除镜像, 先执行docker rm 403c92061c98
,在执行docker rmi 2cb0d9787c4d
5.创建镜像 (3种方式)
-
基于已有镜像文件创建
首先,启动一个镜像,并在其中进行修改操作,例如创建一个test文件,之后退出:
$ docker run -it ubuntu:16.04 /bin/bash
root@7aa3602ab41e:/# touch test
root@7aa3602ab41e:/# exit
然后执行docker commit -m "Added a new file" -a "Docker Newbee" 7aa3602ab41e test:0.1
名令创建镜像(7aa3602ab41e
为刚启动的容器的ID)
-
基于本地模板导入
在OpenVZ下载模板,下载地址:https://openvz.org/Download/template/precreated
[root@localhost ~]# wget http://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz
[root@localhost ~]# cat centos-6-x86-minimal.tar.gz|docker import - centos-6-x86 //import - (+自定义的名字)
[root@localhost ~]# docker images //查看刚刚导入的镜像是否存在
-
基于Dockerfile创建(在后期文章介绍)
6.存出和载入镜像
- 存出镜像
docker save -o ubuntu_16.04.tar ubuntu:16.04
- 载入镜像
docker load --input ubuntu_16.04.tar
或者docker load < ubuntu_16.04.tar
7.上传镜像
(Docker Hub 注册地址:https://hub.docker.com/)(请科学上网 )
命令格式
docker push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
用户在Docker Hub网站注册后可以上传自制的镜像。例如用户user(Docker 账号用户名)上传本地的test:latest镜像,可以先添加新的标签user/test:latest,然后用docker push命令上传镜像:
$ docker tag test:latest user/test:latest
$ docker push user/test:latest
The push refers to a repository [docker.io/user/test]
Sending image list
Please login prior to push:
Username:
Password:
Email:
第一次上传会提示登录信息或进行注册。