Docker for Windows 使用入門
歡迎來到Docker for Windows!
Docker是用於建立Docker應用程式的完整開發平臺,Docker for Windows是在Windows系統上開始使用Docker的最佳方式。
如果你沒有安裝Docker for Windows,請閱讀這篇文章:Windows 10 安裝 Docker for Windows
檢查Docker Engine,Compose和Machine的版本
啟動您最喜愛的shell(cmd.exe,PowerShell或其他)來檢查docker和docker-compose的版本,並驗證安裝。
PS C:\Users\Docker> docker --version
Docker version 17.03.0-ce, build 60ccb22
PS C:\Users\Docker> docker-compose --version
docker-compose version 1.11.2, build dfed245
PS C:\Users\Docker> docker-machine --version
docker-machine version 0.10.0, build 76ed2a6
瀏覽應用程式並執行示例
接下來的幾個步驟將通過一些例子。 這些只是建議您在系統上測試Docker的方法,檢查版本資訊,並確保docker命令正常工作。
1.開啟一個shell(cmd.exe,PowerShell或其他)。
2.執行一些Docker命令,例如docker ps,docker version和docker info。
這是在powerhell中執行的docker ps的輸出。 (在這個例子中,沒有容器正在執行。)
PS C:\Users\jdoe> docker ps
CONTAINER>
以下是docker version的命令輸出示例。
PS C:\Users\Docker> docker version
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 10:40:59 2017
OS/Arch: windows/amd64
Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64
Experimental: true
以下是docker info的命令輸出示例。
PS C:\Users\Docker> docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.03.0-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.12-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.934 GiB
Name: moby
ID: BM4O:645U:LUS6:OGMD:O6WH:JINS:K7VF:OVDZ:7NE4:ZVJT:PSMQ:5UA6
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 13
Goroutines: 21
System Time: 2017-03-02T16:59:13.417299Z
EventsListeners: 0
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
上面的輸出是例子。 您的輸出,例如docker版本和docker資訊,將取決於您的產品版本(例如,在安裝較新版本時)。
3.執行docker run hello-world來測試從Docker Hub拉一個影象並啟動一個容器。
PS C:\Users\jdoe> docker run hello-world
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.
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.
4.嘗試更有意義的東西,並使用此命令執行Ubuntu容器。
PS C:\Users\jdoe> docker run -it ubuntu bash
這將下載ubuntu容器image並啟動它。 以下是在powerhell中執行此命令的輸出。
PS C:\Users\jdoe> docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5a132a7e7af1: Pull complete
fd2731e4c50c: Pull complete
28a2f68d1120: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:4e85ebe01d056b43955250bbac22bdb8734271122e3c78d21e55ee235fc6802d
Status: Downloaded newer image for ubuntu:latest
輸入exit以停止容器並關閉powerhell。
5.使用此命令啟動Dockerized Web伺服器:
PS C:\Users\jdoe> docker run -d -p 80:80 --name webserver nginx
這將下載nginx容器image並啟動它。 以下是在powerhell中執行此命令的輸出。
PS C:\Users\jdoe> docker run -d -p 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
6.將您的Web瀏覽器指向http:// localhost 以顯示起始頁面。
(由於您指定了預設HTTP埠,因此無需在URL的末尾追加:80)。
7.執行docker ps時,您的網路伺服器正在執行,以檢視容器上的詳細資訊。
PS C:\Users\jdoe> docker ps
CONTAINER>
NAMES
dfe13c68b3b8 nginx "nginx -g 'daemon off" 3 days ago Up 45 seconds 0.0.0.0:80->80/tcp, 443/tc
p webserver
8.停止或移除容器和映象。
nginx網路伺服器將繼續在該埠上的容器中執行,直到您停止和/或刪除該容器。 如果要停止Web伺服器,請鍵入:docker stop webserver,並使用docker start webserver重新啟動它。
要使用單個命令停止並刪除正在執行的容器,請鍵入:docker rm -f webserver。 這將刪除容器,而不是nginx映象。 您可以列出本地映象與docker映象。 您可能想要保留一些映象,以便您不必再從Docker Hub再次拉出。 要刪除不再需要的映象,請使用docker rmi,後跟映象ID或映象名稱。 例如,docker rmi nginx。
PowerShell設定 tab completion
如果您希望為Docker命令提供方便的tab completion,可以按如下方式安裝posh-docker PowerShell模組。
先決條件說明:
根據您的設定,您可能需要NuGet軟體包管理器,以便您可以獲得安裝的提示。
確保您具有以管理員許可權執行PowerShell的許可權。
1.啟動 PowerShell(以管理員身份執行)。
為此,請搜尋PowerShell,右鍵單擊,然後選擇以管理員身份執行。
當系統詢問您是否允許此應用更改您的裝置時,單擊是。
2.設定指令碼執行策略,允許由受信任的釋出者簽名的下載指令碼在您的計算機上執行。 為此,請在PowerShell提示符下鍵入。
Set-ExecutionPolicy RemoteSigned
要檢查策略設定是否正確,請執行get-executionpolicy,這將返回RemoteSigned。
3.要安裝posh-docker PowerShell模組以自動完成Docker命令,請鍵入:
Install-Module posh-docker
或者,僅為當前使用者安裝模組,請鍵入:
Install-Module -Scope CurrentUser posh-docker
剩下的的有時間在翻譯。
https://docs.docker.com/docker-for-windows/#set-up-tab-completion-in-powershell