1.2 Docker 镜像管理
2018-05-09
1.2 Docker 镜像管理
从 docker.com 获取镜像
root@ubuntu:~# docker pull ubuntu //
latest: Pulling from ubuntu
20ee58809289: Pull complete
a9bf41614bd7: Pull complete
3bb5bd4ed64a: Pull complete
958fcc55b7ad: Pull complete
ac8b2a2b477b: Pull complete
Digest: sha256:6194c351c69cd4d7248d3c45b59b2d941c586607e5c53b3c996709744db0fe2b
Status: Downloaded newer image for ubuntu:latest
查看本地都有哪些镜像
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ac8b2a2b477b 35 hours ago 125.2 MB
为 Ubuntu 镜像设置标签为 test1
再使用使用 docker images 查看,该行的 image id 和 Ubuntu 的一样
root@ubuntu:~# docker tag ubuntu test1
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ac8b2a2b477b 35 hours ago 125.2 MB
test1 latest ac8b2a2b477b 35 hours ago 125.2 MB
root@ubuntu:~# docker tag ubuntu:v1 test1:v1.0
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu v1 ac8b2a2b477b 36 hours ago 125.2 MB
test1 latest ac8b2a2b477b 36 hours ago 125.2 MB
test1 v1.0 ac8b2a2b477b 36 hours ago 125.2 MB
从 docker 仓库搜索 docker 镜像 : docker search [image-name]
root@ubuntu:~# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 4293 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 65 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 29 [OK]
torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 26 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 25 [OK]
nickistre/ubuntu-lamp LAMP server on Ubuntu 8 [OK]
未全部显示,其中有一项为 STARS,代表这个镜像的热度
用下载的镜像开启容器
-i 表示让容器的标准输入打开,-t 表示分配一个伪终端,要把 -i -t 放在镜像名字的前面
root@ubuntu:~# docker run -it ubuntu /bin/bash
root@81678fc4d6c7:/# //这个界面就是 -i 选项的结果
root@81678fc4d6c7:/# ls
bin boot core dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@81678fc4d6c7:/# w
05:48:55 up 2:16, 0 users, load average: 0.02, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root@81678fc4d6c7:/# exit
exit
root@ubuntu:~#
用这种方式打开容器,exit 退出后,容器直接关掉了。
查看运行的容器
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81678fc4d6c7 ubuntu:latest "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago happy_newton
加-a 选项可查看已经退出的容器
删除指定镜像
其中后面的参数可以是 tag,如果是 tag 时,实际上是删除该 tag,只要该镜像还有其他 tag,就不会删除该镜像。当后面的参数为镜像 ID 时,就会彻底删除整个镜像,连同所以标签一起删除。
root@ubuntu:~# docker tag ubuntu ubuntu:v1
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ac8b2a2b477b 36 hours ago 125.2 MB
ubuntu v1 ac8b2a2b477b 36 hours ago 125.2 MB
test1 latest ac8b2a2b477b 36 hours ago 125.2 MB
root@ubuntu:~# docker rmi ubuntu:latest
Untagged: ubuntu:latest
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu v1 ac8b2a2b477b 36 hours ago 125.2 MB
test1 latest ac8b2a2b477b 36 hours ago 125.2 MB
查看容器中 Ubuntu 版本
root@81678fc4d6c7:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
内核:内核是根据宿主机得出的
root@81678fc4d6c7:/# uname -a
Linux 81678fc4d6c7 3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux