1. 程式人生 > 實用技巧 >【docker容器入門(三)】: Docker 映象使用

【docker容器入門(三)】: Docker 映象使用

1. 檢視映象列表

使用docker images檢視映象列表

[root@guoxiaobo ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    7baf28ea91eb   4 days ago      133MB
ubuntu        latest    f643c72bc252   2 weeks ago     72.9MB
hello-world   latest    bf756fb1ae65   11 months ago   13.3kB

2. 獲取映象

拉取centos映象,預設拉取最新版本映象

[root@guoxiaobo ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

3. 查詢映象

查詢httpd映象

[root@guoxiaobo ~]# docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  3279      [OK]       
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36                   
centos/httpd                                                                            33                   [OK]
arm32v7/httpd                           The Apache HTTP Server Project                  9                    
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                    [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   1                    [OK]
inanimate/httpd-ssl                     A play container with httpd, ssl enabled, an…   1                    [OK]
hypoport/httpd-cgi                      httpd-cgi                                       1                    [OK]
...               

4.拖取映象

拖取httpd映象

[root@guoxiaobo ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists 
17e233bac21e: Pull complete 
130aad5bf43a: Pull complete 
81d0a34533d4: Pull complete 
da240d12a8a4: Pull complete 
Digest: sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

5. 刪除映象

刪除hello-world映象

[root@guoxiaobo ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

6. 建立映象

第一種方法是使用docker commit命令把正在執行的容器提交為映象

[root@guoxiaobo ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
1ddbf60e048a   ubuntu    "/bin/bash"   7 seconds ago   Up 5 seconds             ubuntu-test
[root@guoxiaobo ~]# docker commit 1ddbf60e048a guoxb/ubuntu-guoxb
sha256:b9e51cb41e959d84f587c60b9d390ae9884d21e4b9b72f4cc3bae0f81ec08b26
[root@guoxiaobo ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
guoxb/ubuntu-guoxb   latest    b9e51cb41e95   24 seconds ago   72.9MB
httpd                latest    dd85cdbb9987   4 days ago       138MB
nginx                latest    7baf28ea91eb   4 days ago       133MB
centos               latest    300e315adb2f   7 days ago       209MB
ubuntu               latest    f643c72bc252   2 weeks ago      72.9MB

第二種方法是使用Dockerfile指令來建立一個新的映象

此處不詳細講,後面會專門來講如何使用Dockerfile建立映象

7.設定映象標籤

給映象id為b9e51cb41e95的映象打一個標籤guoxb/ubuntu-guoxb:2.0

[root@guoxiaobo ~]# docker tag b9e51cb41e95 guoxb/ubuntu-guoxb:2.0
[root@guoxiaobo ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
guoxb/ubuntu-guoxb   2.0       b9e51cb41e95   2 minutes ago   72.9MB
guoxb/ubuntu-guoxb   latest    b9e51cb41e95   2 minutes ago   72.9MB
httpd                latest    dd85cdbb9987   4 days ago      138MB
nginx                latest    7baf28ea91eb   4 days ago      133MB
centos               latest    300e315adb2f   7 days ago      209MB
ubuntu               latest    f643c72bc252   2 weeks ago     72.9MB