Building an Azure Event Grid app. Part 3: Turning the publisher app into a Docker image.
Why do I want my publisher app to be a Docker image? Because I’d potentially like to be able to run the app from different environments and/or have other people run the app from their environments. Not having to rebuild or distribute the binaries would make it easier and I can be more certain that it will “just work”.
How to take my code (.NET Core in my example but again could be pretty much anything) and containerize it? I edited my app using Visual Studio Code and that made the process very easy.
- Make sure you’ve got the Docker extension installed in VS Code.
- Get VS Code to generate the Dockerfile for you by just answering some simple questions.
- Get VS Code to build the image for you.
That’s it. Very straightforward and now I have a Docker image for my app, run docker images and see your new image, or use the Docker explorer in VS Code for a UI equivalent.
Now I can run docker run <imagename> and pass in the same arguments as discussed in part 1.
But that’s only useful on my dev box. What I can now do is push the image to a repository so that I, or someone else, can pull down the image and run it on another environment. The obvious repository options for me are DockerHub and Azure Container Registry. In this example I’ll push to DockerHub as I want it to be easily publicly available:
- Create a DockerHub login if you don’t have one.
- Login to Docker at the command line:
- Build the container with a suitable tag (the prefix being your DockerHub id)
docker build -t gdavi/alarms-iot-simulator .
- Push the image to the repository:
docker push gdavi/alarms-iot-simulator
You should then have your image in the repository. Mine is at https://hub.docker.com/r/gdavi/alarms-iot-simulator/
Now you can perform a docker pull and have the image on another environment quickly, and ready to run as soon as it’s downloaded.