在window的wsl2子系统Linux ubunt中使用docker

183 阅读1分钟

在wsl2子系统Linux ubunt中使用docker

安装doocker

  1. linxu终端输入
docker
  1. 按照提示安装docker

  2. 用docker --version命令查看是否安装成功

docker --version


#得到的结果
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2

但此时并不能实际运行docker容器,因为docker运行依赖的daemon服务还没有在后台运行

给daemon服务的启动程序dockerd添加sudo权限

  1. 打开终端并运行以下命令:
sudo visudo
  1. 在打开的文件末尾添加以下行:
<username> ALL=(ALL) NOPASSWD: /usr/bin/dockerd

请将<username>替换为你的用户名。

//查看用户名
whoami
  1. 保存并退出文件。您可以使用快捷键Ctrl + X退出文件,然后输入Y并回车以保存更改。

现在,您应该可以在任何地方使用以下命令来启动dockerd而不需要输入密码:

sudo /usr/bin/dockerd

给每次登入WSL子系统添加自动启动dockerd程序的启动项

在WSL子系统中的.bashrc文件中添加一个脚本,该脚本在每次登录后自动启动dockerd程序。

请按以下步骤操作:

  1. 打开终端并运行以下命令:
nano ~/.bashrc
  1. 在文件末尾添加以下内容:
sudo /usr/bin/dockerd &
  1. 保存并退出文件。您可以使用快捷键Ctrl + X退出文件,然后输入Y并回车以保存更改。

接下来,每当您登录WSL子系统时,系统都会自动启动dockerd程序

把当前用户组添加到docker组

sudo usermod -aG docker $USER

配置完成后重启WSL的Terminal,运行docker run hello-world测试。

 ~  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.
    (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/

文章借鉴