Ubuntu Start Docker On Boot

This will control how Docker should handle starting of the container upon startup and re-starting of the container when it exits. I’ve used the ‘always’ option so far, and can confirm that it makes Docker auto-start the container at system boot: sudo docker run -restart=always -d myimage Documentation Excerpt.

  1. Starting Docker from your.bashrc doesn't seem like a good idea. Anyway, Why not do a wsl -user root -distribution Ubuntu /rundocker.sh or something like that? Or, why not just install and use 'Docker Desktop' which runs it as a separate distro and does all the hard work for you?
  2. It then suggested restarting Docker with sudo /etc/init.d/docker restart. When I did this under Ubuntu 14.04 it reported back that: Docker is managed via upstart, try using service docker restart; I tried sudo service docker restart to which it reported: stop: Unknown job: docker. Start: Unknown job: docker. Now I'm stuck with the docker daemon.
  3. Before you can run Docker container startup commands, you must first create a Dockerfile. A Dockerfile is a text document that contains a list of commands to build containers, Docker images and determines how a Docker image is created.
  4. Sep 22, 2021 I have a local VM running on Ubuntu that, on start, I need to run two commands on (as my user, andreas): sudo mount -a docker-compose up -d The last command is run in my home directory, and can't be run as root otherwise docker gives me grief.

I like running Docker containers in WSL2. However, one piece of nuisance for me has been that the Docker daemon doesn’t automatically start, and there’s no ‘easy’ way to start it automatically. Since WSL2 doesn’t run systemd, you cannot use systemd to automatically start Docker as you typically would in a Linux system.


In this post I’ll explore one way using which this can be done in WSL2 (and I assume this works for WSL1 as well, but I haven’t tested that).

Details of the system I’m running this on:

  • WSL2 on top of Windows 10
  • Ubuntu 18.04

Automatically start Docker daemon on WSL2

Docker Start Containers On Boot Ubuntu

First, you’ll need to install Docker.

With Docker installed, we’ll now need a way to run the Docker daemon automatically at boot time. One way this can be done is to run the command to execute to Docker daemon at boot time via your profile file. To do this without your command line prompting for passwords, run the following command:

This will open your /etc/sudoers file, which controls how sudo command are executed. We’ll want to allow our user to start dockerd without being prompted for a password. Add the following line to the bottom:

(replace nilfranadmin with your username, unless you want to give me access to your system 🤣).

Now, we will update our profile file to automatically run the docker daemon if it’s not running yet. This can be done by adding the following lines to your profile. Please note, I’m using the zsh shell. If you’re using bash (like most people), you’ll need to change .zshrc by .bashrc.

Finally, add your username to the docker group, so you don’t need to use sudo to run Docker commands.


Logout and log back in (or open a new terminal) for all changes to take effect. With the changes in effect, you should be able to run:

And see the hello-world from Docker.


Ubuntu Start Docker On Boot Usb

This was a short post, explaining how to setup your WSL system to automatically start Docker when you open your terminal. It’s a bit of a hacky solution, but it’ll make starting the Docker daemon a bit easier.