Docker Automation with Dockerfile

During my previous articles, I showed how to create Docker containers, in Windows and Linux environment. In both solutions there’s always a problem about creation of master template, through manual editing and this means waste time.

 

In this article we will see how to automate this phase with the Dockerfile. The logic is very simple: into this file there are some step-by-step instructions to avoid manual activities and this means reduce the errors to zero and automate procedure.

 

Syntax

These are the commands that you can use into Dockerfile:

  • ADD
  • CMD
  • ENTRYPOINT
  • ENV
  • EXPOSE
  • FROM
  • MAINTAINER
  • RUN
  • USER
  • VOLUME
  • WORKDIR

 

Example

This is an example that you can use to build a container based on Windows Server 2016 with IIS:

 

FROM microsoft/iis

RUN mkdir C:\temp

RUN powershell -Command \

               Install-WindowsFeature -Name NET-Framework-45-ASPNET ; \

               Install-WindowsFeature -Name Web-Asp-Net45 ; \

    wget -uri http://10.10.1.8/image01-docker.zip -OutFile C:\temp\webmodel.zip ; \

               Expand-Archive -Path ‘C:\temp\webmodel.zip’ -DestinationPath ‘C:\inetpub\wwwroot’ -Force ; \

               Remove-Item C:\temp\webmodel.zip -Force

 

What the script does is use the image microsoft/iis, from the syntax FROM (if the image is not present into the local repository, it will be downloaded); the second step is run two commands, with the syntax RUN, where the first one create a new folder and the second one run a subset of PowerShell cmdlets to enable ASP.Net role, download the web site template, expand the content and flush the unneeded files.

 

To copy the item, I could also use the syntax COPY with the only difference that the source file must be into Container Host.

 

Once the Dockerfile is ready, you can build the container with the command docker build -t containername .

 

The “dot” and the end of command is necessary to specify where is the Dockerfile; in case you have a central repository, you must declar the full path. The result is similar like figure 1.

 

2016_11_08_docker-01

Figure 1 – Dockerfile with External Path

 

When the wizard end, check the presence of new image with the command docker images.

 

Conclusion

This is another example to automate Docker in order to simplify deployment of your workload, compared with the classic virtual machine deployment. Dockerfile is also perfect where there are more than one Container Host, where is necessary build many times the same images, like a web server for a specific customer.

 

Links

https://docs.docker.com/engine/reference/builder/