Skip to main content

Build a Windows Docker Image with Azure DevOps

Building a Docker Image with CI/CD is very useful. The primary use case I like is that I can set up CI for my Dockerfile. That means any time I update my Docker file it will automagically create a new build for me. Let's get started!

Pre-requisites/what you need:

1) An Azure Account
2) An Azure DevOps Account
3) Azure repos or another source control repo
4) ACR (Azure Container Repository) to store out Docker image
5) VSCode
6) A working knowledge on CI/CD in Azure DevOps (how to create builds, queue builds, build releases)

First things first - let's head over to our source control and copy down our repo. First thing I need to run is git clone yourrepo. I'm using Azure Repos but you can use whatever you prefer. You can copy it to wherever you like to work out of. I typically put everything in /users/me/Documents/GITREPOS on Windows or /users/me/home if I'm on Linux.



Now that we have our git repo copied down, let's go ahead and open up the directory in VSCode. Create a new file and name it "Dockerfile" with no extension.


Now that we have our Dockerfile let's go ahead and start filling it in! Below is the code I used, but feel free to use whatever suits you.

Please Note: If you are using Windows 10, please ensure you switch to Windows containers. If you right click the Docker whale in the task bar, you will see the option to switch. If you don't do this you will get a bunch of errors when trying to pull down the image.

FROM mcr.microsoft.com/windows/servercore:1607
MAINTAINER "Michael Levan"
RUN powershell -Command New-Item -ItemType Directory -Path C:\ -Name mynewconfigdir

Notice how I had to use "servercore:1607"? The way Microsoft does images is based on OS build. So if you're on Windows 10 1903, you can down the Docker Image for "servercore:1903". After looking at some errors I found, it seems that the OS build running this in Azure on the backend is 1607. This may change after the time of writing this so if the release fails, take a look at the error message and it should tell you what OS build it's currently running on.

Create a new directory and put your Dockefile in it. I called mine "WindowsDockerFile". Once you have your code, it's time to commit it up to your repo.


Now that our code is committed we're ready to kick off our build! Let's head over to Azure DevOps and go to pipelines > builds. Create a new build pipeline and choose the classic editor.


Once you choose your source, team project, repo, and branch, you're ready to go to click continue. Click on an empty project and for your tasks, use "Copy Files" and "publish build artifacts". Ensure that you have the right source folder, what contents you want to target, and your target folder. We're going to use the Build.ArtifactStagingDirectory predefined Azure DevOps variable.




If all goes well you should have all green check-marks with no errors or warnings.



We're now ready to queue up our build and start on our release. Let's head over to releases under Pipelines. Choose New Release and create an empty pipeline.

 For our artifact, choose the build you just created. If you re-did your build a few times, ensure you're choosing the latest.


Next we'll change our staging environment name to Dev, change the agent display name, and select the Docker task.


In my task for my container registry I'm going to connect to ACR and choose my repo.


The next thing we need to do is specify our repo and point to our Dockerfile that we want to build.



Once we do that let's save and create a release! If your release succeeded, you should see all green check-marks.


Now let's head over to ACR and see if our Docker image was pushed.


My Docker image was officially built and pushed to ACR with the help of CI/CD! 


Comments

Popular posts from this blog

DevOps tooling in the Microsoft realm

When I really started to dive into automation and practicing DevOps with specific tooling, there were a few key players. At the time Microsoft was not one of them. They were just starting to embrace the open source world, including the art and practice of DevOps. Since then Microsoft has went all in and the tech giant has made some incredible tooling. Recently I switched to a Microsoft-heavy environment and I love it. I went from AWS/Python/Ansible/Jenkins to Azure/PowerShell/ARM/Azure DevOps. My first programming language was PowerShell so being back in the saddle allowed me to do a full circle between all of the different types of tooling in both worlds. Today I want to share some of that tooling with you.

The first thing I want to talk about is ARM. What is ARM? ARM is a configuration management tool that allows you to perform software-defined-infrastructure. Much like Ansible and Terraform, ARM allows you to define what you want your environment to look like at scale. With ARM, yo…

Monitoring your containers in an AKS cluster with Prometheus

Monitoring and alerting is arguably one of the most important thing in Cloud Engineering and DevOps. It's the difference between your clients stack being up and a client being down. Most of us have SLA's to abide by (for good reason). Today we're going to learn how to spin up Prometheus in an AKS cluster to monitor our applications.

Pre-reqs;
1. Intermediate knowledge of Kubernetes
2. An AKS cluster spun up in Azure

Recently AKS supports Prometheus via Helm, so we'll use that for an automated solution to spin this up. This installs kube-prometheus, which is a containerized version of the application. With raw Prometheus, there are a few things that are needed for the operator;

1. Prometheus: Defines a desired deployment.
2. ServiceMonitor: Specifies how groups of services should be monitored
3. Alertmanager: Defines the operator to ensure services and deployments are running by matching the resource

With kube-prometheus, it is all packaged for you. This means configuri…

Run PowerShell code with Ansible on a Windows Host

Ansible is one of the Configuration Manager kings in the game. With it's easy-to-understand syntax and even easier to use modules, Ansible is certainly a go-to when you're picking what Configuration Management you want to use for your organization. Your question may be "but Ansible is typically on Linux and what happens when I'm in a Windows environment?". Luckily I'm here to tell you that Ansible will still work! I was pleasantly surprised with how easy it is to use Ansible on Windows with a little WinRM magic. Let's get started.

Pre-requisites for this post:
1) WinRM set up to connect to your Windows host from Ansible
2) Ansible set up for Windows Remote Management
3) SSH access to the Ansible host
4) Proper firewall rules to allow WinRM (port 5985) access from your Ansible host to your Windows host
5) Hosts file set up in Ansible that has your IP or hostname of your Windows Server.
6) At least one Linux host running Ansible and one Windows Server host …