In the end, it couldn't be resolved, and I was torn
I. "Solution"#
Previously recommended: "Exploration of 'Troubleshooting' VSCode + wsl2 + Docker_Computer Networking_沉冰浮水"
The most common command found online seems to not work, so I have been relying on using the input method to quickly enter sudo service docker start
to run it, but it is indeed very troublesome;
sudo systemctl enable docker
# Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
# Executing: /lib/systemd/systemd-sysv-install enable docker
I don't know why I didn't even want to try a few of the solutions in the middle, and then I clicked on an English one:
How to automatically start the Docker daemon on WSL2 – NillsF blog
https://blog.nillsf.com/index.php/2020/06/29/how-to-automatically-start-the-docker-daemon-on-wsl2/
~This solution requires installing zsh, installation can be found in the second section of this article;~
· First, you need to set up your user so that you don't need to enter a password when executing sudo. I have already set it up, although I don't understand why it works, so I didn't take notes;
· Write the following content to ~/.bashrc
:
echo '# Start Docker daemon automatically when logging in if not running.' >> ~/.bashrc
echo 'RUNNING=`ps aux | grep dockerd | grep -v grep`' >> ~/.bashrc
echo 'if [ -z "$RUNNING" ]; then' >> ~/.bashrc
echo ' sudo dockerd > /dev/null 2>&1 &' >> ~/.bashrc
echo ' disown' >> ~/.bashrc
echo 'fi' >> ~/.bashrc
source ~/.bashrc
· Execute the following command, the effect is that you can run the docker command without sudo
?:
sudo usermod -a -G docker $USER
# -G modifies the supplementary group of the user
# -a option indicates append, used only with -G option, indicating that the user is added to the group
II. Install zsh#
Actually, if you only need to solve the self-starting of Docker, you don't need to switch to zsh. It's just that my English is not good enough and I missed the original information;
Configuration and usage of Oh-My-Zsh - Goodbye Ideal_ - Cnblogs
https://www.cnblogs.com/monsterdev/p/11166720.html
sudo apt update
sudo apt install git zsh -y
# Install Oh My Zsh
sh -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ohmyzsh/ohmyzsh@master/tools/install.sh)"
# Switch to zsh
chsh -s /bin/zsh
# Check version
zsh --version
# zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
# Check current shell
# If the switch is not successful, you can close the terminal or reopen VSCode to check again
echo $SHELL
# /bin/zsh
# sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"