跳至主要內容
  • 簡單快速地下訂單
  • 檢視訂單及追蹤商品運送狀態
  • 建立並存取您的產品清單

Docker Containers on Windows Server 2022 101

摘要: This article is an introduction to what are containers, how to create and run containers in Windows Server, and what can containers be used for. A basic understanding of virtualization technologies and the Hyper-V hypervisor is a prerequisite. The included step-by-step demonstration and video can help with creating a lab environment. ...

本文章適用於 本文章不適用於 本文無關於任何特定產品。 本文未識別所有產品版本。

說明



What is a Container?

A container is an isolated environment where an application can run.
The container, the application and all its dependencies are constructed with pieces of the host's resources such as a kernel operating system, CPU, and memory and then logically separated from other containers and from the host itself.

The diagram below illustrates how a red and yellow container co-exists within the same host, using same hardware and kernel operating system but are contained within their individual space which allows them to run their apps without external interference. 

A red and yellow container co-exists within the same host

Note how the same hardware and the operating system installed on that hardware can run its own apps and services also independent from what happens on each container.

How to create and run containers in Windows Server

Containers can run on various platforms such as Linux, Windows, and Mac operating systems. This article/demo, however, focuses on running containers in Windows Server.

There are multiple ways to work with Windows containers, including Windows Admin Center (WAC) or Visual Studio. In this article/demo, however, Docker is used.

Docker is a platform-as-a-service product that can be used to combine an application, all its dependencies and configuration information into a single package called a container. Docker can then be used to ship, run, or delete that container.

The subsequent steps were performed on a fresh installation of Windows Server 2022.
You can find a list of prerequisites for running containers on Windows Server in the external link below:

Get started: Prep Windows for containers Third party link icon



The steps to create and run containers on Windows Server using Docker can be summarized as follows:

1. Install Docker

In PowerShell (run as Administrator) enter: 

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force

At the prompt, enter "Y" to confirm the installation of NuGet.

In the same PowerShell session, enter:

Install-Package -Name docker -ProviderName DockerMsftProvider

At the prompt, enter "Y" to trust the package.

In the same PowerShell session, enter:

Restart-Computer

Installing Docker

2. Create a Docker file and download a container image

On an elevated PowerShell session, switch to the root directory and create a new folder by entering:
cd\
mkdir Containers
cd Containers

In the same PowerShell session, create the docker file and edit it by entering:
New-Item dockerfile 

(Notice there is no file extension)
notepad dockerfile

In the notepad text editor, enter the following 3 lines and then save and close the dockerfile:
FROM mcr.microsoft.com/windows/servercore/iis
RUN powershell
COPY index.html C:/inetpub/wwwroot


Creating the docker file

Return to Powershell, still in the C:\Containers context and create a n html file and edit it by entering the following:

New-Item index.html

notepad index.html

In the notepad text editor, enter the following three lines and then save and close the index.html file:
<h1>Hello World!</h1>
<p>This is an example of a simple HTML page hosted on:</p>
<h2>container #1</h2>

Sample HTML app

3. Build and run the container.

Back in PowerShell, still in the C:\Containers, type:

docker build -t webserver .

Downloading the container image

Wait for the image to download from the Microsoft Container Registry then type:

docker images 

You should now see the downloaded image.

To finally run a container using the downloaded image template, back in powershell type:

docker run --name container1 -d -p 80:80 webserver

This last line creates and runs a container named container1 based on the webserver image that had been prepared. The -d switch instructs Windows to run the container in the background and -p tells windows to forward port 80 (HTTP) on the host to port 80 on the container.

To test the container:
Open a web browser on the host and browse to localhost (you can also use a different computer on the same network segment and browse to the container's host IP address or computer name).

You should press the sample HTML web page created earlier:

Webpage hosted on container

This completes the tutorial on how to run Docker containers on Windows Server 2022.
 

受影響的產品

Microsoft Windows Server 2016

產品

Microsoft Windows Server 2016, Microsoft Windows Server 2019, Microsoft Windows Server 2022
文章屬性
文章編號: 000201261
文章類型: How To
上次修改時間: 08 1月 2024
版本:  7
向其他 Dell 使用者尋求您問題的答案
支援服務
檢查您的裝置是否在支援服務的涵蓋範圍內。