Image for post
Image for post

Creating your own Docker image

Docker Hub is the world’s largest library and community for container images. And recently docker became one of the essentials tools in development. Most of the time when you want to build your project you can find the most suitable image in Docker hub for that purpose. But sometimes you may need a custom image to do a specific job, like the backup process for MySQL and Mongo and store the backups in Azure cloud, this is a scenario I faced that couldn't find any suitable image to achieve that, and the solution was to build my own image.

My example needed components

I need to run a job on a Kubernetes cluster that will use mysqldump and mongodump to dump the databases, then I will need to call “az storage blob upload” to upload these backups to Azure Blob storage. So while I couldn’t find a suitable docker image that contains at least two components of my scenario I decided to choose microsoft/azure-cli as my base image, you can check it from the next link:

And then I will add the other components manually on this image. this image already comes with the Azure command interface.

Steps to build the image

First, I created a docker container from the parent image, so after logging in to my Docker hub account through the next command:

docker login

I ran the image using the next command:

docker run -it microsoft/azure-cli /bin/bash

I ran the /bin/bash to make sure that the container still running and so I can apply more commands on that image.

Then I installed Mongodump command, its enough just to install that command alone than installing the whole Mongo database. You can find that command for alpine images from the next link:

So downloading that command is just about applying the next command:

apk add mongodb-tools

Then I installed mysqldump command, I used mariadb-client for that purpose from the next link:

More about that package in the next link:

So in order to install that package, I used the next command:

apk add mariadb-client

At that time my container is ready and I needed to commit my changes to a new image, so from another command-line interface I called:

docker ps

To see all running containers, then I took my container id, and then applied the next command:

docker commit <container_id> amhaish/azure-mysql-mongo-cli

By that time, if I run the new image I will find all packages we need inside.

In order to push the image to the Docker hub, I needed to use the next command:

docker push amhaish/azure-mysql-mongo-cli

So always the name of the image will start with your user name on Docker Hub which is “amhaish” in my case and then the name of the image which is “azure-mysql-mongo-cli” in my case.

In both cases, if you need a container image that contains the dump commands for both MySQL and Mongo in addition to the Azure command interface, then no need to invent the car wheel again. I already made one and you can use it from the next link:

So always search for an image before building your own, most of the time you will find your need.

I am a Software Architect and AI engineer that have a great passion for integrating technology with businesses and human life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store