Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the easiest way to write Go Dockerfile

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the simplest Go Dockerfile writing method". In daily operation, I believe many people have doubts about what the simplest Go Dockerfile writing method is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what is the simplest Go Dockerfile writing method?" Next, please follow the editor to study!

1. Some extra points for Dockerfile

Choose the simplest mirror

For example, alpine, the whole image is about 5m.

Set the mirror time zone

RUN apk add-no-cache tzdata

ENV TZ Asia/Shanghai

two。 Multi-stage construction

The first phase builds the executable file to ensure that the build process is independent of the host

In the second stage, the output of the first stage is used as input to construct the final minimalist image.

3. Complete Dockerfile writing process

Install the goctl tool first

GO111MODULE=on GOPROXY= https://goproxy.cn/,direct go get-u github.com/tal-tech/go-zero/tools/goctl

Create a hello service under the greet project

Goctl api new hello

The file structure is as follows:

Greet

├── go.mod

├── go.sum

└── service

└── hello

├── etc

│ └── hello-api.yaml

├── hello.api

├── hello.go

└── internal

├── config

│ └── config.go

├── handler

│ ├── hellohandler.go

│ └── routes.go

├── logic

│ └── hellologic.go

├── svc

│ └── servicecontext.go

└── types

└── types.go

Generate Dockerfile in the hello directory with one click, as follows

Goctl docker-go greet.go

The Dockerfile content is as follows:

FROM golang:alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED 0

ENV GOOS linux

ENV GOPROXY https://goproxy.cn,direct

WORKDIR / build/zero

ADD go.mod.

ADD go.sum.

RUN go mod download

COPY. .

COPY service/hello/etc / app/etc

RUN go build-ldflags= "- s-w"-o / app/hello service/hello/hello.go

FROM alpine

RUN apk update-no-cache & & apk add-no-cache ca-certificates tzdata

ENV TZ Asia/Shanghai

WORKDIR / app

COPY-- from=builder / app/hello / app/hello

COPY-- from=builder / app/etc / app/etc

CMD [". / hello", "- f", "etc/hello-api.yaml"]

Build image in greet directory

Docker build-t hello:v1-f service/hello/Dockerfile.

View Mirror

Hello v1 5455f2eaea6b 7 minutes ago 18.1MB

You can see that the size of the image is about 18m.

Start the service

Docker run-- rm-it-p 8888 it 8888 hello:v1

Test service

$curl-I http://localhost:8888/from/you

HTTP/1.1 200 OK

Content-Type: application/json

Date: Thu, 10 Dec 2020 06:03:02 GMT

Content-Length: 14

{"message": ""}

At this point, the study on "what is the simplest way to write Go Dockerfile" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report