WSL中配置docker环境

Share on:

由于在wsl中无法安装docker daemon,所以无法在wsl中直接使用docker服务。但是为了体验linux的开发环境,我们可以在wsl中安装docker客户端,并通过连接window系统中的docker daemon服务来使用docker。

配置Ubuntu

  1. 在wsl的ubuntu系统中安装docker-cli,docker-compose。
 1# Update apt package list.
 2$ sudo apt-get update -y
 3
 4# Install Docker's package dependencies.
 5$ sudo apt-get install -y \
 6    apt-transport-https \
 7    ca-certificates \
 8    curl \
 9    software-properties-common
10
11# Download and add Docker's official public PGP key.
12$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
13
14# Verify the fingerprint.
15$ sudo apt-key fingerprint 0EBFCD88
16
17# Add the `stable` channel's Docker upstream repository.
18#
19# If you want to live on the edge, you can change "stable" below to "test" or
20# "nightly". I highly recommend sticking with stable!
21$ sudo add-apt-repository \
22   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
23   $(lsb_release -cs) \
24   stable"
25
26# Update the apt package list (for the new apt repo).
27$ sudo apt-get update -y
28
29# Install the latest version of Docker CE.
30$ sudo apt-get install -y docker-ce
31
32# Allow your user to access the Docker CLI without needing root access.
33$ sudo usermod -aG docker $USER
34
35# Install Python and PIP.
36$ sudo apt-get install -y python python-pip
37
38# Install Docker Compose into your user's home directory.
39$ pip install --user docker-compose
40

配置Docker

  1. 允许远程连接docker

docker-setting

  1. 设置ubuntu连接远程的docker daemon
1$ echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
  1. 验证是否正常工作
1$ docker info
2
3$ docker-compose --version
  1. 设置ubuntu的磁盘映射路径,docker需要的路径是/c/Users/nick/dev/myapp, 实际上的挂在路径是/mnt/c/Users/nick/dev/myapp
1$ sudo vi /etc/wsl.conf # 这个文件可能不存在
2
3# Now make it look like this and save the file when you're done:
4[automount]
5root = /
6options = "metadata"
  1. 重启wsl

参考:

Setting Up Docker for Windows and WSL to Work Flawlessly