Table of Contents
Why should you care about streamlined Docker mirrors? ?
2. Check the size
The key factor of success?
Home Backend Development Golang Building Minimal Docker Images

Building Minimal Docker Images

Jan 29, 2025 pm 02:07 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

See all articles