Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Windows how to keep container running without login?

I have Docker installed inside a Virtual Machine with Windows Server 2016.

I have a Linux Container from Python3 with NGINX server using --restart=always param, it runs fine while I am logged in, if I restart the VM, the container is no longer active, and it starts only if I log in.

Also if I logout, the container stops.

How can I make a container run as a service without login and keep it running on logout?

like image 309
Alex Lord Mordor Avatar asked Mar 05 '19 17:03

Alex Lord Mordor


2 Answers

I've got a better answer from HERE

The summary is to build a Task and assign it to Task Scheduler to run on Windows start.

All the scripts should be run on powershell

  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.

  1. 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

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] { }
  1. 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.

  1. 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\
like image 142
mammadalius Avatar answered Dec 27 '22 19:12

mammadalius


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'])

Now install the service using python service.py install

Now you need to create a service account on the VM that has local admin rights. 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 me.

like image 44
Beinhardt Avatar answered Dec 27 '22 18:12

Beinhardt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!