前言
Docker的三大核心概念:鏡像、容器、倉庫。初學者對鏡像和容器往往分不清楚,學過面向對象的應該知道類和實例,這跟面向對象里面的概念很相似
我們可以把鏡像看作類,把容器看作類實例化后的對象。
| docker | 面向對象 |
|---|---|
| 鏡像 | 類 |
| 容器 | 實例 |
查看鏡像列表
使用docker images查看本地已經下載的鏡像
- REPOSITORY:表示鏡像的倉庫源
- TAG:鏡像的標簽,區分不同版本
- IMAGE ID:鏡像ID,16進制組成,唯一標識
- CREATED:鏡像創建時間
- SIZE:鏡像大小
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
我們本地下載的鏡像文件是從倉庫下載過來的,每個鏡像在倉庫源都有個名稱,也就是 REPOSITORY,同一個鏡像源可以有不同的版本,同標簽(TAG)區分
下載鏡像
直接使用 docker pull centos 默認是下載的最新的latest版本
docker pull centos
[root@yoyo ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Already exists
Digest: sha256:b5e66c4651870a1ad435cd75922fe2cb943c9e973a9673822d1414824a1d0475
Status: Downloaded newer image for centos:latest
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
centos latest 9f38484d220f 3 months ago 202MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
搜索鏡像
docker search搜索相關的鏡像文件
[root@yoyo ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5424 [OK]
ansible/centos7-ansible Ansible on Centos7 121 [OK]
jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86… 110 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 91 [OK]
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 57 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 54
tutum/centos Simple CentOS docker image with SSH access 44
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 37
kinogmt/centos-ssh CentOS with SSH 27 [OK]
如果我想下載一個centos7.5的鏡像版本,該如何找到呢?
查找TAG版本
如果要找到指定的TAG版本,需打開docker官網https://hub.docker.com/search/?type=image,搜索框輸入:centos搜索。
點擊詳情,找到TAGS,就可以看到不同的標簽版本了

接下來指定TAG名稱下載,后面加個冒號:標簽名稱
docker pull centos:centos7.5.1804
[root@yoyo ~]# docker pull centos:centos7.5.1804
centos7.5.1804: Pulling from library/centos
5ad559c5ae16: Pull complete
Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720a
Status: Downloaded newer image for centos:centos7.5.1804
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
centos centos7.5.1804 cf49811e3cdb 3 months ago 200MB
centos latest 9f38484d220f 3 months ago 202MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
更新鏡像
上面下載的TAG名稱是centos7.5.1804,這個太長了不太好記,可以改成一個自己喜歡的TAG名稱,比如7.5
更新鏡像需先啟動容器
docker run -d centos:centos7.5.1804
[root@yoyo ~]# docker run -d centos:centos7.5.1804
64cc20e825e3cb70bdbb5c22dac72b061fba77895e794ae7a06d57d2ddfb8a96
[root@yoyo ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64cc20e825e3 centos:centos7.5.1804 "/bin/bash" 38 seconds ago Exited (0) 35 seconds ago recursing_ardinghelli
fefdcbb9c662 centos/python-36-centos7 "container-entrypoin…" 24 hours ago Exited (0) 24 hours ago quirky_cray
9df329b5effd centos/python-36-centos7 "container-entrypoin…" 24 hours ago Exited (0) 24 hours ago nifty_roentgen
啟動之后,查看到容器id號64cc20e825e3,根據容器id,去修改
- -m:提交的描述信息
- -a:指定鏡像作者
- e218edb10161:容器ID
- runoob/ubuntu:v2:指定要創建的目標鏡像名
docker commit -m="update tag name" -a="yoyo" 64cc20e825e3 centos:7.5
[root@yoyo ~]# docker commit -m="update tag name" -a="yoyo" 64cc20e825e3 centos:7.5
sha256:254d4dfe9df7765ccf511bd8e7ff1f5de96b0b5a0af2542ee4cd30c8ac0575b3
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.5 254d4dfe9df7 8 seconds ago 200MB
centos latest b9af5ce31055 35 seconds ago 200MB
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
設置鏡像TAG
如果只是修改鏡像TAG名稱,可以用docker tag給鏡像取個新的tag名稱, 這里的id是鏡像的id
docker tag 254d4dfe9df7 centos:v7.5
[root@yoyo ~]# docker tag 254d4dfe9df7 centos:v7.5
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7.5 254d4dfe9df7 9 minutes ago 200MB
centos v7.5 254d4dfe9df7 9 minutes ago 200MB
centos latest b9af5ce31055 9 minutes ago 200MB
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
這時候會多了一個v7.5的標簽
刪除鏡像
上面多了個7.5的TAG,並且IMAGE ID是重復的,可以使用docker rmi 刪掉它,可以加-f參數強制刪除
- -f :強制刪除;
- --no-prune :不移除該鏡像的過程鏡像,默認移除;
[root@yoyo ~]# docker rmi centos:7.5
Untagged: centos:7.5
[root@yoyo ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v7.5 254d4dfe9df7 12 minutes ago 200MB
centos latest b9af5ce31055 12 minutes ago 200MB
centos/7.5 latest 62a395cab78e 13 minutes ago 200MB
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
centos centos7.5.1804 cf49811e3cdb 3 months ago 200MB
centos <none> 9f38484d220f 3 months ago 202MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@yoyo ~]#
想在別人的基礎上重新做一個鏡像,需要創建一個 Dockerfile 文件,后面講 Docker 如何構建我們的鏡像。
python接口QQ群:717225969
