WSL2 安装 Docker

WSL2 安装 Docker

方式一:Docker Desktop+WSL2 运行 Docker
Docker Desktop 将 Docker CE、Docker Compose、Kubernets 等软件整合在了一起进行安装,省去了一一安装的烦恼。

Docker Daemon 由于是安装在宿主机上的,因此可以直接使用宿主机的网卡信息对容器进行访问。

下载安装Docker Desktop运行 Docker,可以让你在Windows中方便的管理配置Docker
Docker for Windows:https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe
Doecker for Mac:https://desktop.docker.com/mac/stable/Docker.dmg
傻瓜式安装即可!

配置Docker Desktop使用WSL2运行Docker engine
现在你就可以在对应的子系统中玩 Docker 了,打开安装好的子系统,输入docker info就可以查看到对应的系统信息了.

踩坑预警
使用docker info打印信息时可能会遇到下述信息,提示/var/run/docker.sock权限不足

Client:
Debug Mode: false

Server:
ERROR: 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/info: dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info
图片

/var/run/docker.sock connect permission denied
通过ls -al /var/run/docker.sock查看该文件权限信息,发现文件是root用户或docker组用户才能进行读写

图片

ls -al show files info
可以通过id或groups ${USER}查看当前用户所在的组信息,发现当前用户的附加组信息里并没有docker,那么问题就好解决了:只需将当前用户加入到docker组即可.

图片

id show ${USER} groups
创建 docker 组
sudo groupadd docker
将当前用户加入到docker组
sudo usermod -aG docker ${USER}
刷新用户的用户组信息
你需要先登出再重新登录一次,这样当前系统会重新刷新你的身份信息。或者,使用下列方式不用退出即可刷新当前进程的用户身份信息:

exec su -l ${USER}
此时再运行docker info应该就正常打印信息了!

安装最新Docker Compose
可参考:https://docs.docker.com/compose/install/#install-compose-on-linux-systems

下载当前最新版本是:1.27.4

sudo curl -L “https://github.com/docker/compose/releases/download/1.27.4/docker-compose- ( u n a m e − s ) − (uname -s)- (unames)(uname -m)” -o /usr/local/bin/docker-compose

赋予 docker-compose 可执行权限

sudo chmod +x /usr/local/bin/docker-compose

刷新当前进程信息

exec $SHELL -l

检查docker-compose是否安装成功

docker-compose --version
设置Docker Server镜像源加速
Docker 官方中国区镜像地址:https://registry.docker-cn.com
网易镜像地址:http://hub-mirror.c.163.com
ustc镜像地址:https://docker.mirrors.ustc.edu.cn
阿里云镜像地址设置参考文章底部:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
如果想使用阿里云镜像地址需要有阿里云账号,但我在广州试用了上述的镜像地址,确实设置了阿里云的镜像加速是效果最好的

通过Docker Desktop配置registry-mirrors
如果是使用 Docker Desktop+WSL 方式安装 Docker 直接使用面板即可修改!

{
“experimental”: false,
“debug”: true,
“registry-mirrors”: [
“https://xxxxxx.mirror.aliyuncs.com”,
“http://hub-mirror.c.163.com”
]
}

增加镜像只需要配置 registry-mirrors,对应一个集合,我配置了网易的镜像和阿里云的镜像

{
“experimental”: false,
“debug”: true,
“registry-mirrors”: [
“https://xxxxxx.mirror.aliyuncs.com”,
“http://hub-mirror.c.163.com”
]
}

yyp@DESKTOP-U0COGSO:~$ docker

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
–config string Location of client config files (default “/home/yyp/.docker”)
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and
default context set with “docker context use”)
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level (“debug”|“info”|“warn”|“error”|“fatal”) (default “info”)
–tls Use TLS; implied by --tlsverify
–tlscacert string Trust certs signed only by this CA (default “/home/yyp/.docker/ca.pem”)
–tlscert string Path to TLS certificate file (default “/home/yyp/.docker/cert.pem”)
–tlskey string Path to TLS key file (default “/home/yyp/.docker/key.pem”)
–tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.5.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container’s changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container’s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container’s filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Run ‘docker COMMAND --help’ for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
yyp@DESKTOP-U0COGSO:~$ docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc., v0.5.0)

Server:
ERROR: 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.24/info: dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info
yyp@DESKTOP-U0COGSO:~$ ls -al /var/run/docker.sock
srw-rw---- 1 root docker 0 Jan 17 15:27 /var/run/docker.sock
yyp@DESKTOP-U0COGSO:~$ id
uid=1000(yyp) gid=1000(yyp) groups=1000(yyp),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),117(netdev)
yyp@DESKTOP-U0COGSO:~$
yyp@DESKTOP-U0COGSO:~$
yyp@DESKTOP-U0COGSO:~$
yyp@DESKTOP-U0COGSO:~$ sudo groupadd docker
groupadd: group ‘docker’ already exists
yyp@DESKTOP-U0COGSO:~$ sudo usermod -aG docker U S E R y y p @ D E S K T O P − U 0 C O G S O :   {USER} yyp@DESKTOP-U0COGSO:~ USERyyp@DESKTOPU0COGSO:  exec su -l U S E R P a s s w o r d : y y p @ D E S K T O P − U 0 C O G S O :   {USER} Password: yyp@DESKTOP-U0COGSO:~ USERPassword:yyp@DESKTOPU0COGSO:  docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc., v0.5.0)

Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.2
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.128-microsoft-standard
Operating System: Docker Desktop
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 25.02GiB
Name: docker-desktop
ID: F43Y:4N6C:5FE3:X7TX:PB5S:WBBZ:7I4B:PVXG:VDZH:GBWT:LCJK:DJZH
Docker Root Dir: /var/lib/docker
Debug Mode: true
File Descriptors: 40
Goroutines: 45
System Time: 2021-01-17T07:31:55.8171453Z
EventsListeners: 4
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

WARNING: No blkio weight support
WARNING: No blkio weight_device support
WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

yyp@DESKTOP-U0COGSO:~$ sudo curl -L “https://github.com/docker/compose/releases/download/1.27.4/docker-compose- ( u n a m e − s ) − (uname -s)- (unames)(uname -m)” -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 651 100 651 0 0 315 0 0:00:02 0:00:02 --:–:-- 315
100 11.6M 100 11.6M 0 0 21881 0 0:09:18 0:09:18 --:–:-- 25694
yyp@DESKTOP-U0COGSO:~$ sudo chmod +x /usr/local/bin/docker-compose
yyp@DESKTOP-U0COGSO:~$ exec S H E L L − l y y p @ D E S K T O P − U 0 C O G S O :   SHELL -l yyp@DESKTOP-U0COGSO:~ SHELLlyyp@DESKTOPU0COGSO:  docker-compose --version
docker-compose version 1.27.4, build 40524192
yyp@DESKTOP-U0COGSO:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
yyp@DESKTOP-U0COGSO:~$

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用两种方式在WSL2安装Docker。 第一种方式是通过安装WSL2前提条件和安装Docker来完成。首先,你需要安装WSL2的前提条件。然后,你可以按照指南安装Docker。这种方式适用于原生Linux安装Docker的场景。 第二种方式是使用Docker Desktop for Windows。这个方法提供了更简单的安装过程,并且可以直接在Windows桌面上运行Docker。你可以使用Docker Desktop for Windows来安装Docker,而无需事先安装WSL2。 根据你的需求,你可以选择其中一种方式来安装Docker。如果你希望在WSL2上使用Docker,并且愿意处理一些额外的配置问题,那么选择第一种方式是一个不错的选择。如果你更喜欢简单快捷的安装过程,并且希望在Windows桌面上运行Docker,那么选择Docker Desktop for Windows是更好的选择。 请注意,如果你选择原生Linux安装Docker的方式,Docker可能无法实现开机自启和每次WSL启动IP会发生变化。这是因为WSL2的网络配置可能会导致这些问题。 希望这些信息对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Windows 利用 WSL2 安装 Docker 的2种方式](https://blog.csdn.net/qq_42362605/article/details/124898062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值