Windows: How to start docker VM att system boot

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


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.


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.