-
If necessary, download and install Docker. Create the container that you wish to run as a service.
For this tutorial, we've:
As you can see here, we named our container "Webserver":
-
If necessary, install Docker daemon as a Windows Service.
Your container needs the Docker daemon to do its work.
The service (named "Docker Engine") was installed on our computer:
-
Create a batch file that runs your Docker container.
The file should contain two commands — one to start the container and another to block until the container closes.
Here's what our batch file looks like:
"C:\Program Files\Docker\Docker\resources\bin\docker.exe" start Webserver
"C:\Program Files\Docker\Docker\resources\bin\docker.exe" wait Webserver
Note that your path to docker.exe (and your container name) may be different. Be sure to update accordingly!
At this point, please confirm that your batch file starts the container.
-
Create a batch file that gracefully stops your container.
Our batch file contains a single command:
"C:\Program Files\Docker\Docker\resources\bin\docker.exe" stop Webserver
Please confirm that your batch file stops the container.
-
Next, download and install AlwaysUp, if necessary.
-
Start AlwaysUp.
-
Select Application > Add to open the Add Application window:
-
On the General tab:
-
In the Application field, enter the full path to the batch file you created in step 3 (to launch the container).
AlwaysUp will run the batch file to start the Docker container as a Windows Service.
-
In the Start the application field, choose Automatically, but shortly after the computer boots.
With that setting, Docker will start a couple of minutes after boot — once all the machine's critical services are
fully up and running.
-
And in the Name field, enter the name that you will call the application in AlwaysUp.
We have used NGINX Docker Container but you can specify almost anything you like.
-
Click over to the Logon tab and enter the user name and password of the Windows account where you installed and run Docker.
It's best to run Docker in that account, where you know it works.
-
Click over to the Startup tab.
Enable the Ensure that the following services have started option and check the Docker Engine entry.
This informs AlwaysUp that your container needs the Docker daemon to operate.
-
Switch to the Extras tab, where we'll ensure that the container exits gracefully whenever the service stops.
-
Check the Use this special command to stop the application box.
-
Enter the full path to the batch file you created in step 4 (to stop the container).
-
Check the Wait for up to box and enter 30 seconds. That should be enough time for your container to close.
-
We're done configuring the container to run as a service with AlwaysUp. Click the Save button to record your settings.
In a couple of seconds, a new entry will show up in the AlwaysUp window:
-
To start your container, choose Application > Start.
The state should transition to "Running" after a few seconds:
At this point, you should confirm that your container is up and running as expected.
For example, run "docker container ls --all" to check the status. Our container was up:
And because our container is running NGINX, we were also able to validate by browsing to the web server's URL:
-
That's it! Next time your computer boots, your Docker container will start immediately, before anyone logs on.
We encourage you to edit the Docker entry in AlwaysUp and check out the many other settings that may be appropriate for your environment.
For example, send an email if Docker stops, run a customized failure detection script, and much more.