三、【Docker筆記】Docker鏡像
- 2020 年 4 月 5 日
- 筆記
鏡像是Docker的三大核心概念之一。Docker在運行容器之前,本地需要存有鏡像,若不存在則Docker會首先嘗試從默認的鏡像倉庫中去下載,當然我們也可以去配置自己的倉庫,如此就會從我們配置的倉庫中去下載。
一、獲取鏡像
鏡像是Docker運行容器的前提條件,我們可以通過 docker pull 命令來下載鏡像,語法格式為:docker pull name[:tag]。若我們不指定具體的tag版本,則默認下載最新的版本即 latest標籤的鏡像。
# 1. 下載一個最新版本的 ubuntu 鏡像 $ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 5bed26d33875: Pull complete f11b29a9c730: Pull complete 930bda195c84: Pull complete 78bf9a5ad49e: Pull complete Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest # 2. 下載一個指定的 ubuntu 鏡像版本 $ sudo docker pull ubuntu:14.04 14.04: Pulling from library/ubuntu 2e6e20c8e2e6: Pull complete 30bb187ac3fc: Pull complete b7a5bcc4a58a: Pull complete Digest: sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4 Status: Downloaded newer image for ubuntu:14.04 docker.io/library/ubuntu:14.04 # 3. 以上兩種都是從默認的Docker鏡像倉庫下載鏡像,當然我們也可以從指定倉庫下載 $ sudo docker pull dl.aliyun.com:1000/ubuntu # 4. 鏡像下載之後就可以運行了,使用鏡像來創建容器 $ sudo docker run -t -i ubuntu /bin/bash root@95e3c40a7b3b:/# root@95e3c40a7b3b:/# ll # 這裡是在容器中執行的命令 total 28 drwxr-xr-x. 21 root root 4096 Apr 5 10:30 ./ drwxr-xr-x. 21 root root 4096 Apr 5 10:30 ../ -rwxr-xr-x. 1 root root 0 Apr 5 10:30 .dockerenv* drwxr-xr-x. 2 root root 4096 Mar 11 21:05 bin/ drwxr-xr-x. 2 root root 6 Apr 24 2018 boot/ drwxr-xr-x. 5 root root 360 Apr 5 10:30 dev/ drwxr-xr-x. 29 root root 4096 Apr 5 10:30 etc/ drwxr-xr-x. 2 root root 6 Apr 24 2018 home/ drwxr-xr-x. 8 root root 96 May 23 2017 lib/ drwxr-xr-x. 2 root root 34 Mar 11 21:03 lib64/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 media/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 mnt/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 opt/ dr-xr-xr-x. 388 root root 0 Apr 5 10:30 proc/ drwx------. 2 root root 37 Mar 11 21:05 root/ drwxr-xr-x. 5 root root 58 Mar 20 19:20 run/ drwxr-xr-x. 2 root root 4096 Mar 20 19:20 sbin/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 srv/ dr-xr-xr-x. 13 root root 0 Apr 11 2018 sys/ drwxrwxrwt. 2 root root 6 Mar 11 21:05 tmp/ drwxr-xr-x. 10 root root 4096 Mar 11 21:03 usr/ drwxr-xr-x. 11 root root 4096 Mar 11 21:05 var/
二、查看鏡像資訊
我們可以使用 docker images 命令來列出本地主機上已有的鏡像。
# 1. 列出本機上都有哪些鏡像 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB ubuntu 14.04 6e4f1fe62ff1 3 months ago 196MB # 說明:從上面我們可以看到,在第一節我們下載的鏡像都在本機中 # REPOSITORY 來自於哪個倉庫 # TAG 鏡像的標籤資訊 # IMAGE ID 鏡像的ID號,這是唯一的 # CREATED 鏡像的創建時間 # SIZE 鏡像大小
除了從倉庫下載的鏡像文件,我們也可以對已有的本地鏡像進行重新打標籤,來適應自己項目命名。
# 將 ubuntu:latest 鏡像打上自己的 ubuntu 鏡像 $ sudo docker tag 192.168.0.128:5000/ubuntu:latest ubuntu:latest $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 4e5021d210f6 2 weeks ago 64.2MB 192.168.0.128:5000/ubuntu latest 6e4f1fe62ff1 3 months ago 196MB ubuntu latest 6e4f1fe62ff1 3 months ago 196MB
我們查看以上的三個本地鏡像,其中有兩個鏡像的ID是相同的,這說明這兩個鏡像指向的是同一個鏡像文件,只是它們有著不同的別名。
# 查看某一個鏡像的詳細資訊 $ sudo docker inspect 6e4f1fe62ff1(鏡像ID) [ { "Id": "sha256:6e4f1fe62ff14a4c8119781d47a3739fa97c190e1df38e868794ad7a7cf50a48", "RepoTags": [ "ubuntu:14.04", "ubuntu:latest" ], "RepoDigests": [ "ubuntu@sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4" ], "Parent": "", "Comment": "", "Created": "2019-12-19T04:23:45.705096855Z", "Container": "accd5cd52ef3ece3d4115e456824c3a37749ec10de75c3ad187b74c518b7890a", "ContainerConfig": { "Hostname": "accd5cd52ef3", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD ["/bin/bash"]" ], "ArgsEscaped": true, "Image": "sha256:f23179da08e323ef4bef58a9425152eb39ed802c1b6f8f597113c9fe950e0b24", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "18.06.1-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "sha256:f23179da08e323ef4bef58a9425152eb39ed802c1b6f8f597113c9fe950e0b24", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 196466523, "VirtualSize": 196466523, "GraphDriver": { "Data": { "DeviceId": "8", "DeviceName": "docker-253:0-67518837-e6323ccccc77b0b03785b672a7de35dd14baa660562dd07f7e9889041c905868", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7", "sha256:48dc77435ad5c63ea60d91e6ad4828c70e7e61755f99982b0505abb8aaa00872", "sha256:3da511183950aa462f667f43fcda0bb5484c5c73eaa94fcd0a94bbd4db396e1c" ] }, "Metadata": { "LastTagTime": "2020-04-05T18:42:30.861232446+08:00" } } ] # 查看具體某個參數 $ sudo docker inspect -f {{".Architecture"}} 6e4(鏡像ID,可以只使用前面幾個字母會自動定位) amd64
三、搜索鏡像
若我們不確定倉庫是否有我們需要版本的鏡像,我們也可以使用 docker search 命令先搜尋一下。語法為 docker search TERM,其中包含以下參數:
–automated=false 僅顯示自動創建的鏡像
–no-trunc=false 輸出資訊不截斷顯示
-s,–stars=0 指定僅顯示評價為指定星級以上的鏡像
# 以搜尋 MySQL 鏡像為例 $ sudo docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9335 [OK] mariadb MariaDB is a community-developed fork of MyS… 3347 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 683 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 74 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 66 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 37 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] prom/mysqld-exporter 27 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 19 mysql/mysql-router MySQL Router provides transparent routing be… 15 databack/mysql-backup Back up mysql databases to... anywhere! 14 arey/mysql-client Run a MySQL client from a docker container 13 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 genschsa/mysql-employees MySQL Employee Sample Database 5 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 5 [OK] devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 2 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK] monasca/mysql-init A minimal decoupled init container for mysql 0 # 僅顯示自動創建的 mysql 鏡像 $ sudo docker search --automated=false mysql Flag --automated has been deprecated, use --filter=is-automated=true instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9335 [OK] mariadb MariaDB is a community-developed fork of MyS… 3347 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 683 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 74 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 66 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 37 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] prom/mysqld-exporter 27 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 19 mysql/mysql-router MySQL Router provides transparent routing be… 15 databack/mysql-backup Back up mysql databases to... anywhere! 14 arey/mysql-client Run a MySQL client from a docker container 13 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 genschsa/mysql-employees MySQL Employee Sample Database 5 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 5 [OK] devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 2 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK] monasca/mysql-init A minimal decoupled init container for mysql 0 # 不截斷顯示鏡像 $ sudo docker search --no-trunc=false mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9335 [OK] mariadb MariaDB is a community-developed fork of MyS… 3347 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 683 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 74 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 66 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 37 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] prom/mysqld-exporter 27 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 19 mysql/mysql-router MySQL Router provides transparent routing be… 15 databack/mysql-backup Back up mysql databases to... anywhere! 14 arey/mysql-client Run a MySQL client from a docker container 13 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 genschsa/mysql-employees MySQL Employee Sample Database 5 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 5 [OK] devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 2 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK] monasca/mysql-init A minimal decoupled init container for mysql 0 # 顯示 stars 大於10 的鏡像 $ sudo docker search -s=10 mysql Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9335 [OK] mariadb MariaDB is a community-developed fork of MyS… 3347 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 683 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 74 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 66 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 37 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] prom/mysqld-exporter 27 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 19 mysql/mysql-router MySQL Router provides transparent routing be… 15 databack/mysql-backup Back up mysql databases to... anywhere! 14 arey/mysql-client Run a MySQL client from a docker container 13 [OK]
四、刪除鏡像
對於不需要的鏡像我們可以將其刪除,刪除鏡像語法為:docker rmi IMAGE[IMAGE…]
# 刪除一個鏡像 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu <none> 4e5021d210f6 2 weeks ago 64.2MB ubuntu 14.04 6e4f1fe62ff1 3 months ago 196MB ubuntu latest 6e4f1fe62ff1 3 months ago 196MB $ sudo docker rmi ubuntu:14.04(6e4f1fe62ff1) # 可為標籤或ID刪除,需要注意若指定鏡像ID刪除,則所有的指向該鏡像的標籤也都刪除,此時需要強制刪除才可否則刪除失敗 Error response from daemon: conflict: unable to delete 6e4f1fe62ff1 (must be forced) - image is referenced in multiple repositories $ sudo docker rmi -f 6e4f1fe62ff1 Untagged: ubuntu:14.04 Untagged: ubuntu:latest Untagged: ubuntu@sha256:ffc76f71dd8be8c9e222d420dc96901a07b61616689a44c7b3ef6a10b7213de4 Deleted: sha256:6e4f1fe62ff14a4c8119781d47a3739fa97c190e1df38e868794ad7a7cf50a48 Deleted: sha256:bf1b2d677f21703e5be92de8b094fbf5cceeea666885053b40b8a5b2f20638b3 Deleted: sha256:9b22c82c6d5a884491639891b0eb053e6e58d6da35ad2d73655d7dd2707d6d73 Deleted: sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu <none> 4e5021d210f6 2 weeks ago 64.2MB
在刪除鏡像之前,我們建議先查看該鏡像是否有一個容器在引用,還有盡量不要使用 -f 強制刪除。
# 查看本地有哪些容器 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95e3c40a7b3b 4e5021d210f6 "/bin/bash" 46 minutes ago Exited (127) 43 minutes ago determined_rubin # 說明:通過查看Docker容器,我們可知鏡像4e5021d210f6被使用,在刪除該鏡像時就需要注意了。不要強制去刪除
若我們發現該鏡像以後也不在使用了,正確的做法也不是強制刪除,而是先解除容器和鏡像之間的依賴,再刪除鏡像文件。
# 0. 查看本機有哪些容器 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95e3c40a7b3b 4e5021d210f6 "/bin/bash" 51 minutes ago Exited (127) 48 minutes ago determined_rubin # 1. 刪除不需要的的容器 $ sudo docker rm 95e3c40a7b3b 95e3c40a7b3b # 2. 再查看發現容器已刪除 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES # 3. 最後再刪除不需要的鏡像文件 $ sudo docker rmi 4e5021d210f6 # 4. 查看本地鏡像已被刪除 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE
五、創建鏡像
關於創建鏡像主要有三種方式:基於已有的鏡像容器創建、基於本地模板導入、基於Dockerfile創建。
1、基於已有鏡像的容器創建
使用 docker commit 命令,語法為:docker commit [options] container [repository[:tag]],其中 options 參數可為:
-a,–author=「」 作者資訊
-m,–message=「」 提交消息
-p,–pause=true 提交時暫停容器運行
下面我們來創建一個鏡像:
# 0. 查看當前鏡像 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB # 1. 啟動一個已有的鏡像 $ sudo docker run -ti ubuntu /bin/bash root@56a5d871e739:/# touch test.txt # 在容器中創建一個文件,用來與當前的鏡像做區分 root@56a5d871e739:/# exit # 退出當前容器 # 2. 提交,創建新的鏡像 $ sudo docker commit -m "This is a new file" -a "gongziqi" 56a5d871e739 test sha256:e5bc93985af09811230818a6905ce21b3428aebfeb6bae48f86772846b324be3 # 3. 再查看鏡像 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE test latest e5bc93985af0 26 seconds ago 64.2MB ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB # 4. 查看這兩個鏡像運行的容器內容 $ sudo docker run -ti ubuntu /bin/bash root@2ee2858e4b28:/# ll total 28 drwxr-xr-x. 21 root root 4096 Apr 5 13:07 ./ drwxr-xr-x. 21 root root 4096 Apr 5 13:07 ../ -rwxr-xr-x. 1 root root 0 Apr 5 13:07 .dockerenv* drwxr-xr-x. 2 root root 4096 Mar 11 21:05 bin/ drwxr-xr-x. 2 root root 6 Apr 24 2018 boot/ drwxr-xr-x. 5 root root 360 Apr 5 13:07 dev/ drwxr-xr-x. 29 root root 4096 Apr 5 13:07 etc/ drwxr-xr-x. 2 root root 6 Apr 24 2018 home/ drwxr-xr-x. 8 root root 96 May 23 2017 lib/ drwxr-xr-x. 2 root root 34 Mar 11 21:03 lib64/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 media/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 mnt/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 opt/ dr-xr-xr-x. 395 root root 0 Apr 5 13:07 proc/ drwx------. 2 root root 37 Mar 11 21:05 root/ drwxr-xr-x. 5 root root 58 Mar 20 19:20 run/ drwxr-xr-x. 2 root root 4096 Mar 20 19:20 sbin/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 srv/ dr-xr-xr-x. 13 root root 0 Apr 11 2018 sys/ drwxrwxrwt. 2 root root 6 Mar 11 21:05 tmp/ drwxr-xr-x. 10 root root 4096 Mar 11 21:03 usr/ drwxr-xr-x. 11 root root 4096 Mar 11 21:05 var/ root@2ee2858e4b28:/# exit exit $ sudo docker run -ti test /bin/bash root@0e4030bfbad3:/# ll total 28 drwxr-xr-x. 21 root root 4096 Apr 5 13:07 ./ drwxr-xr-x. 21 root root 4096 Apr 5 13:07 ../ -rwxr-xr-x. 1 root root 0 Apr 5 13:07 .dockerenv* drwxr-xr-x. 2 root root 4096 Mar 11 21:05 bin/ drwxr-xr-x. 2 root root 6 Apr 24 2018 boot/ drwxr-xr-x. 5 root root 360 Apr 5 13:07 dev/ drwxr-xr-x. 29 root root 4096 Apr 5 13:07 etc/ drwxr-xr-x. 2 root root 6 Apr 24 2018 home/ drwxr-xr-x. 8 root root 96 May 23 2017 lib/ drwxr-xr-x. 2 root root 34 Mar 11 21:03 lib64/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 media/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 mnt/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 opt/ dr-xr-xr-x. 395 root root 0 Apr 5 13:07 proc/ drwx------. 2 root root 58 Apr 5 13:05 root/ drwxr-xr-x. 5 root root 58 Mar 20 19:20 run/ drwxr-xr-x. 2 root root 4096 Mar 20 19:20 sbin/ drwxr-xr-x. 2 root root 6 Mar 11 21:03 srv/ dr-xr-xr-x. 13 root root 0 Apr 11 2018 sys/ -rw-r--r--. 1 root root 0 Apr 5 13:05 test.txt drwxrwxrwt. 2 root root 6 Mar 11 21:05 tmp/ drwxr-xr-x. 10 root root 4096 Mar 11 21:03 usr/ drwxr-xr-x. 11 root root 4096 Mar 11 21:05 var/ # 觀察:test鏡像創建的容器中是有 test.txt 文件的
2、基於本地模板導入
可以從作業系統模板文件導入一個鏡像。假設我們本地已經有了一個 ubuntu.tar.gz 文件,我們可通過以下命令來導入:
# 導入本地模板 $ sudo ll $ sudo docker images $ sudo cat ubuntu-16.04-x86_64.tar.gz | docker import - ubuntu:16.04 $ sudo docker images
3、基於Dockerfile創建
這種創建方式,我們後面專門用一節來介紹。
六、存出和載入鏡像
關於Docker鏡像的存出和載入,我們可以使用以下兩個命令:docker save / docker load。
# 1. 存出鏡像,將某個鏡像存出(注意觀察當前工作目錄前後的文件變化) # 1.1 查看鏡像,和當前工作目錄文件 $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e5021d210f6 2 weeks ago 64.2MB $ sudo ll 總用量 242148 drwxr-xr-x. 4 root root 4096 4月 2 19:09 apache drwxr-xr-x. 11 root root 4096 3月 29 15:24 apache-activemq-5.15.12 -rw-r--r--. 1 root root 62666908 3月 29 14:40 apache-activemq-5.15.12-bin.tar.gz drwxr-xr-x. 8 10 143 4096 12月 13 2016 jdk1.8.0_121 -rw-r--r--. 1 root root 183246769 3月 29 15:17 jdk-8u121-linux-x64.tar.gz -rw-r--r--. 1 root root 1208398 4月 5 21:16 ubuntu-16.04-x86_64.tar.gz # 1.2 存出鏡像,並查看當前工作目錄 $ sudo docker save -o ubuntu_latest.tar ubuntu:latest $ sudo ll 總用量 307152 drwxr-xr-x. 4 root root 4096 4月 2 19:09 apache drwxr-xr-x. 11 root root 4096 3月 29 15:24 apache-activemq-5.15.12 -rw-r--r--. 1 root root 62666908 3月 29 14:40 apache-activemq-5.15.12-bin.tar.gz drwxr-xr-x. 8 10 143 4096 12月 13 2016 jdk1.8.0_121 -rw-r--r--. 1 root root 183246769 3月 29 15:17 jdk-8u121-linux-x64.tar.gz -rw-r--r--. 1 root root 1980762 4月 5 21:18 ubuntu-16.04-x86_64.tar.gz -rw-------. 1 root root 66612224 4月 5 21:25 ubuntu_latest.tar # 2. 載入鏡像,將某個鏡像載入系統 $ sudo docker load --input ubuntu_latest.tar # 或 $ sudo docker load < ubuntu_latest.tar Loaded image: ubuntu:latest
七、上傳鏡像
若我們希望將自己的鏡像上傳到倉庫供別人使用,可使用 docker push 命令,默認上傳到官方倉庫。語法:docker push NAME[:TAG]
# 將我們上面的 test 鏡像上傳到倉庫 $ sudo docker tag test:latest user/test:latest # 創建一個鏡像標籤 $ sudo docker push user/test:latest # 說明:在第一次上傳時,需要我們輸入登錄資訊