Understanding Docker Images – The Building Blocks of Containers

Pawan LuhanaPawan Luhana
3 min read

1. Introduction

Docker images are the blueprints for running containers. They contain everything needed to run an application, including code, dependencies, and configurations.

Think of a Docker image like a cake recipe πŸ°β€”it defines the ingredients and steps needed to create a container (the final cake).

In this guide, you’ll learn what Docker images are, how to pull them from Docker Hub, manage them, and even create your own using a Dockerfile.


2. What is a Docker Image?

A Docker image is a pre-packaged software environment that includes:
βœ… The application code
βœ… System libraries & dependencies
βœ… Configurations & environment variables

Once an image is created, it can be used to run multiple identical containers anywhere.

Example:

The Nginx image contains everything needed to run an Nginx web server inside a container.

Run this command to start an Nginx container:

docker run -d -p 8080:80 nginx

Now, open http://localhost:8080 in your browser to see the Nginx welcome page! πŸš€


3. How to Pull Images from Docker Hub

Docker Hub is like an App Store for Docker images, hosting thousands of pre-built images.

Pulling an Image

To download an image from Docker Hub, use:

docker pull ubuntu

This fetches the latest Ubuntu image.

Viewing Downloaded Images

Check your local images with:

docker images

4. Listing & Removing Images

Listing Images

To see all images on your system:

docker images

Output Example:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
ubuntu       latest    a2a15febcdf3   2 days ago     29MB
nginx        latest    d1a364dc548d   1 week ago     25MB

Removing Images

To delete an unused image:

docker rmi nginx

If the image is being used by a container, stop and remove the container first:

docker stop <container_id>
docker rm <container_id>
docker rmi nginx

5. What is a Dockerfile, and Why is It Important?

A Dockerfile is a text file containing instructions to create a custom Docker image.

Think of it like a recipe πŸ“œβ€”it defines how an image is built.


6. Writing a Basic Dockerfile

Let’s create a simple Node.js image using a Dockerfile.

Step 1: Create a Dockerfile

Create a new directory and add a file named Dockerfile:

# Use an official Node.js image as the base
FROM node:14

# Set the working directory inside the container
WORKDIR /app

# Copy application files
COPY . .

# Install dependencies
RUN npm install

# Define the command to run the app
CMD ["node", "app.js"]

7. Building a Custom Image Using docker build

To create an image from the Dockerfile, navigate to the directory and run:

docker build -t my-node-app .

This creates an image named my-node-app.

Running the Custom Image

Start a container using:

docker run -d -p 3000:3000 my-node-app

Your Node.js app is now running in a container! πŸŽ‰


8. Conclusion

Docker images are essential for containerized applications. Now you know how to:
βœ… Pull images from Docker Hub
βœ… List and remove images
βœ… Write a Dockerfile
βœ… Build a custom Docker image

Start experimenting with Dockerfiles and create your own images! πŸš€

0
Subscribe to my newsletter

Read articles from Pawan Luhana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pawan Luhana
Pawan Luhana