We have seen the manual method of creating a container from an image and an image back from a container after you have done all the customizations. What if there is a more automated way of doing this. We can achieve the same end goal by using a DockerFile.
What is DockerFile?
Dockerfile is nothing but a set of instructions or the set of commands you would like to be executed in your container before it’s packaged into an image. For example
- Download ubuntu docker image
- execute apt update
- apt upgrade
- apt install nano
- create a folder named network_automation
- copy a demo text file from you local OS into the network_automation folder of the cotainer
- and then build the image which can be distribubted. We have seen the manual method in post III of this series.
Let’s see how we can automate that process using a Dockerfile.
I have opened the current working directory of the host OS in VScode to write Dockerfile.
Highlighted in PINK are the keywords that serve as instructions for the docker build server.
Execute docker build command to make an image out of the instructions in the Dockerfile
Once the build process is completed, we can check docker images to verify my-multi-container is created and docker ps shows no running container at the moment.
Let’s run this image inside a container to see what it has in it.
So now instead of sharing the network_automation image that we built in the last post which was more than a Gig in size, we can share this Dockerfile instead which is only bytes in size and they can build the exact same container as you have when you started working on your project. Instead of sharing the Dockerfile, you could upload the image you built on DockerHub or any other cloud for people to use directly or as a base for their project.
For example, for the purpose of network automation, we need at least below dependencies
- Base image ( ubuntu most probably )
- nano text editor
- apt-get update && apt-get upgrade
- install python
- install pip
- install python libraries that we need for automation like netmiko, napalm, nornir, etc etc
All this could be dockerized as a template for network engineers to start working without they needing to build it from scratch. In fact, if you look on docker hub for network automation, you will surely find images that you can directly leverage for network automation, and it’s highly likely they might have some code samples in it too.