Keep Docker Containers Running on Windows Server 2016: A Complete Guide (2026)

Learn how to keep Docker containers running on Windows Server 2016 without needing to log in. Discover how to use NSSM to run containers as services.

Keep Docker Containers Running on Windows Server 2016: A Complete Guide (2026)

Keep Docker Containers Running on Windows Server 2016: A Complete Guide (2026)

Running Docker containers on Windows Server 2016 can present some challenges, especially when it comes to keeping containers running without requiring a user to be logged in. This tutorial will guide you through setting up your Docker containers to run as services, ensuring they stay active even when you log out or restart your virtual machine. By the end of this guide, you'll know how to configure your containers to run independently of your login sessions.

Key Takeaways

  • Understand why Docker containers stop on logout and how to prevent it.
  • Learn to set up Docker containers as Windows services.
  • Discover methods to troubleshoot common issues with Docker on Windows Server.
  • Ensure your containers start automatically after a system reboot.

Docker is an essential tool for many developers, providing a consistent environment to develop and deploy applications. However, running Docker containers on a Windows Server, especially within a virtual machine, can be tricky. One common issue users face is that containers stop running when they log out of their Windows session. This can interrupt services and workflows and is not ideal for production environments.

In this guide, we’ll explore a solution to this problem by configuring Docker containers to run as services on Windows Server 2016. This approach ensures your containers continue to operate independently of user sessions, providing a more robust and reliable deployment environment.

Prerequisites

  • Windows Server 2016 with Docker installed.
  • Basic knowledge of Docker and Windows services.
  • Administrator access to the Windows Server.
  • Existing Docker containers you wish to keep running.

Step 1: Understand the Problem

Before diving into the solution, it's crucial to understand why containers stop. By default, Docker containers on Windows Server may rely on the session state, stopping when you log out. This is because the Docker daemon, which manages container lifecycles, might be tied to the user session under certain configurations.

Step 2: Configure Docker to Start on Boot

Ensure Docker is configured to start with the system. This is typically set up during the Docker installation, but verifying this setting is crucial.

# Check Docker service status
Get-Service -Name 'Docker' | Select-Object -Property Status, StartType

# Set Docker service to start automatically
Set-Service -Name 'Docker' -StartupType Automatic

This PowerShell script checks the Docker service status and configures it to start automatically if it isn't already.

Step 3: Use NSSM to Create a Service for Your Container

NSSM (Non-Sucking Service Manager) is a tool that allows you to create Windows services for any executable, including Docker commands. This step involves setting up your containers to run as services using NSSM.

  1. Download and install NSSM from the official website.

Start the service:

nssm start MyDockerService

Create a service using NSSM. This service will run your Docker container command:

nssm install MyDockerService
nssm set MyDockerService Application C:\Program Files\Docker\Docker\resources\bin\docker.exe
nssm set MyDockerService AppParameters "run --name my_nginx_container --restart=always nginx"

This setup allows your Docker container to run as a service, independent of the logged-in user session.

Step 4: Verify Service Configuration

Ensure the service is configured correctly and starts as expected. You can do this by checking the service's status and logs:

# Check service status
Get-Service -Name 'MyDockerService'

# Check logs for issues
nssm logs MyDockerService

Step 5: Test the Setup by Logging Out and Restarting

To ensure everything works as expected, log out of the Windows session and restart the virtual machine. Verify that the Docker container remains running without requiring a user to log in.

Common Errors/Troubleshooting

  • Service fails to start: Check the NSSM logs for errors and ensure all paths and parameters are correct.
  • Docker not starting on boot: Confirm Docker's service is set to automatic and there are no permission issues.
  • Container not running: Verify the Docker run command used in NSSM is correct and check for any syntax errors.

Conclusion

By following these steps, you can ensure that your Docker containers on Windows Server 2016 continue running even after logging out or rebooting the system. This approach provides a reliable way to manage containerized applications in a server environment, enhancing uptime and stability for your deployments.

Frequently Asked Questions

Why do Docker containers stop when I log out?

Docker containers may stop on logout if they're tied to the user session. Configuring them as services allows them to run independently.

What is NSSM?

NSSM (Non-Sucking Service Manager) is a tool that helps you run executables as Windows services, making it ideal for running Docker containers independently.

Can I use this method on Windows Server 2019?

Yes, the method is applicable to Windows Server 2019 and later versions, with minor adjustments if needed.