GP - Fotolia

Tip

How to save and reuse a Windows container image

With five simple steps, you can create a Windows container, save it as an image and reuse it to create new containers with the same configuration and settings.

Windows containers are lightweight and easy to deploy. Considering the nature of Windows containers, you might want to deploy multiple containers running the same set of components or applications. The best way to do this is to save a Windows container image and spin up another container using that image.

Before familiarizing yourself with the process of saving and reusing a Windows container image, you should figure out when it makes sense to do so. Containers are designed to be disposable, meaning you can quickly create a container to run a specific task and kill it once that task is complete. Because containers don't have a data drive attached to them and are lightweight in design, it would make sense to use the save-and-reuse process for stateless applications, such as web servers and applications that don't require data to be stored in memory.

Reusing a Windows container image also makes sense when you must deploy a service quickly, because reusing the image doesn't require you to create the container from scratch first, which saves on time.

Steps for creating a container and saving an image

First, you must create a Windows container, install Internet Information Services (IIS) in the container and save the image. You'll deploy the container with an IIS web server instance.

Step 1: Create a Windows Container by running the command below:

Docker run –it –name winserver16 –p 80:80 –p 443:443 –v C:\MyData\docker\volumes\data1:E:\ Microsoft/windowsservercore cmd

The command above creates a container by the name of winserver16 and saves the image for the container in the E:\ directory.

Step 2: Inside the container, open a PowerShell window, and install an IIS web server instance. Run the following PowerShell command to install an IIS web server:

Add-WindowsFeature Web-Server

Once the IIS web server installs, exit and stop the container. This is required to make sure the changes are committed to the image. To exit the container, use the Ctrl+P+Q shortcut, and to stop the container, issue the command below:

Docker stop winserver16

Step 3: Next, get the container ID by running the following command:

Docker ps –a

Make a note of the container ID, as you'll be required to use it in the next step.

Step 4: Commit the changes to the container, and create an image. The command below creates a Windows container image by the name of image.v3 and stores it in the container image library:

Docker commit e747474cef4fa windowsiis/image.v3

If you run the docker images command, you'll be able to see all of the container images, along with the one you just created by using the docker commit command.

Step 5: Once you see your image in the list, you can start a new container with the same image by using the command below:

docker run -it newimages/myimage.v4 cmd

If your container starts with the new Windows container image without any issues, the image was saved successfully and can be reused multiple times when creating new containers.

Dig Deeper on Containers and virtualization