We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Using Docker and Docker Compose on Windows Server 2022

Learn how to – Using Docker and Docker Compose on Windows Server 2022.

Containers have been around for quite some time now and have become an increasingly popular way to deploy and manage applications. They allow developers to package up an application and its dependencies into a single portable image that can run consistently across different environments, from development to production.

Initially, containers were primarily associated with the Linux operating system, but support for Windows containers has steadily grown in recent years. With the release of Windows Server 2016, Microsoft introduced support for Docker containers running on Windows, allowing developers to take advantage of the benefits of containerization on the Windows platform. In addition to Docker support, Microsoft also offers its own container runtime for Windows called Windows Container (previously known as Windows Server Containers). Windows Containers support both traditional Windows applications and modern applications based on .NET Core.

Microsoft has also embraced Kubernetes, an open-source container orchestration system, and offers support for running Kubernetes on Windows Server. This allows developers to deploy and manage containers using Kubernetes on the Windows platform, providing a consistent way to manage both Linux and Windows workloads.

Today, we will learn how to use Docker and Docker Compose on Windows Server 2022.

Docker is an open-source platform for building, shipping, and running container applications. It provides a way to create and manage containers, which are lightweight, standalone, and executable software packages that can run on any machine with Docker installed, regardless of the underlying operating system or hardware.

Docker is important because it helps to simplify the process of building, deploying, and scaling applications. With Docker, developers can package their applications in a consistent and portable way, making it easier to move them between development, testing, and production environments. Docker also allows for rapid deployment of applications, which can help to speed up the development cycle and improve time-to-market. It also isolates applications and their dependencies, which can help to improve security and reduce the risk of conflicts between different applications running on the same system.

Install Docker on Windows Server 2022

Before we use Docker, we need to have it installed on the Windows Server 2022 system. This can be done using two methods:

  • Using the GUI
  • Using PowerShell

Method 1: Install Docker on Windows Server 2022 using GUI

With this method, launch the server Manager and access the add roles and Features Wizard.

Select the role-based features then proceed and select your server as the destination.

On this page, just click next:

On the features tab, select the Containers feature.

Once the installation is complete, you will see this:

Now, access PowerShell with elevated privileges and install DockerMsftProvider and Dokcer using the commands

 Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
 Install-Package -Name docker -ProviderName DockerMsftProvider

Sample Output:

Once installed, restart the system. Once it restarts, the service will be available as shown:

Method 2: Install Docker on Windows Server 2022 using PowerShell

You can also install Docker entirely on Windows Server 2022 PowerShell. Launch PowerShell with admin rights and execute the below commands:

First, enable the containers feature:

Enable-WindowsOptionalFeature -Online -FeatureName Containers 

Now install docker:

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force 
Install-Package -Name docker -ProviderName DockerMsftProvider 

Sample Output:

Once installed, restart the system.

Restart-Computer -Force 

Once the system boots, verify your installation:

docker version 

Sample Output:

How to Use Docker on Windows Server 2022

Once the Docker Engine has been installed, you can now build/pull and use container images as desired. Below are some of the activities you can perform with Docker.

a. Pull Docker Images

To pull a docker image, use the command with the below syntax:

docker pull [image-name]

For example:

docker pull mcr.microsoft.com/windows/servercore:ltsc2022 

Once pulled, view the available images:

docker images

Sample output:

b. Build Docker Images

You can also build a Docker Image from a DockerFile. Create a Docker file with the required variables for your container.

Create a dedicated directory for the docker file.

mkdir docker-file 
cd docker-file 

Create a sample DockerFile for IIS web server.

$str_file = @"
FROM mcr.microsoft.com/windows/servercore:ltsc2022
MAINTAINER ownyourlife.com.ng

RUN dism.exe /online /enable-feature /all /featurename:iis-webserver /NoRestart
RUN echo Dockerfile test example > C:inetpubwwwrootindex.html

EXPOSE 80
CMD [ "cmd" ]
"@ 

Export these values to a file:

$str_file | Out-File Dockerfile -Encoding Default 

Now build a docker image:

docker build . -t iis-webserver:latest

Sample Output:

View the image:

c. Run Docker Containers

You can use the built/pulled images to spin a docker container. If the image has not been pulled, docker will find it and pull it before spinning the container.

For this demo, we will run the server core container from the pulled image, and echo some statements inside the container:

docker run mcr.microsoft.com/windows/servercore:ltsc2022 powershell -c "echo 'Voila! We have a Docker Container running on Windows server 2022!'"

Sample Output:

You can also launch an interactive session in the container:

docker run -it mcr.microsoft.com/windows/servercore:ltsc2022 powershell

Sample Output:

To exit the session use:

exit

d. Expose Docker Services

For the Docker services to be accessed from outside the container, they need to be exposed. When running a container, you need to use the -p flag to expose the container port.

For example, running an IIS webserver container(using the image we built earlier) and exposing the service:

docker run -t -d -p 8080:80 --name webserver iis-webserver cmd

Check the status of the container:

docker ps

Sample Output:

Add a test file:

 docker exec webserver powershell -c "Write-Output 'This is a test IIS container' | Out-File -Encoding default C:inetpubwwwrootindex.html"

This will run the container and expose port 80 to port 8080 on your local network. The service can then be accessed using the URL http://IP_Address:8080

e. Run Docker Container with Persistent Storage

To persist data in Docker containers, we use persistent storage. This can be a dedicated directory or a docker volume mapped to a given path.

The easiest way to create persistent storage is by creating a path. Say:

mkdir C:docker-container-storage 

Once the path has been created, we can test if we are able to write to it:

Write-Output 'Docker Persistent Storage Test' | Out-File -Encoding default C:docker-container-storageindex.html

Now we can spin a container that uses the created storage using the -v flag and map the paths:

docker run -it -v C:docker-container-storage:C:disk01 mcr.microsoft.com/windows/servercore:ltsc2022 

This will map the contents C:disk01 in the container to C:docker-container-storage on your host machine.

Install Docker Compose on Windows Server 2022

After installing the Docker Engine, you can now install Docker Compose. This is a tool that allows you to define and run multi-container Docker applications. It makes it possible to describe the services, networks, and volumes required to run a set of Docker containers as a single application.

To install Docker Compose, launch PowerShell with admin rights and execute the command. But first check releases page and replace v2.18.0 with the release number.

Invoke-WebRequest "https://github.com/docker/compose/releases/download/v2.18.0/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFilesdockerdocker-compose.exe 

Verify the installation:

docker-compose --version 

Sample Output:

You can now use Docker Compose to run a container. This involves creating a docker-compose YAML file for the container.

For example, export the variables:

$str_file = @"
version: '3'
services:
  web:
    image: iis-webserver
    container_name: iis
    ports:
      - "80:80"
"@ 

Put the variables in a file:

$str_file | Out-File docker-compose.yml -Encoding Default 

Now run the container with the command:

docker-compose up -d

View the status of the container:

docker-compose ps

Sample Output:

How to Manage Networks on Docker

Docker provides several networking options for containers to communicate with each other and with the outside world. Here are the basic steps to manage networks on Docker:

You can view the available networks:

docker network ls 

Get details of a given network. For example ‘nat‘

docker network inspect nat 

Sample Output:

‘nat‘ is the default network assigned to containers. You can create another network using the NAT driver with a command bearing the below syntax:

docker network create my-network -d "nat"

Sample Output:

Once the network has been created, you can connect a container to it. For example:

docker network connect [network name] [container name]

For example:

docker network connect my-network  webserver

To run a new container and connect to a network, use:

docker run --network my-network mcr.microsoft.com/windows/servercore:ltsc2022 powershell -c "ipconfig" 

Sample Output:

To delete a network, use:

docker network rm [network name] 

To remove all the networks with no containers attached:

docker network prune 

Verdict

Today we have learned how to use Docker and Docker Compose on Windows Server 2022. This can be used for rapid deployment of applications, which can help to speed up the development cycle and improve time-to-market. I hope this was informative.

See more:

Cart

Your Cart is Empty

Back To Shop