Container image customization

Build and Customize Container Image in windows 2016

In this blog, we will show you how to Build and Customize Container Image in windows 2016 using docker commands.

OVERVIEW

When we planned to create our own customized image, there are two options available. Either manually or automatically. For manual process, launch a container, customize it and save it.

For automation, we need to use the docker file. It contains all the customize code that we need to build an image. It looks like a great way to store the container image as a code.

BUILDING CUSTOMIZE IMAGE MANUALLY

  • Create a new container using below command.

Example: docker run -it microsoft/nanoserver powershell

Build and Customize Container Image in windows 2016
  • Once you get inside the container, we are creating a test file for testing purpose. To create a file and add a value in it, execute the below command.

Example: New-Item -ItemType file -Name test.txt -Value assistanz

Build and Customize Container Image in windows 2016
  • Now disconnect the container by pressing CTRL+PQ keys.
  • To view running containers, type the below command.

docker ps

Build and Customize Container Image in windows 2016
  • To stop the container, execute the below command.

Example: docker stop 6d

Build and Customize Container Image in windows 2016
  • To build a customize image, type the below command.

docker commit aa my-nano-image

Build and Customize Container Image in windows 2016
  • Now you can launch the new container using this customize image.
Build and Customize Container Image in windows 2016
  • To view the history of the images, execute the below command.

Example: docker history my-nano-image

Build and Customize Container Image in windows 2016

In this, you can see one custom layer stacked on top of nano server base OS image.

PREPARING SCRIPTS FOR BUILDING CONTAINER IMAGE

  • Create a new folder named web under C: drive.
Build and Customize Container Image in windows 2016
  • Go-to web folder and create a new docker file using below command.

New-Item -ItemType file -Name dockerfile

Build and Customize Container Image in windows 2016

Note: The filename should not contain any extension

  • Open the file in the notepad and paste the below code.

# Sample Dockerfile to build a Windows Web Server

# Indicates that the windowsservercore image will be used as the base image
FROM microsoft/windowsservercore

#Metadata indicating an image maintainer
MAINTAINER @AssistanZ

# Uses PowerShell to install the web server role
RUN Powershell.exe -Command Install-WindowsFeature Web-Server

# Copies an HTML file to the web server root
COPY ./websrc c:/inetpub/wwwroot

# Sets a command or process that will run each time a container is run from the new image
CMD [ “powershell” ]

Build and Customize Container Image in windows 2016

In the docker file, # symbol used to describe the comment what this command does.

FROM microsoft/windowsservercore – It indicates which operating system image should be used.

MAINTAINER @AssistanZ – This is the optional command. It used to give metadata to indicate that who is maintaining this image.

RUN Powershell.exe -Command Install-WindowsFeature Web-Server – It will start a process called powershell.exe and deploy the web server role.

COPY ./websrc c:/inetpub/wwwroot – Copy the files into the container image during the build process. It looks for source folder named websrc and copies everything from that folder to

c:/inetpub/wwwroot inside the container. It’s a great way to inject a data or file inside container image during the build process.

CMD [ “powershell” ] – It asks the container to run a process anytime you run a container from this image. Here we are using PowerShell command window.

  • Create a folder named websrc under c:\web folder using below command.

mkdir websrc

Build and Customize Container Image in windows 2016
  • Create an index.html file and few contents in it.
Build and Customize Container Image in windows 2016
Build and Customize Container Image in windows 2016

BUILDING CONTAINER IMAGE

  • Go to PowerShell command window and execute the below command.

docker build -t web c:\web

docker – command for containers.

build – It’s a sub-option to build container images.

-t – To name and tag the container image.

c:\web – Path for the docker file.

Once you hit the command it will take few minutes to complete the build.

  • After completion, it will show the below result.
Build and Customize Container Image in windows 2016
  • You can able to view our new container images in the image list.
Build and Customize Container Image in windows 2016
  • If we run docker history web command, we will see four layers on top of container OS image.
Build and Customize Container Image in windows 2016
  • Basically, we are having every layer for each command and it’s important concept to understand it. Every command will have a docker file which going to create a new layer inside the image.
  • For complex docker files, we need to optimize the process to make more efficient during image building process.

VIDEO

Thanks for reading this blog. We hope it was useful for you to learn how to build the container images manually and through automation.

Loges