Docker is working with Microsoft and the community to build container images based on both Windows Server Core and Nano Server. Golang, Python and Mongo are available as official Docker images (more are on their way), and Microsoft also maintains a set of very popular sample images.
-->Deploying a Windows container host has different steps depending on the operating system and the host system type (physical or virtual). This document details deploying a Windows container host to either Windows Server 2016 or Windows Server Core 2016 on a physical or virtual system.
Windows Server Core Docker Free
- This document details deploying a Windows container host to either Windows Server 2016 or Windows Server Core 2016 on a physical or virtual system. Install Docker Docker is required in order to work with Windows containers.
- Docker build is the Docker engine command that consumes a Dockerfile and triggers the image creation process. This topic will show you how to use Dockerfiles with Windows containers, understand their basic syntax, and what the most common Dockerfile instructions are.
Install Docker
Docker is required in order to work with Windows containers. Install xcode 11 on mojave. Docker consists of the Docker Engine and the Docker client.
To install Docker, we'll use the OneGet provider PowerShell module. The provider will enable the containers feature on your machine and install Docker, which will require a reboot.
Open an elevated PowerShell session and run the following cmdlets.
Install the OneGet PowerShell module.
Use OneGet to install the latest version of Docker.
When the installation is complete, reboot the computer.
Install a specific version of Docker
There are currently two channels available for Docker EE for Windows Server:
17.06
- Use this version if you're using Docker Enterprise Edition (Docker Engine, UCP, DTR).17.06
is the default.18.03
- Use this version if you're running Docker EE Engine alone.
To install a specific version, use the RequiredVersion
flag:
Installing specific Docker EE versions may require an update to previously installed DockerMsftProvider modules. To Update:
Windows Server Core Docker Rdp
Update Docker
If you need to update Docker EE Engine from an earlier channel to a later channel, use both the -Update
and -RequiredVersion
flags:
Install base container images
Before working with Windows containers, a base image needs to be installed. Base images are available with either Windows Server Core or Nano Server as the container operating system. For detailed information on Docker container images, see Build your own images on docker.com.
Tip
With effect from May 2018, delivering a consistent and trustworthy acquisition experience, almost all of the Microsoft-sourced container images are served from the Microsoft Container Registry, mcr.microsoft.com, while maintaining the current discovery process via Docker Hub.
Windows Server 2019 and newer
To install the 'Windows Server Core' base image run the following:
To install the 'Nano Server' base image run the following:
Windows Server 2016 (versions 1607-1803)
To install the Windows Server Core base image run the following:
To install the Nano Server base image run the following:
Please read the Windows containers OS image EULA, which can be found here – EULA.
Hyper-V isolation host
You must have the Hyper-V role to run Hyper-V isolation. If the Windows container host is itself a Hyper-V virtual machine, nested virtualization will need to be enabled before installing the Hyper-V role. For more information on nested virtualization, see Nested Virtualization.
Nested virtualization
The following script will configure nested virtualization for the container host. This script is run on the parent Hyper-V machine. Ensure that the container host virtual machine is turned off when running this script.
Enable the Hyper-V role
To enable the Hyper-V feature using PowerShell, run the following cmdlet in an elevated PowerShell session.
-->The Docker engine includes tools that automate container image creation. While you can create container images manually by running the docker commit
command, adopting an automated image creation process has many benefits, including:
- Storing container images as code.
- Rapid and precise recreation of container images for maintenance and upgrade purposes.
- Continuous integration between container images and the development cycle.
The Docker components that drive this automation are the Dockerfile, and the docker build
command.
The Dockerfile is a text file that contains the instructions needed to create a new container image. These instructions include identification of an existing image to be used as a base, commands to be run during the image creation process, and a command that will run when new instances of the container image are deployed.
Docker build is the Docker engine command that consumes a Dockerfile and triggers the image creation process.
This topic will show you how to use Dockerfiles with Windows containers, understand their basic syntax, and what the most common Dockerfile instructions are.
This document will discuss the concept of container images and container image layers. If you want to learn more about images and image layering, see container base images.
For a complete look at Dockerfiles, see the Dockerfile reference.
Basic Syntax
In its most basic form, a Dockerfile can be very simple. The following example creates a new image, which includes IIS, and a ‘hello world’ site. This example includes comments (indicated with a #
), that explain each step. Subsequent sections of this article will go into more detail on Dockerfile syntax rules, and Dockerfile instructions.
Note
A Dockerfile must be created with no extension. To do this in Windows, create the file with your editor of choice, then save it with the notation 'Dockerfile' (including the quotes).
For additional examples of Dockerfiles for Windows, see the Dockerfile for Windows repository.
Instructions
Dockerfile instructions provide the Docker Engine the instructions it needs to create a container image. These instructions are performed one-by-one and in order. The following examples are the most commonly used instructions in Dockerfiles. For a complete list of Dockerfile instructions, see the Dockerfile reference.
FROM
The FROM
instruction sets the container image that will be used during the new image creation process. For instance, when using the instruction FROM mcr.microsoft.com/windows/servercore
, the resulting image is derived from, and has a dependency on, the Windows Server Core base OS image. If the specified image is not present on the system where the Docker build process is being run, the Docker engine will attempt to download the image from a public or private image registry.
The FROM instruction's format goes like this:
Here's an example of the FROM command:
To download the ltsc2019 version windows server core from the Microsoft Container Registry (MCR):
For more detailed information, see the FROM reference.
RUN
The RUN
instruction specifies commands to be run, and captured into the new container image. These commands can include items such as installing software, creating files and directories, and creating environment configuration.
The RUN instruction goes like this:
The difference between the exec and shell form is in how the RUN
instruction is executed. When using the exec form, the specified program is run explicitly.
Here's an example of the exec form:
The resulting image runs the powershell New-Item c:/test
command:
To contrast, the following example runs the same operation in shell form:
The resulting image has a run instruction of cmd /S /C powershell New-Item c:test
.
Considerations for using RUN with Windows
On Windows, when using the RUN
instruction with the exec format, backslashes must be escaped.
When the target program is a Windows installer, you'll need to extract the setup through the /x:<directory>
flag before you can launch the actual (silent) installation procedure. You must also wait for the command to exit before you do anything else. Otherwise, the process will end prematurely without installing anything. For details, please consult the example below.
Examples of using RUN with Windows
The following example Dockerfile uses DISM to install IIS in the container image:
This example installs the Visual Studio redistributable package. Start-Process
and the -Wait
parameter are used to run the installer. This ensures that the installation completes before moving on to the next instruction in the Dockerfile.
For detailed information on the RUN instruction, see the RUN reference.
COPY
The COPY
instruction copies files and directories to the container's file system. The files and directories must be in a path relative to the Dockerfile.
The COPY
instruction's format goes like this:
If either source or destination includes white space, enclose the path in square brackets and double quotes, as shown in the following example:
Considerations for using COPY with Windows
On Windows, the destination format must use forward slashes. For example, these are valid COPY
instructions:
Meanwhile, the following format with backslashes won't work:
Examples of using COPY with Windows
The following example adds the contents of the source directory to a directory named sqllite
in the container image:
The following example will add all files that begin with config to the c:temp
directory of the container image:
For more detailed information about the COPY
instruction, see the COPY reference.
ADD
The ADD instruction is like the COPY instruction, but with even more capabilities. In addition to copying files from the host into the container image, the ADD
instruction can also copy files from a remote location with a URL specification.
The ADD
instruction's format goes like this:
If either the source or destination include white space, enclose the path in square brackets and double quotes:
Considerations for running ADD with Windows
On Windows, the destination format must use forward slashes. For example, these are valid ADD
instructions:
Meanwhile, the following format with backslashes won't work:
Additionally, on Linux the ADD
instruction will expand compressed packages on copy. This functionality is not available in Windows.
Examples of using ADD with Windows
The following example adds the contents of the source directory to a directory named sqllite
in the container image:
The following example will add all files that begin with 'config' to the c:temp
directory of the container image.
The following example will download Python for Windows into the c:temp
directory of the container image.
For more detailed information about the ADD
instruction, see the ADD reference.
WORKDIR
The WORKDIR
instruction sets a working directory for other Dockerfile instructions, such as RUN
, CMD
, and also the working directory for running instances of the container image.
The WORKDIR
instruction's format goes like this:
Considerations for using WORKDIR with Windows
On Windows, if the working directory includes a backslash, it must be escaped.
Examples
For detailed information on the WORKDIR
instruction, see the WORKDIR reference.
CMD
The CMD
instruction sets the default command to be run when deploying an instance of the container image. For instance, if the container will be hosting an NGINX web server, the CMD
might include instructions to start the web server with a command like nginx.exe
. If multiple CMD
instructions are specified in a Dockerfile, only the last is evaluated.
The CMD
instruction's format goes like this:
Considerations for using CMD with Windows
On Windows, file paths specified in the CMD
instruction must use forward slashes or have escaped backslashes . The following are valid
CMD
instructions:
However, the following format without the proper slashes will not work:
For more detailed information about the CMD
instruction, see the CMD reference.
Escape character
In many cases a Dockerfile instruction will need to span multiple lines. To do this, you can use an escape character. The default Dockerfile escape character is a backslash . However, because the backslash is also a file path separator in Windows, using it to span multiple lines can cause problems. To get around this, you can use a parser directive to change the default escape character. For more information about parser directives, see Parser directives.
The following example shows a single RUN instruction that spans multiple lines using the default escape character:
To modify the escape character, place an escape parser directive on the very first line of the Dockerfile. This can be seen in the following example.
Windows Server Core Docker Licensing
Note
Only two values can be used as escape characters: and
`
.
For more information about the escape parser directive, see Escape parser directive.
PowerShell in Dockerfile
PowerShell cmdlets
PowerShell cmdlets can be run in a Dockerfile with the RUN
operation.
REST calls
PowerShell's Invoke-WebRequest
cmdlet can be useful when gathering information or files from a web service. For instance, if you build an image that includes Python, you can set $ProgressPreference
to SilentlyContinue
to achieve faster downloads, as shown in the following example.
Another option for using PowerShell to download files during the image creation process is to use the .NET WebClient library. This can increase download performance. The following example downloads the Python software, using the WebClient library.
Note
Nano Server does not currently support WebClient.
PowerShell scripts
In some cases, it may be helpful to copy a script into the containers you use during the image creation process, then run the script from within the container.
Windows Server Core Dockerfile
Note
This will limit any image layer caching and decrease the Dockerfile's readability.
This example copies a script from the build machine into the container using the ADD
instruction. This script is then run using the RUN instruction.
Docker build
Once a Dockerfile has been created and saved to disk, you can run docker build
to create the new image. The docker build
command takes several optional parameters and a path to the Dockerfile. For complete documentation on Docker Build, including a list of all build options, see the build reference.
The format of the docker build
command goes like this:
For example, the following command will create an image named 'iis.'
Windows Server Core Docker Rdp
When the build process has been initiated, the output will indicate status and return any thrown errors.
Windows Server Core Docker Linux
The result is a new container image, which in this example is named 'iis.'