How to Create a Docker Image on Debian 12 Bookworm System

How to Create a Docker Image on Debian 12 Bookworm System

In the world of software development and system administration, Docker has become a go-to tool for packaging, distributing, and running applications in isolated environments called containers. On a modern Debian 12 “Bookworm” system, Docker can be seamlessly integrated into your workflow. This guide will walk you through the process of creating your own Docker image—from setting up Docker to building and managing your custom image.

Whether you’re deploying microservices, testing environments, or just looking to package your application for easier deployment, understanding how to create a Docker image is a fundamental skill.


1. Introduction to Docker Images

A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software—code, runtime, libraries, and system tools. These images serve as blueprints to create Docker containers.

Docker images are built using a special file called a Dockerfile, which contains a series of instructions describing how to assemble the image. Once built, images can be reused, shared, versioned, and deployed consistently across environments.


2. Prerequisites

Before diving into Docker image creation, ensure you have the following:

  • A system running Debian 12 Bookworm
  • A user account with sudo privileges
  • An active internet connection
  • Basic knowledge of the Linux terminal and shell scripting

3. Installing Docker on Debian 12

Let’s begin by installing Docker on your Debian system.

Step 1: Update Your System

sudo apt update && sudo apt upgrade -y

Step 2: Install Required Packages

sudo apt install ca-certificates curl gnupg lsb-release -y

Step 3: Add Docker’s Official GPG Key

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 4: Set Up the Docker Repository

echo \
"deb [arch=$(dpkg --print-architecture) \
signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Install Docker Engine

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Step 6: Verify Docker Installation

sudo docker version

You can also run a test container:

sudo docker run hello-world

If successful, Docker is properly installed and ready to use.


4. Understanding Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image. Each instruction in the Dockerfile creates a layer in the image, allowing Docker to cache and reuse layers efficiently.

Here’s a basic example of a Dockerfile that creates a simple image with Nginx installed:

# Use an official base image
FROM debian:bookworm

# Set metadata
LABEL maintainer="you@example.com"

# Install Nginx
RUN apt update && apt install -y nginx && apt clean

# Expose port 80
EXPOSE 80

# Start Nginx in the foreground
CMD ["nginx", "-g", "daemon off;"]

5. Creating Your First Docker Image

Let’s walk through creating a Docker image step-by-step.

Step 1: Create a Working Directory

mkdir ~/my-nginx-image
cd ~/my-nginx-image

Step 2: Write Your Dockerfile

Create a file named Dockerfile with the content from the previous section:

nano Dockerfile

Paste the Dockerfile content and save it.


6. Building the Docker Image

Now that you have your Dockerfile ready, build the image using the docker build command.

sudo docker build -t my-nginx-image .

Here:

  • -t my-nginx-image tags your image with a name.
  • The . indicates the current directory as the build context.

If everything goes well, Docker will go through each instruction, and your image will be built and stored locally.


7. Running a Container from Your Image

Once built, you can create and run a container using your new image:

sudo docker run -d -p 8080:80 --name my-nginx-container my-nginx-image

This command:

  • Runs the container in detached mode (-d)
  • Maps port 8080 on the host to port 80 in the container (-p)
  • Names the container my-nginx-container
  • Uses the my-nginx-image to create the container

You can now access Nginx in your browser by visiting:

http://localhost:8080

To stop the container:

sudo docker stop my-nginx-container

To remove it:

sudo docker rm my-nginx-container

8. Tagging and Publishing Images (Optional)

If you want to share your image via Docker Hub or another registry, follow these steps:

Step 1: Tag Your Image

sudo docker tag my-nginx-image yourdockerhubusername/my-nginx-image:latest

Step 2: Log in to Docker Hub

sudo docker login

Step 3: Push the Image

sudo docker push yourdockerhubusername/my-nginx-image:latest

Now others (or you) can pull the image using:

docker pull yourdockerhubusername/my-nginx-image:latest

9. Best Practices for Creating Docker Images

Creating efficient and secure Docker images involves more than just writing a Dockerfile. Here are some best practices:

a. Use Minimal Base Images

Start with minimal images like alpine or slim variants of distributions to reduce image size.

b. Combine Commands to Reduce Layers

Instead of:

RUN apt update
RUN apt install -y nginx

Use:

RUN apt update && apt install -y nginx

c. Clean Up After Installation

To reduce image size:

RUN apt update && apt install -y nginx && apt clean && rm -rf /var/lib/apt/lists/*

d. Specify Versions for Reproducibility

Always specify exact versions when installing software to avoid surprises in the future.

e. Use .dockerignore

Similar to .gitignore, this file tells Docker which files to exclude from the build context.

Example .dockerignore:

*.log
*.tmp
.git
node_modules

f. Use Multi-Stage Builds

When building larger applications (like compiling code), multi-stage builds help keep the final image small.


10. Conclusion

Creating Docker images on a Debian 12 Bookworm system is straightforward and powerful. Whether you’re working on web applications, microservices, or backend APIs, Docker allows you to build reproducible, portable, and scalable environments.

To summarize:

  • Install Docker properly on Debian 12
  • Write a Dockerfile that suits your needs
  • Build, run, and test your Docker image
  • Optionally, share your image via Docker Hub

By following best practices, you can ensure your Docker images are efficient, secure, and maintainable. With Docker now in your toolset, you’re better equipped to tackle development and deployment challenges in a modern Linux environment.