Docker Run Windows Image

  



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. Docker run: The `docker run` command first `creates` a writeable container layer over the specified image, and then `starts` it using the specified command. That is, `docker run` is equivalent.

Docker Desktop is an application for MacOS and Windows machines for the building and sharing of containerized applications and microservices. Docker Desktop delivers the speed, choice and security you need for designing and delivering containerized applications on your desktop. For developers, Windows 10 is a great place to run Docker Windows containers and containerization support was added to the the Windows 10 kernel with the Anniversary Update (note that container images can only be based on Windows Server Core and Nanoserver, not Windows 10). All that’s missing is the Windows-native Docker Engine and some image. Then you can run in detached mode so your terminal is still usable. You have several options to run it using a repository name (with or without a tag) or image ID: docker run -d repository docker run -d repository:tag docker run -d imageid Then you can check your container is running using. Docker ps docker ps gives you a container ID.

Key Features and Capabilities

The fastest way to design and deliver containerized applications and microservices on the desktop and cloud.

Simple Setup for Docker and Kubernetes

No need to fiddle with VMs or add a bunch of extra components; simply install from a single package and have your first containers running in minutes. You get certified Kubernetes and Docker, for developers of all levels of container expertise.

Certified Kubernetes

Setup a fully functional Kubernetes environment on your desktop with a single click and start developing and testing modern applications in minutes.

Application Templates and App Designer

Customize and share multi-service applications and service templates that are tailored to your organization. Pre-defined and customizable application templates adhere to corporate standards and automate configuration, eliminating error-prone manual setup. Intuitive Application Designer facilitates the packaging, installing, and managing of multi-service applications as a shareable package.

-->

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:

Windows
  • 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.

Docker run windows imagenes

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:

Run Windows On Mac

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.

Docker Run Windows 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:

Docker Run Windows Image Software

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.

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

Docker For Windows Run Image

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.

Docker Run Windows Image On Linux

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.'

When the build process has been initiated, the output will indicate status and return any thrown errors.

The result is a new container image, which in this example is named 'iis.'

Further reading and references