A few pointers ...
First, you don't mention what Windows version you are running. Windows 11 is required to use the WSL [boot] feature in /etc/wsl.conf (Update note: No longer the case. Please see this answer for details).
But even if you have Windows 11, your example would still have a few problems:
[boot]
command="ping 10.0.0.8 && service docker restart "
First, ping 10.0.0.8, as you point out, is an interactive command. The command that is executed does not have access to a terminal, so you'll need to redirect the output if you want to capture the results.
Second, ping will typically "run forever" in a normal scenario. The ping is never going to "finish" so that your service docker restart is executed. You can reproduce the same thing at the command-line.
Third, since the boot command only executes once when the WSL instance is starting, there's really no need for a service ... restart. Perhaps not a huge deal, since restart will typically start if it isn't already, but I think I've seen some service scripts that will refuse to restart if the service isn't currently started. I can't remember how the Docker init service is scripted.
Step 1: Capture ping output
So (assuming you are on Windows 11), try, for starters:
[boot]
command="ping 10.0.0.8 >> /var/log/mybootping.log"
Exit your Ubuntu/WSL2 instance and, from PowerShell:
wsl -l -v
# Confirm your distribution name -- Change it below if needed
wsl --terminate Ubuntu-20.04
wsl ~ -d Ubuntu-20.04
Then, check cat /var/log/mybootping.log. You should see the results of the ping. Note that it will continue to run, and the log grow, until you either exit the instance or kill the ping process.
Step 2: Allow ping to exit
You can limit this by changing the command to ping -c 4 10.0.0.8 to repeat the ping only 4 times. That way, ping will exit on its own.
Step 3: Capture errors from ping
But let's say that you mistype the commandline:
[boot]
command="ping -c 10.0.0.8 >> /var/log/mybootping.log"
Oops, I left off the number of retries for the -c flag. Note -- I actually did make this mistake when testing this. But /var/log/mybootping.log didn't get the output since it goes to stderr instead of stdout.
Best to make this:
[boot]
command="ping -c 4 10.0.0.8 >> /var/log/mybootping.log 2>&1"
That will capture/append both error and output to the log.
Step 4: Add Docker service
At this point, with ping being able to exit with the -c argument, you can add the Docker service.
[boot]
command="ping -c 4 10.0.0.8 >> /var/log/mybootping.log 2>&1 && service docker start"
Step 5: Capture output and error from both ping and service
But what if you just want to capture all output from the boot command?
[boot]
command="exec 1>>/var/log/wslboot.log && exec 2>&1 && echo -------------------- && date && ping -c 4 10.0.0.8 && service docker start"
For Windows 10
If you don't have Windows 11, then, as mentioned above, you won't be able to use the [boot] feature of wsl.conf.
You can either:
Add the commands to your user's ~/.bashrc as I mention in this answer
Run a one-time "boot" command manually for WSL. For example, from PowerShell:
wsl ~ -u root -d Ubuntu-20.04 -e sh -c "ping -c 4 10.0.0.8 && service docker start"
Since the output will appear in this case, we can skip all of the output/error capture that we used in the [boot] command.
If you have a service that doesn't fork (background itself), like dockerd (from the comments), then you'll need to execute it with nohup. Something like:
wsl ~ -u root -d Ubuntu-20.04 -e nohup sh -c "dockerd &"
Any output will be in /root/nohup.out. If you want to direct it elsewhere, it starts to get a bit more complicated:
wsl ~ -u root -d Ubuntu-20.04 -e sh -c "nohup sh -c 'dockerd &' >> /var/log/dockerd-nohup.out"
I might be wrong, but I believe there's a WSL bug that makes this a bit more difficult. From what I can tell, WSL is overly aggressive about reaping (not yet) orphaned subprocesses of the shell into init.