Home > Backend Development > Golang > Building Minimal Docker Images

Building Minimal Docker Images

Linda Hamilton
Release: 2025-01-29 14:07:09
Original
982 people have browsed it

Building Minimal Docker Images

streamlined Docker mirror: Revolutionary advantages of improving the workflow

Let's talk about Docker. Yes, this magical tool seems to be praised by every developer. It promises to make our lives easier and the deployment is smoother. But the problem is that your Docker mirror may be bloated. They are like a large suitcase you pack for weekend, full of unnecessary things, you can't even find what you need! This is like using a backpack instead of a handcase. Now, let's introduce to the Docker mirror

, this is the ultimate weight loss plan of your container. ?

This is not just a few megers that reduce mirrors, but about creating streamlined, efficient, and fast containers. These containers are more easy to protect, deploy and expand. So, fasten the seat belt! We will explore the world of streamlined Docker mirrors, and use real examples and actual steps to enrich it.

What is the significance of streamlined Docker mirror? ?


The docker mirror is regarded as the dietary diet. Rather than filling unnecessary libraries, tools and configurations, it is better to include only necessary items. This is like ordering a simple Margaret pizza instead of a "luxury" pizza full of various ingredients. The taste is better and the effect is better.

The following is a fast comparison:

----

Why should you care about streamlined Docker mirrors? ?

If you are thinking, "Why pay so much? My application runs well", then the streamlined mirror is The reason for changing the rules of the game as follows:

1. Security: more streamlined, more at ease?

Every additional dependencies, tools or libraries in the Docker mirror are potential loopholes. The streamlined mirror reduces the attack surface. The fewer parts, the less the hacker sneaks. It's like locking all your doors -there are no uninvited guests here!

2. Save cost?

Storage and bandwidth are not free, especially in the cloud. Whether you are deployed on AWS, Azure or GCP, you will accumulate per meged byte. Simplified mirrors can save storage costs and reduce data transmission costs. You can use this money to buy pizza. ?

3. Easy expansion?

When you deploy multiple containers in the cluster, every second is important. Smaller mirror startup speed is faster, making the extension easier.

Step by step: Construct a streamlined Docker mirror? ️ ​​


Let's enrich with a real example! We will build a simple Go application and create a super -lightweight Docker mirror for it.

Project structure?

Your project folder should be shown below:

GO application

main.go
<code>/dockerized-golang-server
  |-- Dockerfile
  |-- go.mod
  |-- main.go</code>
Copy after login
Copy after login
- A simple "Hello, World!" Server. Because why make life complicated?

GO.MOD - Define modules and GO versions.

<code>package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })
    http.ListenAndServe(":8080", nil)
}</code>
Copy after login
Secret: Streamline Dockerfile? ️

The following is the magic secret recipe of streamlined mirror constructed in multiple stages:

<code>module github.com/krishnaaher/golang-server
go 1.23.3</code>
Copy after login
Its working principle:

Construct
<code># 阶段1:构建Go应用程序
FROM golang:1.23.3-alpine AS builder

WORKDIR /app

COPY go.mod .
COPY main.go .

RUN go build -o /app/main

# 阶段2:创建一个精简的运行时环境
FROM scratch

COPY --from=builder /app/main /app/main

# 运行应用程序的命令
CMD ["/app/main"]</code>
Copy after login
: The first stage compiles the application. The second stage only includes the compiled binary files.

SCRATCH basic mirror : The lightest element image (0 byte). This is like starting with a piece of white paper.

    No additional content
  • : No unnecessary library, tools or files -only your application.
  • Build and run your streamlined mirror? The following is how to from zero to heroes in a few simple steps:
  • 1. Build a mirror image
  • Run the following command in your project directory:

2. Check the size

Verify whether your mirror is streamlined as you want:

Expecting output:

3. Run the container
<code>docker build -t go-minimal-server .</code>
Copy after login

Run your mirror image and map it to the port 8080:

4. Test applications

<code>docker images</code>
Copy after login

Open your browser or use CURL for testing:

<code>REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
go-minimal-server   latest    0b690a22521a     Just now     11.7MB</code>
Copy after login

Output:
<code>/dockerized-golang-server
  |-- Dockerfile
  |-- go.mod
  |-- main.go</code>
Copy after login
Copy after login

The key factor of success?

  • Construct : compile in one stage and run in another stage. It is clean, efficient, and keeps mirroring small.
  • Select the correct basic mirror image
  • : Start from scratch to get the smallest image. If you need some basic OS features, Alpine is a good choice. Delete unnecessary dependencies
  • : Use Go Mod Tidy or similar tools to clean up your language.
  • Local test : Always test your streamlined mirror to ensure that no important content is lost.
  • Now, start docker! Your lightweight container is waiting for you. ?

The above is the detailed content of Building Minimal Docker Images. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template