docker-镜像的基本操作(三)

docker-镜像的基本操作(三)

列出镜像列表

语法

docker images [OPTSIONS][REPOSITROY] 
-a,all=flase
-f,--filter=[]
--no-trunc=false
-q,--quite=flase

来列出本地主机上的镜像。

runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB 
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB 
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB 
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB 
hello-world latest 690ed74de00f 6 months ago 960 B 
training/webapp latest 6fae60ef3446 11 months ago 348.8 MB

各个选项说明:

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。


所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash 

如果要使用版本为14.04的ubuntu系统镜像来运行容器时,命令如下:

runoob@runoob:~$ docker run -t -i ubuntu:14.04 /bin/bash 

如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。

实例:

①查看镜像,显示不完整 IMAGE ID
[root@izwz93cxtzylaaouqzjj4sz docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              2a4cca5ac898        37 hours ago        111.5 MB
docker.io/centos    latest              ff426288ea90        8 days ago          207.2 MB
②查看镜像,显示完整 IMAGE ID
[root@izwz93cxtzylaaouqzjj4sz ~]# docker images --no-trunc
REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE
docker.io/ubuntu    latest              sha256:2a4cca5ac898476c2c47a8d6a17102e00241d6fa377fbe4d50787fe3d7a8d4d6   37 hours ago        111.5 MB
docker.io/centos    latest              sha256:ff426288ea903fcf8d91aca97460c613348f7a27195606b45f19ae91776ca23d   8 days ago          207.2 MB
③查看镜像详细信息:
docker inspect [OPTIONS]CONTAINER|IMAGE[CONTAINER|IMGAE..]
[root@izwz93cxtzylaaouqzjj4sz ~]# docker inspect centos:latest

删除镜像

语法:

doker rmi [OPTIONS]IMAGE[IMAGE...]
    -f,--force=false  Force removal of the image
    --no-prune=flase do not delete untagged parents

实例:

[root@izwz93cxtzylaaouqzjj4sz ~]# docker rmi -f  ubuntu:latest
Untagged: ubuntu:latest
Untagged: docker.io/ubuntu@sha256:55c3bcbe279b387e344c3e8dddec74115dbb3b4f4c91f97f4d1827e2ab9aba17
Deleted: sha256:2a4cca5ac898476c2c47a8d6a17102e00241d6fa377fbe4d50787fe3d7a8d4d6

删除所有镜像:

[root@izwz93cxtzylaaouqzjj4sz ~]# docker rmi $(docker images ubuntu -q)
"docker rmi" requires at least 1 argument(s).
See 'docker rmi --help'.

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

获取和推送镜像

①查找镜像

语法

docker search [OPTIONS] TERM
--automated=false  Only show automated builds
--no-trunc=false    Don't rtruncate output
-s,--starts=0 Onlry displays with at least x stars
最多返回25个结果

实例

查找全部镜像
[root@izwz93cxtzylaaouqzjj4sz ~]# docker search ubuntu
INDEX       NAME                                                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/ubuntu                                                    Ubuntu is a Debian-based Linux operating s...   7115      [OK]       
docker.io   docker.io/dorowu/ubuntu-desktop-lxde-vnc                            Ubuntu with openssh-server and NoVNC            157                  [OK]
docker.io   docker.io/rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of of...   127                  [OK]
docker.io   docker.io/ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   90                   [OK]
docker.io   docker.io/ubuntu-upstart                                            Upstart is an event-based replacement for ...   80        [OK]       
docker.io   docker.io/neurodebian                                               NeuroDebian provides neuroscience research...   41        [OK]       
.......................... 
查找三星以上的镜像
[root@izwz93cxtzylaaouqzjj4sz ~]# docker search -s 3 ubuntu
Flag --stars has been deprecated, use --filter=stars=3 instead
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/ubuntu                                                 Ubuntu is a Debian-based Linux operating s...   7115      [OK]       
docker.io   docker.io/dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            157                  [OK]
docker.io   docker.io/rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of of...   127                  [OK]

②拉取镜像

语法

docker pull [iptions]  name[:TAG]
-a, --all-tags=false Download all tagged images in the repository

实例

拉取ubuntu:14.04
[root@izwz93cxtzylaaouqzjj4sz ~]# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              ff426288ea90        8 days ago          207.2 MB
[root@izwz93cxtzylaaouqzjj4sz ~]# docker pull ubuntu:14.04
Trying to pull repository docker.io/library/ubuntu ... 
14.04: Pulling from docker.io/library/ubuntu
556ab54f6580: Pull complete 
6ca8fd6ef32a: Pull complete 
e64a7e7a2b21: Pull complete 
09ed7c05bfd3: Pull complete 
ec1faacd6fd9: Pull complete 
Digest: sha256:f0cc0848fdadb4ae7341028a21f894df7cc2ab56e2bfe162850cbd602234d9a4
[root@izwz93cxtzylaaouqzjj4sz ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    14.04               02a63d8b2bfa        40 hours ago        221.7 MB
docker.io/centos    latest              ff426288ea90        8 days ago          207.2 MB
在上述过程中速度非常慢,有什么解决方法呢?   使用--registry-mirror选项
1.修改/etc/default/docker
2.添加 DOCKER_OPTS= "--registry-mirror=http://MIRROR-ADDR"    MIRROR-ADDR可选:www.daoclod.io或者阿里云镜像镜像加速
3.重启docker的守护进程 service docker restart

③推送镜像

语法

 docker push NAME[:TAG]

实例:

docker-镜像的基本操作(三)_第1张图片
docker不会整个都提交,只会提交修改的部分

④构建镜像

1.保存对容器的修改,并再次使用
2.自定义镜像的能力
3.以软件的形式打包并分发服务及其运行环境

语法:

方式一:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] //通过容器构建
参数:
    -a,-author="" Author
    -m,--message="" commit message
    -p,pause=ture Pause container during commit
方式二:
docker build [OPTIONS] PATH |URL |- //通过Dockerfile文件构建
参数:
    --force-rm=false
    --no-cache=false
    --pull=false
    -q,--quiet=false
    --rm=true
    -t,--tag=""

实例:

[root@izwz93cxtzylaaouqzjj4sz ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
295e5cfea561        ubuntu              "/bin/bash"         2 minutes ago       Exited (127) About a minute ago                       commit_test
[root@izwz93cxtzylaaouqzjj4sz ~]# docker commit -a 'jackzhu' -m 'hellowrold' commit_test 435999799/commit_test1
sha256:0f6cb6231b80269cffeaf71ce05236399f1d8f91f293bb699a9516935dc74392
[root@izwz93cxtzylaaouqzjj4sz ~]# docker images;
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
435999799/commit_test1   latest              0f6cb6231b80        27 seconds ago      111.5 MB
docker.io/ubuntu         latest              2a4cca5ac898        43 hours ago        111.5 MB
docker.io/ubuntu         14.04               02a63d8b2bfa        43 hours ago        221.7 MB
docker.io/centos         latest              ff426288ea90        8 days ago          207.2 MB
docker.io/ubuntu         12.10               3e314f95dcac        3 years ago         172 MB

使用Dockerfile构建镜像

创建第一个Dockerfile

runoob@runoob:~$ cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "[email protected]"
RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D
每一个指令都会在镜像上创建一个新的层
每一个指令的前缀都必须是大写的
FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。

runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "[email protected]"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob

你可能感兴趣的:(docker,docker)