Docker - How to build Docker Image and push it on Docker Hub

Feb 20, 2023

What is Docker?

In this article, you will learn to build Docker image from scratch, and deploy and run your application as a Docker container using Dockerfile.Docker allows developers to build, test, and deploy applications quickly and efficiently using isolated and portable containers that run anywhere.

 

How to Create Docker File

In order to build the container image, you’ll need to use a Dockerfile. A Dockerfile is simply a text-based file with no file extension. A Dockerfile contains a script of instructions that Docker uses to create a container image.In a Dockerfile Everything on left is INSTRUCTION, and on right is an ARGUMENT to those instructions. Remember that the file name is "Dockerfile" without any extension.

To create Docker file from visual studio

- Open project folder in visual studio

- Right click on project folder 

- Go to add

- Go to docker support

 

 

- There are two options to build docker file : windows and Linux

- Select one of the given option and it will create the docker file

 

 

Here we have selected windows operating system, so it will create docker file for windows image but if you select Linux operating system then it will create docker file for Linux image and rest of the docker commands will be same for windows as well as Linux

 

How to Build Docker Image

We will build our image using the Docker command. The below command will build the image using Dockerfile from the same directory.

docker build -t demoimage:1.0 .

- t is for tagging the image.

- demoimage is the name of the image.

- 1.0 is the tag name. If you don’t add any tag, it defaults to the tag named latest.

- . means, we are referring to the Dockerfile location as the docker build context.

 

After the image build output will look like below.

Now, we can list the images by using this command.

docker images

 

Test the Docker Image

Now after building the image we will run the Docker image. The command will be,

docker run -d -p 4000:80 --name democontainer2 demoimage:1.0

- d flag is for running the container in detached mode.

- p flag flag for the port number, the format is local-port:container-port.

- --name for the container name, democontainer2 in our case.

 

Docker started our container in the background and printed the Container ID on the terminal.

We can check the running container by using the below command.

docker ps

In a web browser, access http://localhost:4000 and we can see the index page which displays the content in the custom HTML page we added to the docker image.

After creating Docker image we can see all the local images in Docker windows Desktop.

Go to the images tab and we can see all the images.

Go to the containers tab and we can see all the containers also.

Here we are using docker desktop for windows,so we have selected switch to windows option,if you are using docker desktop for Linux then select switch to Linux option.

 

Push Docker Image to Docker Hub

Docker Hub is a registry service on the cloud that allows you to download Docker images that are built by other communities. You can also upload your own Docker built images to Docker hub.To push our Docker image to the Docker hub, we need to create an account in the Docker hub.

After that, execute the below command to log in from the terminal. It will ask for a username and password (if you are login for the first time). Provide the Docker hub credentials.

docker login

After login, we now need to tag our image with the docker username as shown below.

docker tag demoimage:1.0 <username>/<image-name>:tag

For example, here hiral1 is the docker hub username.

docker tag demoimage:1.0 hiral1/demoimage:1.0

Run docker images command again and check the tagged image will be there.

Now we can push our images to the Docker hub using the below command.

docker push hiral1/demoimage:1.0

Now we can check this image will be available in our Docker Hub account.

 

We can inspect a container by following command.

docker inspect <container-id>

 

We can view Docker logs in a Docker container by following command.

docker logs <container-id>

 

And we can stop the running container by following command.

docker stop <container-id>

 

Pull and Run Docker Image from Docker Hub

To pull image from docker hub use the following command.

docker pull <imagename:tag>

 

Docker checks if the image already exists or not, if it is then it does not download further.In our case it is already there.So we need to remove existing image.

To remove the docker image use the following command.

docker rmi <imagename:tag>

 

Now pull the docker image from docker hub and check for the list of images.

 

To run the pulled image use the below command

docker run -d -p 4000:80 --name windowscontainer  hiral1/demoimage:1.0

- d flag is for running the container in detached mode.

- p flag flag for the port number, the format is local-port:container-port.

- --name for the container name, windowscontainer  in our case.

In a web browser, access http://localhost:4000 and we can see the index page which displays the content in the custom HTML page we added to the docker image.

Below is the example of how to pull and run docker image on Linux VM.

Here is the output of the docker image on the browser.

Rename/Tag Docker Image

To rename the docker image tag the image as follows.

docker tag <oldimagename:tag> <newimagename:tag>

In our case the old image name is hiral1/demoimage , old tag is 1.0 and new image name is newimage/latest.We have not provided a new tag to newimage, so it gives tag - latest by default.

 

Remove Docker Image

To remove the docker image use the following command.

docker rmi <imagename:tag>

TAGS Docker
Kishan Parmar

About the Author

Kishan Parmar

Team Leader at MagnusMinds IT Solution
Team Leader with a demonstrated history of working in the computer software industry. Skilled in Asp.net MVC 4.0, C#, WPF Development, Terraform, Infrastructure as a code, AWS, Azure, IONIC, Node JS, Asp. Net Core, Web API MVC, .NET Core Web API, Application Programming Interfaces, and Raspberry Pi.
Strong engineering professional with a Bachelor of Engineering (B.E.) focused on Computer Engineering.