WSL運行docker容器


Linux子系統(Windows Subsystem for Linux, WSL)是Windows10上的一個模擬環境,因而並不能完全實現LInux中的各種操作,而且性能也不如一般的虛擬機。得益於此,WSL與Windows交互非常方便,消耗非常低,沒有啟動時間,用來做輕量開發或測試很適合。

網上關於如何在WSL中安裝Docker有很多教程,大多時間較早,都面臨着需要安裝Docker for Windows的問題,這就需要運行虛擬機,對於我資源吃緊的筆記本還是有些負擔,而且對於沒有Win10 Pro(Hyper-V)的用戶也不方便安裝。Win10創意者更新后,在WSL中運行Docker Engine成為可能,當然這個功能正在逐步完善中,當前並不能支持docker全部指令

本機環境

Windows10 Pro 1903


安裝WSL
設置==>更新安全==>開發者選項==>旁加載應用(選擇此模式即可)
Microsoft Store==>搜索Linux==>安裝Ubuntu18.04(可選Ubuntu16.04)

 

安裝Docker

1. 首先執行以下命令安裝Docker

# 下載docker 17.09.0版本【必須使用此版本】
curl -O https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_17.09.0~ce-0~ubuntu_amd64.deb
# 安裝docker
sudo dpkg -i docker-ce_17.09.0~ce-0~ubuntu_amd64.deb
# 安裝依賴
sudo apt -y -f install
# 修改docker權限(非必須)
sudo usermod -aG docker $USER
# 安裝cgroupfs-mount
sudo apt -y install cgroupfs-mount

 

2. 以管理員身份啟動WSL控制台,執行以下命令啟動docker

sudo cgroupfs-mount
sudo service docker start

 

測試Docker

測試安裝結果

$ docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

 

測試運行容器

$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete                                                                                             Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
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/

顯示如上信息,就表示可以正常使用了。

需要注意的是每次電腦重啟后先執行cgroupfs-mount再啟動docker服務。

 

配置

1. docker開啟遠程訪問

# 默認情況下,Docker守護進程Unix socket(/var/run/docker.sock)來進行本地進程通信,而不會監聽任何端口,
# 因此只能在本地使用docker客戶端或者使用Docker API進行操作。如果想在其他主機上操作Docker主機,
# 就需要讓Docker守護進程打開一個HTTP Socket,這樣才能實現遠程通信。

# 編輯docker的配置文件/etc/default/docker修改DOCKER_OPTS
# 同時監聽本地unix socket和遠程http socket(2375)
DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"

# 然后重新啟動docker守護進程。
sudo service docker restart

# 此時發現docker守護進程已經在監聽2375端口,在另一台主機上可以通過該端口訪問Docker進程了。
docker -H IP:2375 images

# 本地操作docker。
docker images

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM