Thursday, May 16, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 8266  / 1 Year ago, wed, may 17, 2023, 1:44:41

I've installed Docker to WSL Ubuntu 20.04 distr.
To make it run I need:


sudo dockerd

It runs but it blocks my terminal tab. To work with containers I need to open the second tab.
I've tried to use sudo dockerd & - it seems to run dockerd in the background, but when I change a directory, it stops.


enter image description here


How can I run dockerd in the background and change directories without stopping?


More From » 20.04

 Answers
1

The standard way to run the Docker Engine daemon (without Docker Desktop) under WSL Ubuntu is simply:


sudo service docker start

This handles the daemonization of it along with many other tasks. You can see the full script by examining /etc/init.d/docker. Run this instead of trying to manually replicate the startup process.


If you want it to automatically start, there are several options:



  • You've already seen the other answer to modify your startup scripts. Personally, I'm not a huge fan of this method. First, I like to keep my startup scripts as lean as possible for best performance. Second, modifications like this tend to "accumulate" over time, making troubleshooting your shell more difficult in the future. That said, it's not a horrible option for WSL, at least on Windows 10.


    However, if you really do want to go this route, there's a much easier way than the other answer. Simply add the following line to your ~/.bash_profile (since it sounds like you are using Bash):


    wsl.exe -u root -e sh -c "service docker status > /dev/null || service docker start"

    That's all. You do not need to make modifications to your sudoers since wsl.exe -u root executes the session as the root user without needing a password.


    But definitely use your ~/.bash_profile for this, not ~/.bashrc. The former is executed only for login shells, but the later is executed for all interactive shells, adding additional overhead.



  • Windows 11 makes this even easier (if you can upgrade) with a special configuration for services you want to run when the WSL instance starts. Just sudo vi /etc/wsl.conf with the following contents:


    [boot]
    command="service docker start"

    This will only execute once, when Ubuntu is started. If the instance is shutdown with wsl --terminate Ubuntu or wsl --shutdown, it will run again the next time you start Ubuntu.


    These commands also run as root, with no password necessary.



  • If you'd like to start Docker Engine whenever you login to Windows, create a "Scheduled Task" in Windows which runs on Logon and points to wsl.exe with the arguments being -u root -e sh -c "service docker status || service docker start"




[#1014] Thursday, May 18, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
piscen

Total Points: 134
Total Questions: 117
Total Answers: 133

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
piscen questions
Sun, Oct 23, 22, 16:06, 2 Years ago
Thu, Nov 24, 22, 19:09, 2 Years ago
Mon, Mar 6, 23, 02:42, 1 Year ago
Wed, May 26, 21, 05:44, 3 Years ago
;