Docker:在 wsl2 上安裝 docker


環境

  1. wondwos 10
  2. wsl 2
  3. ubuntu 18.04

背景

從 wsl 2 開始,可以直接在 windows 上安裝 docker,以下基於 windwos 10 環境安裝,由於國外安裝速度太慢了,使用阿里雲的鏡像進行安裝。阿里雲鏡像安裝的教程參考:https://developer.aliyun.com/mirror/docker-ce ,可以根據不同和系統和需求進行選擇。

安裝 docker

更新 apt

jiangbo@jiangbo-work:~$ sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:3 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease
Get:4 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic-security/main Sources [146 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages [677 kB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic-security/universe amd64 Packages [653 kB]
Get:10 http://mirrors.aliyun.com/ubuntu bionic-updates/main Sources [314 kB]
Get:11 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages [897 kB]
Get:12 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 Packages [1061 kB]
Fetched 4242 kB in 1s (3335 kB/s)
Reading package lists... Done

安裝必要的系統工具

jiangbo@jiangbo-work:~$ sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20180409).
curl is already the newest version (7.58.0-2ubuntu3.8).
software-properties-common is already the newest version (0.96.24.32.12).
apt-transport-https is already the newest version (1.6.12).
The following packages were automatically installed and are no longer required:
  aufs-tools cgroupfs-mount containerd.io docker-ce-cli libltdl7 pigz
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 151 not upgraded.

安裝GPG證書

jiangbo@jiangbo-work:~$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
OK

寫入軟件源信息並更新

jiangbo@jiangbo-work:~$ sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Hit:6 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease
Reading package lists... Done
jiangbo@jiangbo-work:~$ sudo apt-get -y update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease
Hit:6 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Reading package lists... Done

安裝 docker

jiangbo@jiangbo-work:~$ sudo apt-get -y install docker-ce
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  docker-ce
0 upgraded, 1 newly installed, 0 to remove and 151 not upgraded.
Need to get 22.9 MB of archives.
After this operation, 109 MB of additional disk space will be used.
Get:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 docker-ce amd64 5:19.03.8~3-0~ubuntu-bionic [22.9 MB]
Fetched 22.9 MB in 2s (9261 kB/s)
Selecting previously unselected package docker-ce.
(Reading database ... 31855 files and directories currently installed.)
Preparing to unpack .../docker-ce_5%3a19.03.8~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce (5:19.03.8~3-0~ubuntu-bionic) ...
Setting up docker-ce (5:19.03.8~3-0~ubuntu-bionic) ...
invoke-rc.d: could not determine current runlevel
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.21) ...

確認安裝完成

jiangbo@jiangbo-work:~$ docker version
Client: Docker Engine - Community
 Version:           19.03.7
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        7141c199a2
 Built:             Wed Mar  4 01:22:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

啟動並配置 docker

啟動 docker

jiangbo@jiangbo-work:~$ sudo service docker start
 * Starting Docker: docker

加入 docker 用戶組

如果沒有將當前用戶加入 docker 用戶組,則直接使用 docker ps 會得到下面的錯誤:

jiangbo@jiangbo-work:~$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

使用 sudo docker ps 則可以看到正常的結果:

jiangbo@jiangbo-work:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

為了方便,不用每次都加上 sudo,執行以下命令:

jiangbo@jiangbo-work:~$ sudo gpasswd -a $USER  docker
Adding user jiangbo to group docker
jiangbo@jiangbo-work:~$ groups
jiangbo adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev

現在在用戶組中還看不到 docker,需要重新連接一下。

重啟 wsl

只要斷開 bash,重新連接 wsl,則可以刷新當前的用戶信息。斷開重連,查看當前的用戶組信息:

jiangbo@jiangbo-work:~$ groups
jiangbo adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev docker

可以看到,已經加入到了 docker 用戶組了。

docker ps

jiangbo@jiangbo-work:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

配置鏡像加速器

由於鏡像默認是到國外去下載,特別慢,需要配置國內的鏡像加速器,增加下載速度。
在 /etc/docker/ 目錄下,增加 daemon.json 文件,並加入以下內容:

{
  "registry-mirrors": [
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ]
}

重啟 docker 服務,使用 sudo service docker restart 命令。

測試 docker

啟動 nginx 服務

jiangbo@jiangbo-work:~$ docker run -d -p8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
03ebe5b8de21837e426b29de62407e22607e415de623e520737043f7f02fa3da

訪問

瀏覽器訪問 localhost:8080,可以看到下面的內容,表示已成功啟動 nginx,docker 環境安裝完成。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


免責聲明!

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



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