In last article, we learned how to run a python script on Docker container and printed “hello world”. In this article, we will learn how we can push this custom image to DockerHub and create a container on Azure by pulling same image from DockerHub. Benefit of creating custom images on DockerHub is we can deploy it on cloud by just giving name of repository and image name and it will deploy our code with platform required to run it.
We learned some command in previous article to build and run the container. Once we are successfully able to build image it will be saved on local machine with a unique image id. We will have to push it to DockerHub.
Pushing custom image to DockerHub
Step 1: Finding Image Id
Use command given below which will give all images on local machine:
Step 2: Login to DockerHub
SignUp on DockerHub and create a username, public repositories are free create. Run the command to login:
docker login --username=myusername
It will prompt for password, below I have logged into my account
Step 3: Tag your image with repository name and image name
Run below command, image id we can see in step 1:
docker tag ce549b4e9ee3 nikhiljsh22/helloworldapp:helloworldapp
Above format is: ImageID username/repositoryname:imagename
Step 4: Push the image
Use below command to push the image
docker push nikhiljsh22/helloworldapp
It will take some time, once it is done we can see image on DockerHub in our account and we can pull it from any other system. Since we created it on public repository anyone can download this image.
Creating container on Azure from DockerHub image
Now let’s create container from this image on Azure. Login to Azure and go to Create a resource -> containers -> Container Instances. Give container name and image name as shown below. I have this image on public repository you can use this image “nikhiljsh22/helloworldapp:helloworldapp” without creating your own.
Select subscription, resource group and click OK
And change configuration if you want to and click OK
Once validation is passed you can hit OK. I will take some time to create container. Once it is successfully created we can go to the container from All resources -> MyContainerName -> Containers -> Logs.
Once we go to logs and click refresh container restarts and we can see “hello world” in logs which is printed by our python script.