8

I'm in a situation where I need to run docker on a windows based build server (I'm normally working with Arch/Debian Linux) and I can't find a way to have the docker VM to start automatically on system boot. On Linux it would be a service but on windows that does not seem to be the case.

I followed the instructions on https://docs.docker.com/engine/installation/windows/ and everything runs when I start the docker VM with $ docker-machine create but how to have this as a Windows service or similar?

The goal is to be able to use build Docker images, start as containers and run e2e tests against them. Since we are using TeamCity for building any ideas about solving this with TeamCity would also work I guess.

tirithen
  • 185
  • 1
  • 1
  • 7

4 Answers4

7

I wrote a small webapp some time ago which has a system dependency which is only available for unix systems so docker was the natural choice (although i had quite some success with vagrant ..)

I am using windows for development and also running this webapp in its docker container.

So I came across the same issue, "how to start this at system boot"?

I ended up with a small batch file in my autostart directory containing something like the following lines:

docker-machine start default
docker run -d -p 8080:8080 -v //c/Users/%USERNAME%/somepath:/c/Users/%USERNAME%/somepath my/image --some.webapp.param=some-webapp-param-value

The first call starts the "default" virtual machine, although you should be able to specify a different vm there. The second call runs the "my/image" docker container, exposing port 8080 to the host system and mounting "somepath" from my user's home directory. Be sure to specify the "-d" parameter as it runs the container "in the background" (check "docker run --help")

After that my webapp can be reached at the ip address of the "default VM" (in my case that was 192.168.99.100, this may differ on your system, you can check the output when first starting this "docker quickstart terminal" as the ip address will be logged to the console.)

With that i am able to use my browser as usual, point it to "http://192.168.99.100:8080" and can work with my webapp.

Hope that helps =)


edit

with said batch file now at hand, one has 3 options:

  • put the batch file in your autostart directory (requires interactive user sessions, so likely not suitable for server instances..)

  • schedule the execution of the batch file via windows' task scheduler. there you can set "run at computer startup" as a trigger for the task. the task is run regardless of an interactive session (a logged-in user..) being available or not

  • use a service wrapper like NSSM to install the batch file as a windows service

salgmachine
  • 186
  • 1
  • 4
  • Thanks for the answer, :) but what is this autostart directory that you are speaking of, is that the one in the start menu that is only started when that specific user logs in to that machine? In that case it's not really useful on a server since there is no one logging in there until some maintenance is needed. That kind of auto start functionality is meant for user applications I guess. Or did you find some other directory that actually is considered when the machine is booting up? It's weird that the docker team does not provide a service for this, I guess they like me prefer unix systems. – tirithen May 20 '16 at 09:42
  • @tirithen see my updated answer ;) – salgmachine May 22 '16 at 19:32
5

Just a small addon: When you start your docker images and want them to start at boot of docker (in this case the virtualmachine running docker) you can use the --restart always option in the docker run command. Then you can remove the docker run command from your batch files. The container should start automatically after the vm started.

PWFraley
  • 151
  • 1
  • 2
3

I have extended Charlie Carver's answer.

With following script you can specify which boot2docker machine launch. Since Docker Toolbox folder may not be in your path and all the proper environmental variables may not be set, I wrote some initialization instructions.

Put this script in a bat file and use one of the options cited by Charlie to make it run at boot time.

@echo off

REM Set the name of the VM configuration where dockerd will be hosted
set BOOT2DOCKER_VM=default

set PATH=%PATH%;"C:\Program Files\Docker Toolbox\"

REM Start the default machine (or any other machine)
docker-machine start %BOOT2DOCKER_VM%

REM Set the environment variables to use docker-machine and docker commands
@FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd %BOOT2DOCKER_VM%') DO @%i

REM Start the image detached
docker run --detach --publish 5000:5000 foo-service

If you care that in case of a fatal error inside the container, please use --restart option on the docker run command.

1

You could also use NSSM (Non-Sucking Service Manager)

Launch NSSM from a command promt or powershell windows with the install param (nssm.exe install)

This will launch GUI, eneter the following detials;

  • Path: [Docker Install DIR]\Docker\resources\bin\docker-compose.exe
  • Startup Directory: [Path to your docker container]
  • Arguments: any arguments you need for your docker container (i.e up)

Finally enter your service name in the box at the bottom of the GUI, you can then feel free to test with the other options on the additional tabs.