天天看點

一文了解Docker核心概念和安裝配置核心概念安裝Docker引擎使用Docker鏡像搜尋相關的鏡像删除和清理鏡像建立鏡像儲存和載入鏡像上傳鏡像

核心概念

Docker 有三大核心概念,分别是鏡像,容器,倉庫。

Docker 鏡像

Docker 鏡像類似于虛拟機的鏡像,可以把它了解為一個隻讀的模闆,例如一個鏡像可以包含一個基本的作業系統環境,裡面僅僅安裝了Apache應用程式,可以把它稱之為一個Apache鏡像。

鏡像是建立Docker容器的基礎。

通過版本管理和增量檔案系統,Docker提供了一套十分簡單的機制來更新現有的鏡像,使用者甚至可以從網上下載下傳一個已經做好的應用鏡像,并進行使用。

Docker 容器

Docker容器類似于一個輕量級的沙箱,Docker利用容器來運作和隔離應用。

容器是從鏡像建立的應用運作執行個體,它可以啟動,開始停止删除,這些容器彼此互相隔離,互相不可見。

可以把容器看做一個簡易版的Linux系統環境,以及運作在其中的應用程式打包而成的盒子。

Docker 倉庫

Docker倉庫類似于代碼倉庫,是Docker集中儲存鏡像檔案的場所。

根據儲存的鏡像是否公開與否,Docker倉庫可以分為公開倉庫和私有倉庫兩種形式。

安裝Docker引擎

下面的執行個體,是在Windows下安裝docker

  1. 通路官網的首頁。 https://www.docker.com/
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_4399e0076da2409fbca524eb76b130ba.jpg
  1. 單擊立即适用,選擇windows版本的下載下傳 https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_12e9b82a1c688641ddb5d086e51fc21e.jpg
  2. 單擊項目進行安裝 https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_9f2be4a32f0d3a82693ef6e562adc412.jpg
  3. 進行安裝 https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_2957d3fbbd07e5ecb35c3b55be414848.jpg
  1. 檢驗是否安裝成功

    打開powershell 輸入docker

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_b16ea20eaa9f07eb3236826c8c1c4acf.jpg

這樣,windows 版本的docker已經安裝完畢

使用Docker鏡像

對于docker鏡像的使用如下

擷取鏡像

擷取docker鏡像,使用如下的指令。

輸入指令擷取docker鏡像

docker pull ubuntu           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_7f2c68d3773513a35460fbdff4640b05.jpg

從下載下傳的過程可以看到,對于docker容器來說,鏡像檔案一般由若幹層組成,

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_c84e9239bd9c7e1c943acf1fa6034a9d.jpg

每一個字元串都是唯一的id,使用docker的pull指令的時候,會一層層的下載下傳相關的鏡像,并由上一層鏡像指向上上一層的鏡像。

檢視鏡像的資訊

使用docke的images指令,檢視docker的鏡像資訊。

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_f65782aecce1535d9c8eac4cbf162947.jpg

上方使用images列出docker的鏡像資訊。

使用tag指令添加鏡像的标簽

為了友善于docker鏡像的使用,使用docker的tag指令,添加docker的鏡像标簽。

docker tag ubuntu:latest myubuntu:laster           

使用的效果如下

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_e36b8495e5c1616569c9c372835d090b.jpg

使用inspect指令檢視docker的詳細資訊

docker image inspect ubuntu           

檢視資訊如下

PS C:\Users\Administrator\Desktop> docker image inspect ubuntu
[
    {
        "Id": "sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303",
        "RepoTags": [
            "myubuntu:laster",
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-06-17T01:20:56.323568224Z",
        "Container": "9477bb1d88721b63e0c6a359eb48a081741a0992480bf34bbcebf4e5734fcf67",
        "ContainerConfig": {
            "Hostname": "9477bb1d8872",
            "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:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.09.7",
        "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:5412cfe47ff528df823761788f99a11568e2d25f48a4245a3d67cdc3e564c839",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 73856440,
        "VirtualSize": 73856440,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/63e1c7f313e414bd5465953da0ceb615cc9c5906c9b539ed164560b18020bc85/diff:/var/lib/docker/overlay2/09f0332099bc97d0052e07879f18271f2c7ea879501498a1eca00711065c9e9a/diff:/var/lib/docker/overlay2/8fef100fbab32d7aa7a3cc70740ef97b45cc9dcfce91453dd10cc502a3fa10f7/diff",
                "MergedDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/merged",
                "UpperDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/diff",
                "WorkDir": "/var/lib/docker/overlay2/3ec5eeae7aa3781aa605fa1a1dc598c39d7774796600b3c1f86e8190c24d5a47/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10",
                "sha256:9e97312b63ff63ad98bb1f3f688fdff0721ce5111e7475b02ab652f10a4ff97d",
                "sha256:ec1817c93e7c08d27bfee063f0f1349185a558b87b2d806768af0a8fbbf5bc11",
                "sha256:05f3b67ed530c5b55f6140dfcdfb9746cdae7b76600de13275197d009086bb3d"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-07-06T13:37:35.064486339Z"
        }
    }
]           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_de05c19b022a20678e0ed0bcb2a7524c.jpg

使用history指令檢視鏡像曆史

docker history ubuntu           
PS C:\Users\Administrator\Desktop> docker history ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
74435f89ab78        2 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           2 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B
<missing>           2 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B
<missing>           2 weeks ago         /bin/sh -c [ -z "$(apt-get indextargets)" ]     1.01MB
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:b2342c7e6665d5ff3…   72.8MB
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_4721c08ad3d34714eb8a731b64a9bebb.jpg

搜尋相關的鏡像

搜尋官方帶nginx的關鍵字的鏡像

PS C:\Users\Administrator\Desktop> docker search --filter=is-official=true nginx
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   13426               [OK]
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_e41554f6c63eecca91e051b77b35d313.jpg

删除和清理鏡像

對鏡像鏡像相關的删除

使用标簽删除鏡像

docker rmi ubuntu           
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_1535871f998dd127ac66dbc585634146.jpg

使用鏡像ID删除鏡像

PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            laster              74435f89ab78        2 weeks ago         73.9MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
PS C:\Users\Administrator\Desktop> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            laster              74435f89ab78        2 weeks ago         73.9MB
PS C:\Users\Administrator\Desktop> docker rmi 74435f89ab78
Untagged: myubuntu:laster
Deleted: sha256:74435f89ab7825e19cf8c92c7b5c5ebd73ae2d0a2be16f49b3fb81c9062ab303
Deleted: sha256:8a8d1f0b34041a66f09e49bdc03e75c2190f606b0db7e08b75eb6747f7b49e11
Deleted: sha256:f1b8f74eff975ae600be0345aaac8f0a3d16680c2531ffc72f77c5e17cbfeeee
Deleted: sha256:27d46ebb54384edbc8c807984f9eb065321912422b0e6c49d6a9cd8c8b7d8ffc
Deleted: sha256:e1c75a5e0bfa094c407e411eb6cc8a159ee8b060cbd0398f1693978b4af9af10
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_4575887edf904f781e33dc7eb67f8d29.jpg

清理鏡像

使用docker鏡像來進行清理

PS C:\Users\Administrator\Desktop> docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_9ceb53d5fd3b6f10e4daaf2d5cddd350.jpg

建立鏡像

基于已有的容器建立鏡像

檢視運作的容器清單

PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
ff7a974b9771        nginx               "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp              fervent_curie           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_31eb0517d3b82578bb8458d016180a48.jpg

建立相關的容器

docker container commit -m "ming" -a "mingming" ff7a974b9771  test:0.1           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_5fe8be93e901f10b43b061be4eb95e77.jpg https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_adf2be8e7e4644db8b55022e9a4e406b.jpg

再次檢視

PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                0.1                 54a6eb8d4483        5 seconds ago       132MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
PS C:\Users\Administrator\Desktop>
           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_02b59b6d24b3b566e2358fb4b18b02c4.jpg

完整的過程如下圖

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_289b5d3131412e0459dd992ac224d333.jpg

使用模闆方式建立

https://download.openvz.org/template/precreated/

下載下傳相關的模闆

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_91d7636f7638c7154833bd938e1701b4.jpg

導入相關的鏡像

cat centos-6-x86-minimal.tar.gz|docker import - centos6           

使用dockerFile建立

# 建構階段
FROM node
# 指定工作目錄
WORKDIR /
# 指定工作端口
EXPOSE 4000
# 安裝gitbook
RUN npm install gitbook -g;
RUN npm install gitbook-cli -g;
# 拉取檔案
RUN git clone https://github.com/mySoul8012/SE-Basic-knowledge.git
# 進入目錄
WORKDIR /SE-Basic-knowledge
# 初始化鏡像
RUN gitbook init
CMD ["gitbook", "serve"]           

輸入建構的鏡像

docker image build -t ming:0.1           

儲存和載入鏡像

存出鏡像

PS C:\Users\Administrator\Desktop> docker save -o ubuntu.tar ubuntu
PS C:\Users\Administrator\Desktop>           
https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_4d0007c0644fec8ecc77dd19cd3c37c9.jpg

載入鏡像

PS C:\Users\Administrator\Desktop> docker load -i ubuntu.tar
Loaded image: ubuntu:latest
PS C:\Users\Administrator\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ff7a974b9771        nginx               "/docker-entrypoint.…"   12 minutes ago      Up 12 minutes       80/tcp              fervent_curie
PS C:\Users\Administrator\Desktop> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                0.1                 54a6eb8d4483        10 minutes ago      132MB
ubuntu              latest              74435f89ab78        2 weeks ago         73.9MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
PS C:\Users\Administrator\Desktop>
           

上傳鏡像

上傳鏡像到docker

PS C:\Users\Administrator\Desktop> docker push test
The push refers to repository [docker.io/library/test]
53d99160caf2: Preparing
f978b9ed3f26: Preparing
9040af41bb66: Preparing
7c7d7f446182: Preparing
d4cf327d8ef5: Preparing
13cb14c2acd3: Waiting
denied: requested access to the resource is denied
PS C:\Users\Administrator\Desktop>           

可以看到拒絕,這裡先登入賬号

docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: melovemingming
Password:
Login Succeeded           

再進行檔案的上傳

docker push melovemingming/nginx           

需要注意,上傳的鏡像格式必須為 使用者名/鏡像名

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_ac0d6931aa016b176f1ad9cb3ae02349.jpg

檢視docker hub 可以看到docker鏡像的上傳

可以看到docker鏡像已經上傳

https://www.iming.info/wp-content/uploads/2020/07/wp_editor_md_9b7a09fe6f9f85b383f017860f486452.jpg