天天看點

docker使用linux鏡像,Docker入門基礎之鏡像使用

當運作容器時,使用的鏡像如果在本地中不存在,docker 就會自動從 docker 鏡像倉庫中下載下傳,預設是從 Docker Hub 公共鏡像源下載下傳。

下面我們來學習:

1.管理和使用本地 Docker 主機鏡像

2.建立鏡像

列出鏡像清單

我們可以使用 docker images 來列出存在于主控端上的鏡像。

[email protected]:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

alpine latest 055936d39205 3 weeks ago 5.53MB

MySQL 5.7 7faa3c53e6d6 3 weeks ago 373MB

ubuntu 15.04 d1b55fd07600 3 years ago 131MB

ubuntu 16.04 2a697363a870 2 weeks ago 119MB

各個選項說明:

REPOSITORY:表示鏡像的倉庫源

TAG:鏡像的标簽

IMAGE ID:鏡像ID

CREATED:鏡像建立時間

SIZE:鏡像大小

同一倉庫源可以有多個 TAG,代表這個倉庫源的不同個版本,如ubuntu倉庫源裡,有16.04、15.04等多個不同的版本,我們使用 REPOSITORY:TAG 來定義不同的鏡像。

以相應的鏡像啟動容器

我們如果要使用版本為16.04的ubuntu系統鏡像來運作容器時,指令如下:

[email protected]:~# docker run -it ubuntu:16.04 sh

如果要使用版本為15.04的ubuntu系統鏡像,則指令如下:

[email protected]:~# docker run -it ubuntu:15.04 sh

各個參數解析:

docker: Docker 的二進制執行檔案。

run: 與前面的 docker 組合來運作一個容器。

-it: 其實是兩個參數組成, -i 和 -t,-i:允許你對容器内的标準輸入 (STDIN) 進行互動。-t:在新容器内指定一個僞終端或終端。

ubuntu:15.04指定要運作的鏡像,Docker首先從本地主機上查找鏡像是否存在,如果不存在,Docker 就會從鏡像倉庫 Docker Hub 下載下傳公共鏡像。

sh: 執行指令。

擷取一個新鏡像

當我們在主控端上使用一個不存在的鏡像時 Docker 就會自動下載下傳這個鏡像。如果我們想預先下載下傳這個鏡像,我們可以使用 docker pull 指令來下載下傳它。

[email protected]:~# docker pull ubuntu:18.04

18.04: Pulling from library/ubuntu

6abc03819f3e: Pull complete

05731e63f211: Pull complete

0bd67c50d6be: Pull complete

Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5

Status: Downloaded newer image for ubuntu:18.04

下載下傳完成後,我們可以直接使用這個鏡像來運作容器。

查找鏡像

我們可以從 Docker Hub 網站來搜尋鏡像,Docker Hub 網址為: https://hub.docker.com/

我們也可以使用 docker search 指令來搜尋鏡像。比如我們需要一個httpd的鏡像來作為我們的web服務。我們可以通過 docker search 指令搜尋 httpd 來尋找适合我們的鏡像。

docker使用linux鏡像,Docker入門基礎之鏡像使用

NAME: 鏡像倉庫源的名稱

DESCRIPTION: 鏡像的描述

OFFICIAL: 是否docker官方釋出

拉取httpd鏡像

我們決定使用上圖中的httpd 官方版本的鏡像,使用指令 docker pull 來下載下傳鏡像。

[email protected]:~# docker pull httpd

Using default tag: latest

latest: Pulling from library/httpd

743f2d6c1f65: Already exists

c92eb69846a6: Pull complete

2211b052800a: Pull complete

aed180197314: Pull complete

7c472a4980a7: Pull complete

Digest: sha256:a35ad614c1ffc6fe931f12dc42b682edbdcc62cf78d8edc41499dd90ef0f8003

Status: Downloaded newer image for httpd:latest

下載下傳完成後,我們就可以使用這個鏡像了。

建立鏡像

當我們從docker鏡像倉庫中下載下傳的鏡像不能滿足我們的需求時,我們可以通過以下兩種方式對鏡像進行更改。

1.從已經建立的容器中更新鏡像,并且送出這個鏡像

2.使用 Dockerfile 指令來建立一個新的鏡像

更新鏡像

更新鏡像之前,我們需要使用鏡像來建立一個容器。

[email protected]:~# docker run -it ubuntu:16.04 sh

在運作的容器内使用 apt-get update 指令進行更新。

# apt-get update

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]

Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]

Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]

Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]

Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]

Get:6 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844 kB]

Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]

Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]

Get:9 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]

Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB]

Get:11 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]

Get:12 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB]

Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]

Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB]

Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]

Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]

Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]

Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B]

Fetched 15.8 MB in 8s (1933 kB/s)

Reading package lists... Done

在完成操作之後,輸入 exit指令來退出這個容器。

通過 docker ps -a 檢視所有容器:

[email protected]:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

12847d9f5071 ubuntu:16.04 "sh" 2 minutes ago Exited (0) 43 seconds ago peaceful_edison

3cec6f5e47c0 httpd "httpd-foreground" 5 minutes ago Exited (0) 5 minutes ago vigilant_heisenberg

此時ID為12847d9f5071的容器,是按我們的需求更改的容器。我們可以通過指令docker commit來送出容器副本。

[email protected]:~# docker commit -m="has update" -a="yeqing112" 12847d9f5071 yeqing112/ubuntu:v2

sha256:5cce1167ee4c083ff9b87e61a58081805e76d015a12a4811bd152263aa013860

各個參數說明:

-m: 送出的描述資訊。

-a: 指定鏡像作者。

12847d9f5071: 容器ID。

yeqing112/ubuntu:v2: 指定要建立的目标鏡像名。

我們可以使用 docker images 指令來檢視我們的新鏡像 yeqing112/ubuntu:v2:

[email protected]:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

yeqing112/ubuntu v2 5cce1167ee4c 2 minutes ago 144MB

ubuntu 16.04 2a697363a870 2 weeks ago 119MB

從上面的資訊中可以看到我們更新過的鏡像,檔案體積明顯比之前大了一些。

建構鏡像

除了上面更新鏡像的方法,我們還可以使用指令 docker build , 從零開始來建構一個新的鏡像。為此,我們需要建立一個 Dockerfile 檔案,其中包含一組指令來告訴 Docker 如何建構我們的鏡像。

[email protected]:~# cat Dockerfile

FROM ubuntu:16.04

MAINTAINER Fisher "[email protected]"

RUN apt-get update

EXPOSE 22

EXPOSE 80

CMD /bin/bash

每一個指令都會在鏡像上建立一個新的層,每一個指令的字首都必須是大寫的。

第一條FROM,指定使用哪個鏡像源

RUN 指令告訴docker 在鏡像内執行指令,安裝了什麼

這裡我們還是一樣,執行一個 apt-get update 更新指令

然後,我們使用 Dockerfile 檔案,通過 docker build -t yeqing112/ubuntu:v3 . 指令來建構一個新鏡像。

[email protected]:~# [email protected]:/home# docker build -t yeqing112/ubuntu:v3 .

Sending build context to Docker daemon 422.4kB

Step 1/6 : FROM ubuntu:16.04

---> 2a697363a870

Step 2/6 : MAINTAINER Fisher "[email protected]"

---> Running in 92229e7a091a

Removing intermediate container 92229e7a091a

---> 9a89f4debc7d

Step 3/6 : RUN apt-get update

---> Running in d709ef335784

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]

Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]

Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]

Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]

Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [844 kB]

Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]

Get:7 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]

Get:8 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]

Get:9 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]

Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [556 kB]

Get:11 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]

Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1237 kB]

Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]

Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [967 kB]

Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]

Get:16 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]

Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8532 B]

Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6113 B]

Fetched 15.8 MB in 5s (3051 kB/s)

Reading package lists...

Removing intermediate container d709ef335784

---> 540751496556

Step 4/6 : EXPOSE 22

---> Running in 9ae528bc083f

Removing intermediate container 9ae528bc083f

---> cfccc621a52d

Step 5/6 : EXPOSE 80

---> Running in efd88e9349c1

Removing intermediate container efd88e9349c1

---> 428cb5221a06

Step 6/6 : CMD /bin/bash

---> Running in 2bc08b664f97

Removing intermediate container 2bc08b664f97

---> 9c55ea9b9ee8

Successfully built 9c55ea9b9ee8

Successfully tagged yeqing112/ubuntu:v3

參數說明:

-t:指定要建立的目标鏡像名

.:Dockerfile 檔案所在目錄,可以指定Dockerfile 的絕對路徑

建構成功後,使用 docker images 檢視鏡像:

[email protected]:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

yeqing112/ubuntu v3 9c55ea9b9ee8 9 seconds ago 144MB

yeqing112/ubuntu v2 5cce1167ee4c 16 minutes ago 144MB

ubuntu 16.04 2a697363a870 2 weeks ago 119MB

由上面的資訊看出,新的鏡像v3與v2檔案體積是一樣的,兩個鏡像都隻是執行了apt-get update

設定鏡像标簽

我們可以使用 docker tag 指令,為鏡像添加一個新的标簽。

[email protected]:/home# docker tag 9c55ea9b9ee8 yeqing112/ubuntu:v3-1

docker tag 鏡像ID,這裡是 9c55ea9b9ee8,使用者名稱、鏡像源名(repository name)和新的标簽名(tag)。

使用 docker images 指令可以看到,ID為9c55ea9b9ee8的鏡像多了一個v3-1的标簽。

[email protected]:/home# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

yeqing112/ubuntu v3 9c55ea9b9ee8 21 minutes ago 144MB

yeqing112/ubuntu v3-1 9c55ea9b9ee8 21 minutes ago 144MB

yeqing112/ubuntu v2 5cce1167ee4c 38 minutes ago 144MB

ubuntu 16.04 2a697363a870 2 weeks ago 119MB

docker使用linux鏡像,Docker入門基礎之鏡像使用