By design Docker service and Docker Desktop start only when the user has logon to the Windows account. However, there is a way to start theses services as soon as the Windows has started or rebooted without having the user to manually login.
Why does it matter ..
There are some scenarios where this becomes useful when
- docker services are accessed remotely by other machines
- docker services are needed by the Build Pipelines such as Azure Devops
Usually in these cases the Windows machine running Docker services are unattended. It becomes painful when there are multiple servers in the network that are running Docker services and these servers restart due to windows update. In these scenarios a user will have to manually logon to the servers running Docker to have the Docker services started.
There is a way ..
Thankfully there is way to make this automatic. Thanks to inbuilt windows feature “Task Scheduler”. The Task Scheduler enables you to automatically perform routine tasks on a chosen computer.
For this you will need two sets of Powershell scripts;
registerTask script: This script will register a task with the Task Scheduler. This script needs to be executed once.
startDocker script: This will start docker on windows startup. This will get executed automatically by the registered task.
Follow these steps to achieve ..
1. Logon to the windows server/machine where you want the Docker services to start automatically.
2. Create a file called startDocker.ps1 at your location of choice and save the following script inside it:
start-service -Name com.docker.service
start C:\'Program Files'\Docker\Docker\'Docker Desktop.exe'
Verify that the location of Docker.exe is correct on your machine otherwise modify it in the script accordingly.
3. Create a file called registerTask.ps1 and save the following script inside it.
$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-File C:\PowershellScripts\startDocker.ps1"
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Start Docker on Start up" -Settings $settings -User "Your user" -Password "Your user password" -RunLevel Highest
# Add the user to the docker-users user-group
# This is needed so that this user has access to docker services
try {
Add-LocalGroupMember -Group docker-users -Member "Your user" -ErrorAction Stop
} catch [Microsoft.PowerShell.Commands.MemberExistsException] {
}
4. Modify the script: You will have to change a couple of things in the scripts above according to your computer/server.
In the $action line, change the location of startdocker.ps1 script file to where you have placed this file.
In the Register-ScheduledTask line change the account user and password to an account user that needs Docker services to be started at the Windows start-up.
5. Execute registerTask.ps1
Open Windows Powershell as Administrator and set the current directory to where you have placed registerTask.ps1. For example
cd C:\PewershellScripts\
Next execute this script as follows
.\PowershellScripts\
That’s all! Now Docker services will start automatically on Windows start. Please leave a feedback if it helped you or ask for help in the comments if you had troubles following this guide.
Thank you for writing this up.
I use a Microsoft account to log in as a standard user. I have a separate admin user.
My Docker Desktop uses my WSL2 backend.
I needed to create the task as my admin user and configure it to run as the local version of my Microsoft Account and provide the password to my Microsoft account.
It seems to be working, but there are some issues with it.
When I actually log in and start Docker Desktop, I get a generic error.
When I try to manage my containers using the command line, I get errors.
I can find it in task manager and kill it and then restart it interactively which seems to fix things. I’m unsure if my containers all die during this process, but if they do, they start right back up.