windows下docker desktop的安裝和使用

在windows使用docker根據官方文檔安裝的是Docker Desktop。Docker desktop是Microsoft Windows的Docker的社區版本。 我們可以從Docker Hub下載適用於Windows的Docker desktop。地址是:Docker Desktop for Windows

系統要求

Windows 10 64位:專業版,企業版或教育版。
必須啓用Hyper-V和Containers Windows功能。
要在Windows 10上成功運行Client Hyper-V,需要滿足以下硬件先決條件:

具有二級地址轉換(SLAT)的64位處理器
4GB系統內存
必須在BIOS設置中啓用BIOS級硬件虛擬化支持。 

注意:Docker基於Microsoft對Windows 10操作系統的支持生命週期在Windows上支持Docker Desktop。

此外,對於需要使用Docker Toolbox和Docker Machine的用戶:運行Docker Desktop需要用到Microsoft Hyper-V。 必要時,Docker桌面Windows安裝程序會啓用Hyper-V,然後重新啓動計算機。 啓用Hyper-V後,VirtualBox就無法在使用。 但是,將保留所有現有的VirtualBox VM映像。

使用docker-machine創建的VirtualBox VM(包括通常在Toolbox安裝期間創建的默認VM)不能再啓動。 這些VM不能與Docker Desktop一起使用。 但是,我們仍然可以使用docker-machine管理遠程VM

installer裏面包含的東西

Docker Desktop installer包括Docker Engine,Docker CLI客戶端,Docker Compose,Notary,Kubernetes和Credential Helper。

使用Docker Desktop創建的容器和映像在安裝它的機器上的所有用戶帳戶之間共享。 這是因爲所有Windows帳戶都使用相同的VM來構建和運行容器。

windows上docker desktop安裝流程

  1. 雙擊Docker Desktop Installer.exe運行安裝程序。
  2. 如果尚未下載安裝程序(Docker Desktop Installer.exe),則可以從Docker Hub中獲取它。 
  3. 按照安裝嚮導上的說明接受許可證,授權安裝程序,然後繼續安裝。
  4. 出現提示時,請在安裝過程中使用您的系統密碼授權Docker Desktop Installer。 需要特權訪問才能安裝網絡組件,到Docker應用程序的鏈接以及管理Hyper-V VM。
  5. 在安裝完成對話框上單擊完成,然後啓動Docker Desktop應用程序。

啓動docker desktop

Docker Desktop在安裝後不會自動啓動。 要啓動Docker Desktop,我們可以搜索Docker,然後在搜索結果中選擇Docker Desktop。

search for Docker app

當狀態欄中的鯨魚圖標保持穩定時,Docker desktop已經啓動並運行,並且我們可以從任何終端窗口訪問。

whale on taskbar

我們可以創建docker賬戶,然後用docker賬戶登錄docker desktop:

使用docker desktop

1.我們可以打開一個cmd窗口或者power shell

2.執行命令 docker --version

> docker --version

Docker version 19.03.1

3.從Docker Hub提取hello-world映像並運行一個容器:

docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete                                                                                             Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

4.List出從Docker Hub下載的hello-world映像:

> docker image ls

可以得到下面的內容

其中的hello-world就是剛剛落下來的image

5.list出hello-world 容器以及其他容器

> docker container ls --all

如果去掉命令最後的--all的話,則只會list出正在運行的容器。

6.我們還可以通過下面的命令來查看docker的其他功能

> docker --help
> docker container --help
> docker container ls --help
> docker run --help

 

使用docker application

1.提取Ubuntu OS的映像,並在生成的容器內運行交互式終端:

docker run --interactive --tty ubuntu bash
Unable to find image 'ubuntu:latest' locally
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
root@4c6071c62eed:/#         

2.運行成功之後我們就已經在容器中了。 在root#提示符下,檢查容器的主機名:

root@4c6071c62eed:/# hostname
4c6071c62eed
root@4c6071c62eed:/#

請注意,主機名已分配爲容器ID。

3.使用exit命令退出shell程序,這也會停止容器:

root@4c6071c62eed:/# hostname
4c6071c62eed
root@4c6071c62eed:/# exit
exit

4.使用下面的命令查看所有的container

 docker container ls --all

我們可以發現剛剛的Ubuntu也在裏面了

docker container ls --all
CONTAINER ID        IMAGE                                               COMMAND                  CREATED             STATUS                        PORTS               NAMES
4c6071c62eed        ubuntu                                              "bash"                   5 minutes ago       Exited (0) 51 seconds ago                         goofy_goldberg
061f65b54169        hello-world                                         "/hello"                 49 minutes ago      Exited (0) 49 minutes ago                         pe

5.拉取並且運行Dockerized nginx Web服務器,名字叫做webserver:

docker run --detach --publish 80:80 --name webserver nginx

 Unable to find image 'nginx:latest' locally
 latest: Pulling from library/nginx

 fdd5d7827f33: Pull complete
 a3ed95caeb02: Pull complete
 716f7a5f3082: Pull complete
 7b10f03a0309: Pull complete
 Digest: sha256:f6a001272d5d324c4c9f3f183e1b69e9e0ff12debeb7a092730d638c33e0de3e
 Status: Downloaded newer image for nginx:latest
 dfe13c68b3b86f01951af617df02be4897184cbf7a8b4d5caf1c3c5bd3fc267f

注意,如果80端口被佔用了,那就需要換一個端口號,例如使用90:80。

6. 訪問Web瀏覽器http:// localhost,會顯示nginx起始頁。 (如果是使用80端口號,則直接訪問localhost即可,無需附加80,因爲我們在docker命令中指定了80,而他是默認HTTP端口。否則就使用localhost:90的方式訪問)

7.查看所有在運行的container

docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
e4295758bda5        nginx               "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        0.0.0.0:90->80/tcp   webserver

8.通過我們爲分配的名稱(webserver)停止正在運行的nginx容器:

 >  docker container stop webserver

9.刪除我們剛剛創建的container

docker container rm webserver goofy_goldberg peaceful_gagarin
webserver
goofy_goldberg
peaceful_gagarin

Kubernetes

Docker Desktop包含一個在Windows主機上運行的獨立Kubernetes服務器,因此我們可以測試在Kubernetes上部署Docker work load的方式。

Kubernetes客戶端命令kubectl已包括在內,並配置爲連接到本地Kubernetes服務器。 如果我們自己安裝了kubectl並指向其他環境,例如minikube或GKE集羣,要確保更改上下文,以使kubectl指向docker-desktop.

Docker Desktop Dashboard

Docker Desktop dashboard是docker的UI工具,在2020年的release當中發佈,在此之前有一款基於 Electron 開發的桌面客戶端,名叫 Kitematic,會隨着 Docker Toolbox 一起打包,包含在 Docker for Mac 和 Docker for Windows 中。Dashboard 也是基於 Electron 實現的。

Docker Desktop Dashboard

Docker Desktop Dashboard提供了一個簡單的界面,可以與容器和應用程序進行交互,管理應用程序的生命週期。 Dashboard  UI會顯示所有正在運行,已停止和已啓動的容器及其狀態。 它提供了一個直觀的界面,可以執行常見的操作來檢查,交互和管理Docker對象,包括容器和基於Docker Compose的應用程序。

Docker Desktop Dashboard有這樣一些優點:

  1. 在GUI從CLI獲取信息
  2. 直接在UI中訪問容器日誌以搜索容器行爲
  3. 從UI可以訪問Compose application的日誌
  4. 快速查看容器正在使用的端口
  5. 監視容器資源利用率

在Dashboard當中列出了我們所有的容器和應用,我們可以通過下面的命令創建和啓動一個redis container,打開任何一個CLI,運行下面的命令:

docker run -dt redis

這個命令可以創建一個新的Redis容器。 在Dashboard中我們可以查看新的Redis容器:

接下來,我們可以來創建一個sample application, 我們可以從Docker  samples page, 下載voting app。 voting app是一個可在多個Docker容器中運行的分佈式應用程序。

Example voting app architecture diagram

這個示例的voting app包括:

  1. Python或ASP.NET Core中的前端Web應用程序,可讓在兩個選項之間進行投票
  2. Redis或NATS隊列收集新投票
  3. .NET Core,Java或.NET Core 2.1工作程序,它consume votes並將其存儲
  4. 由Docker支持的Postgres或TiDB數據庫
  5. 一個Node.js或ASP.NET Core SignalR Web應用程序,可實時顯示投票結果

要啓動應用程序,我們在CLI中切換至包含示例投票應用程序的目錄,然後運行docker-compose up --build

如果一切順利會看到如下的message:

docker-compose up --build
Creating network "example-voting-app-master_front-tier" with the default driver
Creating network "example-voting-app-master_back-tier" with the default driver
Creating volume "example-voting-app-master_db-data" with default driver
Building vote
Step 1/7 : FROM python:2.7-alpine
2.7-alpine: Pulling from library/python
Digest: sha256:d2cc8451e799d4a75819661329ea6e0d3e13b3dadd56420e25fcb8601ff6ba49
Status: Downloaded newer image for python:2.7-alpine
 ---> 1bf48bb21060
Step 2/7 : WORKDIR /app
 ---> Running in 7a6a0c9d8b61
Removing intermediate container 7a6a0c9d8b61
 ---> b1242f3c6d0c
Step 3/7 : ADD requirements.txt /app/requirements.txt
 ---> 0f5d69b65243
Step 4/7 : RUN pip install -r requirements.txt
 ---> Running in 92788dc9d682

...
Successfully built 69da1319c6ce
Successfully tagged example-voting-app-master_worker:latest
Creating example-voting-app-master_vote_1   ... done
Creating example-voting-app-master_result_1 ... done
Creating db                                 ... done
Creating redis                              ... done
Creating example-voting-app-master_worker_1 ... done
Attaching to db, redis, example-voting-app-master_result_1, example-voting-app-master_vote_1, example-voting-app-master_worker_1
...

但是博主在執行時遇到了一個錯誤:

Creating example-voting-app-master_vote_1 ... error                                                                
ERROR: for example-voting-app-master_vote_1  Cannot create container for service vote: status code not OK but 500: {"Message":"Unhandled exception: Drive has not been shared"}

ERROR: for vote  Cannot create container for service vote: status code not OK but 500: {"Message":"Unhandled exception: Drive has not been shared"}
ERROR: Encountered errors while bringing up the project.

原因在於沒有共享磁盤,右擊docker desktop,就是電腦右下角的圖標。點擊“settings”再點擊“resources”,“file sharing”,然後把磁盤都勾上,就可以了

設置完成之後,再次執行命令,可以看到下面的log

Starting redis                              ... done                                                               Starting example-voting-app-master_result_1 ... done                                                               Starting db                                 ... done                                                               Creating example-voting-app-master_vote_1   ... done                                                               Starting example-voting-app-master_worker_1 ... done                                                               Attaching to redis, db, example-voting-app-master_vote_1, example-voting-app-master_result_1, example-voting-app-master_worker_1
db        |
db        | PostgreSQL Database directory appears to contain a database; Skipping initialization
db        |
db        | LOG:  database system was interrupted; last known up at 2020-04-14 20:52:31 UTC
redis     | 1:C 15 Apr 2020 04:37:53.103 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis     | 1:C 15 Apr 2020 04:37:53.103 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
redis     | 1:C 15 Apr 2020 04:37:53.103 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis     | 1:M 15 Apr 2020 04:37:53.105 * Running mode=standalone, port=6379.
redis     | 1:M 15 Apr 2020 04:37:53.105 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis     | 1:M 15 Apr 2020 04:37:53.105 # Server initialized
redis     | 1:M 15 Apr 2020 04:37:53.105 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis     | 1:M 15 Apr 2020 04:37:53.105 * Ready to accept connections
db        | LOG:  database system was not properly shut down; automatic recovery in progress
db        | LOG:  record with zero length at 0/16BCAF0
db        | LOG:  redo is not required
db        | LOG:  MultiXact member wraparound protections are now enabled
db        | LOG:  database system is ready to accept connections
db        | LOG:  autovacuum launcher started
result_1  | [nodemon] 2.0.3
result_1  | [nodemon] to restart at any time, enter `rs`
result_1  | [nodemon] watching path(s): *.*
result_1  | [nodemon] watching extensions: js,mjs,json
result_1  | [nodemon] starting `node server.js`
vote_1    |  * Serving Flask app "app" (lazy loading)
vote_1    |  * Environment: production
vote_1    |    WARNING: This is a development server. Do not use it in a production deployment.
vote_1    |    Use a production WSGI server instead.
vote_1    |  * Debug mode: on
vote_1    |  * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
vote_1    |  * Restarting with stat
worker_1  | Connected to db
worker_1  | Connecting to redis
worker_1  | Found redis at 172.21.0.2
result_1  | Wed, 15 Apr 2020 04:37:54 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:73:9
result_1  | Wed, 15 Apr 2020 04:37:54 GMT body-parser deprecated undefined extended: provide extended option at ../node_modules/body-parser/index.js:105:29
result_1  | App running on port 80
result_1  | Connected to db
vote_1    |  * Debugger is active!
vote_1    |  * Debugger PIN: 332-372-563

查看docker dash board,我們可以看到voting app及其組件已經成功運行了

我們可以在dashboard上看到正在運行的容器和應用程序的列表,除此之外,我們可以執行下面的操作

  1. 單擊端口以在瀏覽器中打開容器公開的端口。
  2. 單擊CLI打開終端並在容器上運行命令。
  3. 單擊 StopStartRestart, 或 Delete以對容器執行生命週期操作。

然後我們可以單擊application進入其CLI,這裏我們可以看到這個應用的log,另外我們可以在底部搜索log並且copy

單擊特定container可以獲取有關該container的詳細信息。 容器視圖顯示logs,inspect和stats選項卡,並提供操作按鈕以執行各種操作。

選擇logs以查看來自容器的日誌。 還可以在日誌中搜索特定事件,並將日誌複製到剪貼板。

選擇inspect以查看有關容器的底層信息。 您可以看到本地路徑,映像的版本號,SHA-256,端口映射和其他詳細信息。

選擇stats信息以查看有關容器資源利用率的信息。 您可以看到容器使用的CPU,磁盤I / O,內存和網絡I / O的數量。

還可以使用頂部欄上的快速操作按鈕執行常見操作,例如打開CLI以在容器中運行命令,以及執行生命週期操作,例如停止,啓動,重新啓動或刪除容器。

另外單擊端口可以在瀏覽器中打開容器公開的端口,如果應用程序有UI則我們就可以直接訪問他的UI了,例如voting app

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章