Cloud Native 12 min read

Master Docker Images: From Basics to Building and Managing Containers

This article explains Docker images as lightweight, immutable templates, details their layered architecture, shows how to build them with Dockerfiles, demonstrates pulling and running images from Docker Hub, and covers essential commands for managing and cleaning up images.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Docker Images: From Basics to Building and Managing Containers

Docker's core concept includes Docker images, lightweight independent executable packages that contain everything needed to run software.

1. What is a Docker image?

A Docker image can be seen as a "container template" or static blueprint. It bundles the operating system, libraries, application code, and configuration into an immutable, layered package, improving build efficiency and storage usage.

Image construction typically starts from a base OS image (e.g., Ubuntu or Alpine) and adds dependencies, code, and other components, producing a reusable snapshot that can launch containers.

Images are built via a Dockerfile, a set of commands such as FROM (base image), RUN (install dependencies), COPY (add files), and CMD or ENTRYPOINT (define the container's start command). The build steps create layers:

Base layer – start from an OS image like Ubuntu or Alpine.

Dependency layer – install required libraries with RUN .

Application layer – add source code using COPY or ADD .

Configuration layer – set environment variables or tools with CMD / ENTRYPOINT .

Each instruction adds a new read‑only layer; the final image contains the cumulative result.

2. How Docker images work

Docker images are built in layers, and each layer is cached. If a layer already exists, Docker reuses it, speeding up builds.

Only the changed layers are rebuilt when you modify code without altering the base OS or dependencies, which is the key to Docker's efficiency.

Different images can share the same base layer, reducing storage consumption.

Base layer : a lightweight Linux distribution such as Alpine or Ubuntu.

Dependency layer : libraries required by the application.

Application layer : the application code and its own dependencies.

Configuration layer : environment variables, tools, or other settings.

Docker uses layered storage drivers (AUFS, OverlayFS, Btrfs, etc.) to stack these read‑only layers with a writable container layer, presenting a unified filesystem to the container.

Overlay mechanism: read‑only layers are overlaid with the container's writable layer, appearing as a single filesystem.

Copy‑on‑write: modifications are written to the writable layer, preserving the integrity of the underlying image layers.

3. Docker images vs. containers

Think of a Docker image as a class in object‑oriented programming—a static blueprint defining structure and behavior. A Docker container is an instance of that class, a running, isolated process with its own writable layer.

The container's writable layer has two states: running and exited. When you start a container with docker run , it enters the running state; stopping it moves it to the exited state.

4. Pulling and running images from Docker Hub

Docker Hub is a public registry where developers share images. To run an Nginx web server, you can pull and start the official image:

<code># Pull the Nginx image from Docker Hub
docker pull nginx

# Run the Nginx container in detached mode, mapping host port 8080 to container port 80
docker run -d -p 8080:80 nginx
</code>

After running, accessing http://localhost:8080 displays the Nginx welcome page.

Docker Hub restored normal access in mainland China on 2024‑09‑07. Prior to that, users could use acceleration services (e.g., Alibaba Cloud) or domestic mirrors such as AtomHub.

5. Creating your own Docker image

Custom images are built with a Dockerfile, which scripts the build process. Below is a simple Dockerfile for a Go application:

<code># Start from a base image
FROM golang:1.18

# Set the working directory inside the container
WORKDIR /app

# Copy Go module files
COPY go.mod ./
COPY go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build the application
RUN go build -o myapp

# Define the command to run the application
CMD ["./myapp"]
</code>

Build the Docker image

Once the Dockerfile is ready, build the image with:

<code>docker build -t my-go-app .
</code>

Run the Docker image

Start a container from the newly built image and map ports:

<code>docker run -d -p 8080:8080 my-go-app
</code>

6. Managing Docker images

Docker provides commands to list, delete, and clean up images:

List images : <code>docker images </code>

Delete an image : <code>docker rmi &lt;image-id&gt; </code>

Prune unused images : <code>docker image prune </code>

7. Conclusion

Docker images are the core building blocks of the Docker ecosystem, ensuring consistent and portable application delivery across environments. Whether pulling from Docker Hub or crafting custom images with Dockerfiles, mastering image creation and management gives you precise control over containerized applications, enabling efficient, reliable deployments.

cloud-nativeDockerDockerfilecontainersImage ManagementDocker images
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.