Run docker as a service

I am using Docker for Windows on Windows 10. I run docker-compose to start multiple Linux containers (php, mysql, redis, nginx). My problem is that after a reboot I have to log in to Windows and start the docker gui to get my containers running. Without starting the gui, the containers remain stopped.

Is there a way to make sure that all containers get started after a reboot without a user logging in? I already tried dockerd --register-service but that doesn’t seem to be enough.

Does anyone have any experience with this?

Thanks!

3 Likes

IMO a Microsoft forum may be an additional option, for this question.
I’d start at the container docs: https://docs.microsoft.com/en-us/virtualization/windowscontainers/about/

Thank you for your suggestion. The docs are primarily targeted at running a Windows container on Windows. I can get dockerd running as a service, but this won’t work with Linux containers. What I need is the environment created by Docker for Windows where the containers run in the MobyLinuxVM. I cannot find any resources on how to get this working without a logged in user session…

I have the same problem.
The MobyLinuxVM is only created and started when a user logs into windows.
What is the purpose of the “Docker for Windows Service”. It is set to autostart and I thought it is responsible for bringing up docker. But it doesn’t seem to do anything although it is starting / running at boot time.
The following is from the log file.
The DockerService is started at boot (12:47) but the VM is only created when i log in (13:00)
Am I missing something?

Version: 1.13.1 (10072)
Channel: Stable
Sha1: 94675c5a765e2c51fba6c10b87e0a1a4ed3dde51
Started on: 2017/03/01 12:47:03.246
Resources: C:\Program Files\Docker\Docker\Resources
OS: Windows 10 Pro
Edition: Professional
Id: 1607
Build: 14393
BuildLabName: 14393.693.amd64fre.rs1_release.161220-1747
File: C:\ProgramData\Docker\service.txt
CommandLine: "C:\Program Files\Docker\Docker\com.docker.service"
You can send feedback, including this log file, at https://github.com/docker/for-win/issues
[12:47:03.480][DockerService ][Info ] Starting service
[12:47:03.559][BackendServer ][Info ] Started
[13:00:46.228][NamedPipeServer][Info ] Version()
[13:00:46.228][NamedPipeServer][Info ] Version done in 00:00:00.
[13:00:46.313][NamedPipeServer][Info ] Start(Docker.Core.Settings)
[13:00:46.313][PowerMode ][Info ] Stop
[13:00:46.328][HyperV ][Info ] Stop
[13:00:46.328][PowerShell ][Info ] Run script with parameters: -Stop True…
[13:00:46.347][PowerShell ][Info ] Creating a Runspace Pool…
[13:00:46.651][PowerShell ][Info ] Runspace Pool created (Min=1, Max=2)
[13:00:46.713][HyperV ][Info ] Script started at 13:00:46.698
[13:00:47.730][HyperV ][Info ] Module loaded at 13:00:47.730
[13:00:47.930][HyperV ][Info ] VM MobyLinuxVM is stopped
[13:00:47.946][HyperV ][Debug ] [stop] took 00:00:01.6182297 to run
[13:00:47.946][OptimizeDisk ][Info ] Optimize
[13:00:47.947][PowerShell ][Info ] Run script…
[13:00:49.232][Moby ][Info ] Stop
[13:00:49.232][HyperVGuids ][Info ] Installing GUIDs…
[13:00:49.232][PowerMode ][Info ] Start
[13:00:49.232][HyperV ][Info ] Create
[13:00:49.232][PowerShell ][Info ] Run script with parameters: -Create True -VhdPathOverride -SwitchSubnetAddress 10.0.75.0 -SwitchSubnetMaskSize 24 -CPUs 2 -Memory 2048 -IsoFile C:\Program Files\Docker\Docker\Resources\mobylinux.iso…
[13:00:49.232][HyperVGuids ][Info ] GUIDs installed
[13:00:49.249][HyperV ][Info ] Script started at 13:00:49.249
[13:00:49.249][Firewall ][Info ] Removing all existing rules…
[13:00:49.254][HyperV ][Info ] Module loaded at 13:00:49.254
[13:00:49.453][Firewall ][Info ] Removing DockerSmbMount…
[13:00:49.555][HyperV ][Info ] Using existing Switch: DockerNAT
[13:00:49.617][Firewall ][Info ] Removed DockerSmbMount.
[13:00:49.787][Firewall ][Info ] Removing DockerProxy…
[13:00:49.856][HyperV ][Info ] Using existing Switch IP address
[13:00:49.971][Firewall ][Info ] Removed DockerProxy.
[13:00:50.134][Firewall ][Info ] All existing rules are removed.
[13:00:50.134][Firewall ][Info ] Opening ports for C:\Program Files\Docker\Docker\Resources\com.docker.proxy.exe…
[13:00:50.504][Firewall ][Info ] Opening ports for SMB…
[13:00:50.890][Firewall ][Info ] Ports are opened
[13:00:51.058][HyperV ][Info ] Setting CPUs to 2 and Memory to 2048 MB
[13:00:51.205][HyperV ][Info ] Connect Internal Switch DockerNAT
[13:00:51.459][HyperV ][Info ] VM created

My current very dirty workaround is to auto login the user and run a batch file via autostart that starts the containers and immediately locks the workstation. This can’t be the only way!

ping @friism … any info about this issue?

This may be an option too: http://www.techrepublic.com/blog/data-center/configure-a-hyper-v-vm-for-automatic-startup/
I didn’t test it, but I still think this is a MS hyper-v issue.

Thanks for your suggestion but…
a fresh virtual machine is created / destroyed each time docker starts / stops. So this is not an option.

Yea, but imo it should be.

Hey guys

Docker for Windows running Linux containers is currently optimized to be a developer tool. It sounds like you’re trying to use it for running apps as services, which is not currently a design use-case.

Please correct me if I misunderstood

Michael

So is it not possible to run apps as services with docker for windows?
Is it really just a developer tool and I MUST use linux server to run apps / containers for production?

You can get Docker to run before user login if you add a Scheduled Task. (This has some security implications if you save a local user’s password.) Hackishly:

  • Create a local account for docker (e.g. run -> “control userpasswords2”)
  • Create a scheduled task that runs as your new user
  • Set it to run “C:\Program Files\Docker\Docker\Docker for Windows.exe”
  • Set the task to run 1 minute after logon, and wait for a network connection
1 Like

.1. You can use docker-machine to create new virtual machine in hyper-v with docker engine onboard.
.2. Or you can create new virtual machine in hyper-v manually, ssh there and install docker engine there, run containers inside this new virtual machine.

1 and 2 are actually, the same, but 1 is mach easier.

1 Like

is it possible or not to run apps as window services with docker for windows ? did you get the answer of that question if yes please share the answer here
THANKS

In my case that is the exact goal; we are an all windows /.net shop and want to use a selenium grid on docker, and we may move mailslurper onto docker as well.

Running a non-windows machine (or VM) that then run docker means that that machine needs to be maintained (unlike the docker containers that we realize are running other operating systems, but we toss away on a daily basis). This creates a lot of “first” work that needs to be done, like how to manage accounts, patching, virus scanners, backups, etc.

I had the same issue with Win10 pro running in a VM and Docker 18.06.0-ce.

My solution was to start the service delayed.
-> Services -> Docker for Windows Services -> properties -> Autostart (delayed)

I guess it needs the Hyper-V started before…

Since I went through quite a lot of pain in order to make this work, here is a solution that worked for me for running a linux container using docker desktop on a windows 10 VM.

First, read this page to understand a method for running a python script as a windows service.

Then run your container using powershell and give it a name e.g

docker run --name app your_container

In the script you run as a service, e.g the main method of your winservice class, use subprocess.call(['powershell.exe', 'path/to/docker desktop.exe]) to start docker desktop in the service. Then wait for docker to start. I did this by using the docker SDK:

client = docker.from_env()
started = False
while not started:
    try:
        info = client.info()
        started = True
    except:
        time.sleep(1)

When client has started, run your app with subprocess again

subprocess.call(['powershell.exe', 'docker start -interactive app'])

Finally ssh into your container to keep the service and container alive

subprocess.check_call(['powershell.exe', 'docker exec -ti app /bin/bash'])

Install the service using python service.py install

Now you need to create a service account on the VM that has local admin rights. When you’ve done that, go to Services in windows, and find your service in the list of services. Right click -> properties -> Log On and enter the service account details under “This account”. Finally, under general, select automatic(delayed) start and start the service.

Probably not the most ‘by the book’ method, but it worked for my usecase

Thanks Nick, using a scheduled task works fine

I was able to run Docker before login using methods above: 1) Creating a scheduled task, 2) Using python to create the service that calls the Docker Desktop executable and waits for it to open, 3) Use NSSM ( https://nssm.cc/ ) to create a service that runs a batch file (and runs the same python code as in #2).

The problem is that I need to give my windows user credentials to the service in order for it to work. If I try to use the Local System account, it doesn’t work. I even tried adding the system account to the docker-users group and it didn’t work. We would like our software to come up after boot and before login and not require the user to update their password in the service every time they change their password. Is there a way to do this?

Edit: I was able to create a scheduled task that uses my account but does not save a password. This is less preferable to a service but may fit our needs for now.

2 Likes

Thanks for the response Michael. Is it still the case in 2020 that docker containers can’t be run on windows without logging in? Like the author of this post, I have a windows machine that I use as a server, and I’m deploying docker containers on it (graylog/mongodb/elasticsearch to start). I’d like for these containers to run whenever the machine is up, without requiring a user to login. Is that possible or on the roadmap?

Thank you for pointers to any resources that are available.