Windows: How to start docker VM att system boot

build-serverdockerteamcitywindowswindows-service

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.

Best Answer

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