DEV Community

Cover image for Docker for Dummies (Part 2)
Vignesh Pb
Vignesh Pb

Posted on

2 1 1 1 1

Docker for Dummies (Part 2)

In the previous post we took a look at a simple explanation of what Docker is and why one should use it. In this post we will take a look at how to create and delete docker images. You should have Docker installed in your system to follow this blog

As always this series is meant to be easy to understand and as such some topics will not be explained or will be covered at a later part when you are ready to work with them.

Inspecting our System for Docker Images

We can view the images that we have in our system by the docker images command. You can see a sample of this output below

Sample Output of the docker images command with no image

Right now there is no image so it just shows just the headings.

Writing a docker file to create the most simplest image ever

To create an image create an empty folder inside which where you will write your docker file. I named my folder as dockerlearning.

Create a file with any name that you want for the Docker File, however most engineers use the name Dockerfile.

We will learn how to build images by building the simplest image possible

A sample docker file with just one line pulling alpine linux

We have a docker file which asks Docker to pull the alpine linux image. In other words our first image is just a bare bones version of alpine linux.

All images follow the following name convention

Image showing the naming convention all images follow

Where image_name is the name of the image while tag_name represents the version of the image. The version of the image can be anything that resembles a version like 1.3, 2.5, latest, rolling etc

If you do not specify the tag name, docker will assume the tag name is latest

Building your docker image

Once you have written the docker file, you can build an image out of it with the following command

Image showing the command to build a docker file

The above command builds an image called my_first_image:latest from the docker file Dockerfile. You can actually ignore the -f Dockerfile portion of your command if you name your docker file as Dockerfile

Once you do this, docker starts building an image from your Docker file and the process of building gives an output in your terminal which looks something similar to the below image

Sample Build output of a Docker build command

Once the build is done, you can check your system if you have any images again with the docker images command.

Output of docker images command after build

Now we see that docker is listing that we have an image with the tag that we gave it alone with an IMAGE ID and the size for it.

Deleting your Docker Image

If you ever want to delete a docker image that you created you can do so by running the following command

Output of Docker Image Deletion Command

Running the docker rmi command with the image ID will delete the image

Now if you run the docker images command you will see nothing listed again.

Summary

In this blog post, we saw the following

  • How to list the docker images in our computer with the docker images command
  • How to write a very simple docker file
  • How to build a docker image with the docker build command
  • How to delete a docker image with the docker rmi command

Some Homework for you

Here are some questions for you to ensure that you get more practice and that you understand the process that is going on

  • Is alpine linux the only image that can be pulled?
  • Are operating systems the only images that can be pulled?
  • Wasn't the whole point of containers to run your software without interference from other software? How does pulling an OS image solve this? Or are we missing something yet?
  • What happens when we create another image with the same name?
  • Where are the created images stored?

Next Steps

In the next blog post, we will take a look at creating docker images that are much more detailed that the one line docker file that we used right now and we will dive into some additional commands that we can write in the Docker file

Check out the CodeNewbie Podcast!

Check out the show on Spotify or wherever you get your podcasts.

This episode is all about Learning AI

Meet Matt Eland, AI Specialist at Leading EDJE. Matt shares what sparked his passion for AI years ago, why he’s made the decision to go back to school for his master's degree and how he aims to continue spreading his expertise with the community.

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay