Docker images are the building blocks of Docker containers. They are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including code, runtime, system tools, libraries, and dependencies. Images are immutable and can be shared between environments, ensuring consistency in application deployment.
What is a Docker Image?
A Docker image is a read-only template for creating Docker containers. It serves as a blueprint containing the application code, configurations, and dependencies required for execution. Each image consists of multiple layers, allowing efficient storage and distribution.
When a container is created, Docker uses an image as the base and adds a writable layer to store changes. This structure enables fast deployment and reusability of images.
Key Features of Docker Images
- Portability: Can run on any system with Docker installed.
- Lightweight: Uses a layered approach to optimize storage and reduce redundancy.
- Reusability: Images can be reused across different projects and teams.
- Versioning: Supports tagging to manage different versions of an image.
- Security: Read-only layers prevent unintended modifications.
How to Work with Docker Images
Docker images can be pulled from public repositories, built from custom instructions, and managed locally. You can create an image using a Dockerfile that defines the base system, dependencies, and configurations. Once an image is available, it can be used to run containers, versioned with tags, and shared through registries. Managing images effectively ensures consistent and repeatable application deployment.
Pulling a Docker Image
To download a pre-built Docker image from Docker Hub, use the docker pull
command:
docker pull ubuntu
This command fetches the latest Ubuntu image from Docker Hub.
To pull a specific version, specify the tag:
docker pull ubuntu:20.04
Here, 20.04
is the version (tag) of the Ubuntu image.
Listing Docker Images
To view all downloaded images on your system, run:
docker images
This displays the image repository, tag, image ID, and size.
Creating a Docker Image
To create a Docker image, you define the environment and instructions in a special file called a Dockerfile. This file tells Docker how to build the image, including the base operating system, required packages, copied files, and commands to run.
Let's create a simple Docker image that uses Ubuntu and prints a custom message.
Step 1: Set Up Your Project Directory
Create a new folder for your project files:
mkdir mydockerapp
cd mydockerapp
Step 2: Create a Dockerfile
Inside this directory, create a file named Dockerfile
(no file extension):
nano Dockerfile
Add the following content to define your image:
# Start from the official Ubuntu base image
FROM ubuntu:20.04
# Set the working directory inside the container
WORKDIR /app
# Copy current directory contents into the container
COPY . .
# Install necessary packages
RUN apt-get update && apt-get install -y curl
# Define the default command to run when the container starts
CMD ["echo", "Hello from my Docker container!"]
Step 3: Build the Docker Image
Once the Dockerfile is ready, run the docker build
command to create the image:
docker build -t mycustomimage .
-t mycustomimage
: Tags the image with the namemycustomimage
..
: Refers to the current directory, which contains the Dockerfile.
Docker reads the instructions in the Dockerfile, executes them, and creates a reusable image.
After building, the image is available locally and can be used to create containers.
Step 4: Verify the Image
To confirm the image was created, list available images:
docker images
You'll see mycustomimage
listed along with its size and image ID.
Running a Container from an Image
To create and run a container from the image:
docker run mycustomimage
This executes the default command in the CMD
instruction of the Dockerfile
.
To run an interactive container:
docker run -it ubuntu /bin/bash
he -it
flag starts an interactive session, allowing you to execute commands inside the container.
Viewing Running Containers
To check active containers:
docker ps
To view all containers, including stopped ones:
docker ps -a
Tagging a Docker Image
Tagging helps in versioning and pushing images to repositories.
docker tag mycustomimage myrepo/mycustomimage:v1
Now, the image can be pushed to a Docker registry.
Pushing an Image to Docker Hub
To share an image, first log in to Docker Hub:
docker login
Then, push the image:
docker push myrepo/mycustomimage:v1
Removing a Docker Image
To delete an image:
docker rmi mycustomimage
To force removal:
docker rmi -f mycustomimage
Conclusion
In this tutorial, you learned what Docker images are, why they matter, and how they work as the foundation of containers. You explored how to pull images from Docker Hub, list them, and build your own custom images using a Dockerfile. You also saw how to run containers from images, tag and push them to repositories, and remove unused images. Understanding Docker images is key to creating consistent, portable, and efficient environments for deploying applications.