Docker 基本操作-映象篇(上)
獲取映象
Docker 執行容器前需要本地存在對應的映象,如果本地不存在該映象,Docker 會從映象倉庫下載該映象。 從 Docker 映象倉庫獲取映象的命令是:
$ docker pull [選項] [Docker Registry 地址[:埠號]/]倉庫名[:標籤]
--all-tags , -a --disable-content-trust --platform
示例,從Docker Hub 拉取 ubuntu 系統的映象:
root@iZwz96uyro861qckgz89ljZ:~# docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 84ed7d2f608f: Pull complete be2bf1c4a48d: Pull complete a5bdc6303093: Pull complete e9055237d68d: Pull complete Digest: sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68 Status: Downloaded newer image for ubuntu:latest
列出映象
列出已經下載到本地的映象,使用:
$ docker image ls
或者:
$ docker images
--all, -a --digests --filter, -f --format --no-trunc --quiet, -q
列表包含了倉庫名
、標籤
、映象 ID
、建立時間
以及所佔用的空間
。
示例:
root@iZwz96uyro861qckgz89ljZ:~# docker image ls REPOSITORYTAGIMAGE IDCREATEDSIZE hello-worldlatestfce289e99eb93 weeks ago1.84kB ubuntulatest1d9c17228a9e3 weeks ago86.7MB root@iZwz96uyro861qckgz89ljZ:~# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE hello-worldlatestfce289e99eb93 weeks ago1.84kB ubuntulatest1d9c17228a9e3 weeks ago86.7MB
執行映象
映象是靜態的定義,容器是映象執行時的實體。這裡稱之為執行映象其實是不合理的,因為只有容器才能說是執行。但是,啟動容器有兩種方式,一種是基於映象新建一個容器並啟動,另外一個是將在終止狀態的容器重新啟動。這裡要說的就是基於映象來啟動容器:
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Docker run命名的具體使用將在後續的部落格中進行詳細說明。這裡給出一個簡單的示例,通過執行 ubuntu 容器輸出Hello world
:
root@iZwz96uyro861qckgz89ljZ:~/docker# docker run ubuntu /bin/echo "Hello world" Hello world
刪除本地映象
刪除本地的映象,可以使用 :
$ docker image rm [選項] <映象1> [<映象2> ...]
其中,<映象>
可以是映象短 ID
、映象長 ID
、映象名
或者映象摘要
。
--force , -f --no-prune
root@iZwz96uyro861qckgz89ljZ:~# docker image rm ubuntu Untagged: ubuntu:latest Untagged: ubuntu@sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68 Deleted: sha256:1d9c17228a9e80a0a23927f24f3cf17d012cf0bb3eae5e3541a8c6987ab9bd5a Deleted: sha256:3288cd6e6e7d42bcb4a74121b412c42a11f96da52685e42dbf9de6a747a55c6b Deleted: sha256:b1636589630239bdb9153f95ac564bcd2afd9202aaf8511cbf5a9a37e03daf35 Deleted: sha256:043f492f40c539cfe7cee4cb8aae00ed1d5b19e864fbe6ea35ec92a2333bacc4 Deleted: sha256:2fb7bfc6145d0ad40334f1802707c2e2390bdcfc16ca636d9ed8a56c1101f5b9
建立映象
在執行著的容器的基礎上生成映象:
$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
-a -c -m -p
示例,將容器 9fbbb230909a 儲存為新的映象,並新增提交人資訊和說明資訊:
root@iZwz96uyro861qckgz89ljZ:~/docker# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 9fbbb230909aubuntu"/bin/bash -c 'while…"23 minutes agoUp 23 minutesadmiring_chatterjee root@iZwz96uyro861qckgz89ljZ:~/docker# docker commit -a "fanzhenyu" -m "my ubuntu" 9fbbb230909a ubuntu:my sha256:fe0e9216b2dd575dff6b5521616b55ce996fdb89fd893ba9e80a8a671f6acc2d root@iZwz96uyro861qckgz89ljZ:~/docker# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE ubuntumyfe0e9216b2dd13 seconds ago87.5MB ubuntulatest20bb25d327584 days ago87.5MB
構建映象也可以通過:
$ docker image build [OPTIONS] PATH | URL | -
或:
$ docker build [OPTIONS] PATH | URL | -
使用 Dockerfiles 來實現。Dockerfile 是一個文字檔案,其內包含了一條條的指令,每一條指令構建一層,因此每一條指令的內容,就是描述該層應當如何構建。
示例,以定製 nginx 映象為例,Dockerfile 的內容如下:
FROM nginx RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
在 Dockerfile 檔案所在目錄執行:
root@iZwz96uyro861qckgz89ljZ:~/docker# docker image build -t nginx:my . Sending build context to Docker daemon90.01MB Step 1/2 : FROM nginx latest: Pulling from library/nginx 5e6ec7f28fb7: Pull complete ab804f9bbcbe: Pull complete 052b395f16bc: Pull complete Digest: sha256:56bcd35e8433343dbae0484ed5b740843dd8bff9479400990f251c13bbb94763 Status: Downloaded newer image for nginx:latest ---> 42b4762643dc Step 2/2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html ---> Running in da558d0a335d Removing intermediate container da558d0a335d ---> b6cca0cd24a1 Successfully built b6cca0cd24a1 Successfully tagged nginx:my
特別注意命令最後的.
符號,這個點一定要有,否則會報錯。這裡的.
是在指定上下文路徑 。
當構建的時候,使用者會指定構建映象上下文的路徑, docker build 命令得知這個路徑後,會將路徑下的所有內容打包,然後上傳給 Docker 引擎。這樣Docker 引擎收到這個上下文包後,展開就會獲得構建映象所需的一切檔案。
匯入和匯出映象
匯出映象,將一個或多個映象儲存到tar存檔(預設情況下流式傳輸到標準輸出):
$ docker save [OPTIONS] IMAGE [IMAGE...]
或:
$ docker image save [OPTIONS] IMAGE [IMAGE...]
-
選項
--output , -o
,輸出到的檔案。
示例,匯出 ubuntu 映象為 tar 檔案:
root@iZwz96uyro861qckgz89ljZ:~/docker# docker save -o ubuntu.tar ubuntu root@iZwz96uyro861qckgz89ljZ:~/docker# ls -lh total 86M -rw------- 1 root root 86M Jan 27 10:21 ubuntu.tar
匯入映象,從tar存檔或標準輸入載入映象:
$ docker image load [OPTIONS]
--input , -i --quiet , -q
示例,匯入剛剛匯出的 ubuntu 映象:
root@iZwz96uyro861qckgz89ljZ:~/docker# docker load -i ubuntu.tar adcb570ae9ac: Loading layer [==================================================>]89.95MB/89.95MB 7604c8714555: Loading layer [==================================================>]15.87kB/15.87kB 9e9d3c3a7458: Loading layer [==================================================>]11.26kB/11.26kB 27a216ffe825: Loading layer [==================================================>]3.072kB/3.072kB Loaded image: ubuntu:latest