Containers have become a fundamental part of modern application development and deployment. Whether you’re looking to streamline development processes or run isolated applications efficiently, Docker containers offer the perfect solution. In this guide, we’ll explore how to run Docker containers on Windows Server 2022, an essential step for businesses and developers moving toward a containerized infrastructure.
What are Containers?
At their core, containers are isolated environments that bundle an application and all of its dependencies together. They share the host’s resources—such as the kernel, CPU, and memory—while remaining logically separated from the host system and other containers. This isolation ensures that applications can run independently without interference.
Unlike traditional virtual machines, which replicate an entire operating system, containers run on the host system’s OS, making them more lightweight and faster to spin up.
How Containers Work in Windows Server 2022
Running Docker containers on Windows Server 2022 leverages the Windows Hyper-V features. However, it’s important to note that Docker Desktop is not supported on Windows Server. Instead, containers are run directly using DockerMsftProvider—a specialized provider designed for Windows Server.
Before proceeding with containerization on Windows Server, make sure you have a basic understanding of virtualization technologies and the Hyper-V hypervisor.
Step-by-Step Guide to Running Docker Containers on Windows Server 2022
This guide walks through setting up and running Docker containers in Windows Server 2022, including creating a sample web server container.
1. Installing Docker on Windows Server
To install Docker on Windows Server 2022, you’ll need to run several PowerShell commands with administrator privileges:
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Confirm NuGet installation with ‘Y’.
Next, install Docker using the provider:
Install-Package -Name docker -ProviderName DockerMsftProvider
Once Docker is installed, restart your server:
Restart-Computer
Docker is now ready to be used on your Windows Server 2022!
2. Creating a Dockerfile and a Simple Web Application
Docker containers are built using Dockerfiles—scripts that define how the container is structured. Follow these steps to create a basic web server container:
cd\
mkdir Containers
cd Containers
Create a new Dockerfile and open it for editing,Remember that it has no extension:
New-Item dockerfile
notepad dockerfile
Add the following lines to the Dockerfile, which sets up a simple web server based on Windows Server Core with IIS:
FROM mcr.microsoft.com/windows/servercore/iis
RUN powershell
COPY index.html C:/inetpub/wwwroot
Next, create an HTML file that will be served by your web server:
New-Item index.html
notepad index.html
- New-Item index.html: This command creates a new file named
index.html
in the current directory. If the file already exists, it will display an error unless you add the-Force
flag to overwrite it. - notepad index.html: This command opens the
index.html
file in Notepad, a basic text editor in Windows. You can use this to edit or add content to the file directly.
Add the following HTML code to the file:
<h1>Hello World!</h1>
<p>This is an example of a simple HTML page hosted on:</p>
<h2>container #1</h2>
3. Building and Running the Container
Once the files are in place, it’s time to build and run the container:
Build the container from your Dockerfile:
docker build -t webserver .
Check that the container image has been created:
docker images
Run the container:
docker run --name container1 -d -p 80:80 webserver
The -d flag runs the container in detached mode, and the -p 80:80 flag maps port 80 from the host to the container, making it accessible via HTTP.
4. Testing the Container
To verify the container is running, open a web browser on the host system and navigate to http://localhost. You should see the simple HTML page you created.
Alternatively, if you’re using another machine on the same network, you can access the containerized web server using the host’s IP address or computer name.
What Containers Can Be Used For
Containers are widely used for a variety of purposes, including:
- Microservices: Running individual microservices in containers ensures each service is isolated and can be updated independently.
- Development and Testing: Developers can spin up entire environments quickly, allowing them to work in consistent setups across teams.
- Deployment: Containers allow easy, repeatable deployments in production environments.
Conclusion
Running Docker containers on Windows Server 2022 provides a powerful way to manage applications in an isolated, lightweight environment. This guide offers a basic introduction to container creation and deployment using Docker, showing how even complex applications can be packaged and run with ease.
Remember, Docker Desktop is not supported on Windows Server, so these steps rely on the server’s Docker engine instead. As you grow more familiar with containers, you can scale your setup to support more advanced use cases.
Start experimenting with containers today and experience the agility and flexibility they bring to application development and deployment.