天天看點

Docker實戰-關于Docker鏡像的相關操作(二)

Docker實戰-關于Docker鏡像的相關操作(二)

之前的分享中,我們介紹了關于Docker鏡像的查詢操作相關的内容,下面我們繼續來介紹删除清理、導入導出、建立鏡像等操作。

如何删除和清理鏡像?

使用标簽删除鏡像

可以使用docker rmi 或者是 docker image rm 指令來删除鏡像,它也包含了幾個基本選項

  • -f ,-force:強制删除鏡像,即使它存在依賴性也進行删除
  • -no-prune:不要清理未帶标簽的父鏡像
docker rmi  IMAGE [IMAGE……]           
[root@localhost ~]# docker rmi myubuntu:new
Untagged: myubuntu:new
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       18.04     f9a80a55f492   8 weeks ago   63.2MB
[root@localhost ~]#            

這裡我們删除了一個myubuntu:new标簽的鏡像,并且這個删除操作對其他标簽的相同鏡像不會産生影響。但是如果在鏡像中隻存在了一個标簽的時候,如果再使用删除指令,則會徹底删除鏡像。

使用鏡像ID來删除鏡像

還是使用docker rmi指令 并且後面更上ID來删除鏡像。需要注意的是,如果當鏡像建立的容器存在的時候,鏡像檔案是無法被删除的。當然如果使用-f 參數進行強制删除的話也可以。但通常情況下不推薦使用-f參數來強制删除一個已經存在容器依賴的鏡像。正确的做法應該是先删除依賴該鏡像的所有容器,然後再去删除鏡像。

[root@localhost ~]# docker rmi f9a80a55f492
Error response from daemon: conflict: unable to delete f9a80a55f492 (must be forced) - image is being used by stopped container a9a05f9ef099
[root@localhost ~]# 
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND   CREATED        STATUS                    PORTS     NAMES
a9a05f9ef099   ubuntu:18.04   "bash"    18 hours ago   Exited (0) 18 hours ago             hungry_cannon
[root@localhost ~]# docker rm a9a05f9ef099
a9a05f9ef099
[root@localhost ~]# docker rmi f9a80a55f492
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:152dc042452c496007f07ca9127571cb9c29697f42acbfad72324b2bb2e43c98
Deleted: sha256:f9a80a55f492e823bf5d51f1bd5f87ea3eed1cb31788686aa99a2fb61a27af6a
Deleted: sha256:548a79621a426b4eb077c926eabac5a8620c454fb230640253e1b44dc7dd7562
[root@localhost ~]#            

清理鏡像

在我們使用Docker一段時間之後,系統中可能會存在一些臨時性的鏡像檔案,以及有些從來都沒有使用過的鏡像,這個時候,我們就可以使用docker image prune 指令來進行清理。

  • -a , -all:删除鄋的無用鏡像
  • -filter filter:隻清理符合過濾器的鏡像
  • -f ,-force:強制删除鏡像
[root@localhost ~]# docker image prune -f
Total reclaimed space: 0B
[root@localhost ~]#            

如何建立鏡像

建立鏡像的方式有三種

  • 基于已有鏡像的容器進行建立
  • 基于本地模闆導入的方式
  • 基于Dockerfile檔案建立

基于已有容器建立

docker [container] commit 指令

docker [container] commit [OPTIONS] CONTAINER [REPOSITORY] [:TAG]            

首先我們來建立一個鏡像容器,并且在容器中建立一個辨別檔案。容器ID為5cb48f5d7103。

[root@localhost ~]# docker run -it ubuntu:18.04 /bin/bash
root@5cb48f5d7103:/# touch test
root@5cb48f5d7103:/# ls  
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var
root@5cb48f5d7103:/# exit
exit
[root@localhost ~]#            

經過這一系列的操作,我們知道容器中鏡像已經發生了變化,是以我們可以使用上面的指令來建立一個新的鏡像。

[root@localhost ~]# docker  commit -m "add a new test file in root" -a "Docker New" 5cb48f5d7103 test:0.1
sha256:05268b440fd9d2cf31f5ee380969e0e196c670c6513cb0cedd2fc1419f9db7a7
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test         0.1       05268b440fd9   16 seconds ago   63.2MB
ubuntu       18.04     f9a80a55f492   8 weeks ago      63.2MB
[root@localhost ~]#            

建立完成之後運作該鏡像檢視是否有test檔案

[root@localhost ~]# docker run -it test:0.1 /bin/bash
root@cf40152b3907:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var
root@cf40152b3907:/# exit
exit
[root@localhost ~]#            

基于本地模闆導入

可以直接操作一個系統模闆檔案來導入鏡像,使用 docker [container] import 指令來是完成。

docker [image] import [OPTIONS] file|URL -[REPOSITORY] [:TAG]           

要想直接導入一個模闆,我們可以使用OpenVZ來提供模闆,或者使用導出的鏡像來作為模闆。

基于Dockerfile檔案建立

基于Dockerfile檔案是我們最為常見的一種建立鏡像的方式。下面我們就來示範具體的操作過程。

首先需要建立一個Dockerfile的檔案。

FROM centos
RUN yum -y install wget \
    && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
    && tar -xvf redis.tar.gz           

然後執行如下的操作

[root@localhost ~]# docker build -t nginx:0.1 .
Sending build context to Docker daemon    274MB
Step 1/2 : FROM centos
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
 ---> 5d0da3dc9764
Step 2/2 : RUN yum -y install wget     && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"     && tar -xvf redis.tar.gz
 ---> Running in 3198306bde68
CentOS Linux 8 - AppStream                       54  B/s |  38  B     00:00    
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
The command '/bin/sh -c yum -y install wget     && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"     && tar -xvf redis.tar.gz' returned a non-zero code: 1
[root@localhost ~]#            

完成之後會看到在鏡像中出現了該鏡像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
test         0.1       05268b440fd9   17 minutes ago   63.2MB
ubuntu       18.04     f9a80a55f492   8 weeks ago      63.2MB
centos       latest    5d0da3dc9764   22 months ago    231MB
[root@localhost ~]#            

總結

上面我們介紹了關于鏡像的删除和清理,并且介紹了如何去建立一個鏡像,在後續的分享中還會介紹到使用Dockerfile檔案建立鏡像的方式,希望大家多多關注。