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

How golang builds images based on Mac os

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how golang builds images based on Mac os. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The project content is as follows: package mainimport ("fmt"net/http") func main () {http.HandleFunc ("/", handler) http.ListenAndServe (": 8000", nil)} func handler (rw http.ResponseWriter, req * http.Request) {fmt.Fprintf (rw, "Hello World,%s", req.URL.Path [1:])} basic image prepares custom minimum basic image

After reading the official document of the custom basic image, it is best to google how to build it. Tar cv-- files-from / dev/null | docker import-scratch. Use docker images to check that the size of the scratch is 0B. The basic image has been built successfully. Next, build the image in the golang program.

Build golang image and write Dockerfile

Create a Dockerfile file under the current project directory. If you use IDE to recommend installing plugins, find Dockerfile and click to install, you will be able to highlight the syntax and prompt to update it to build in phases

# define the build image environment FROM golang:alpine AS go-buildWORKDIR $GOPATH/src/backendCOPY. .Run go build-o backend# build run image FROM alpine:latest AS productionWORKDIR / root/backend/COPY-- from=go-build $GOPATH/src/backend .EXPOSE 8000ENTRYPOINT [". / backend"] build golang project image CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build-o main .docker build-t "container/echo:latest" .docker run-itd-- name echo-p 8000root/backend/COPY 8000 container/echo:latest# to check whether the container is started successfully, and failed to use docker logs to view the log docker ps-amac os startup error

Standard_init_linux.go:190: exec user process caused "exec format error"

If the above error occurs because your golang is not designated as the linux platform when build, because the Docker kernel depends on Linux development, it is still pulled by Boot2Docker when mac os starts Docker.

Thank you for reading! This is the end of this article on "how to build an image based on golang based on Mac os". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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: 234

*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

Internet Technology

Wechat

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

12
Report