Docker (8): Build Golang http with Alpinelinux see how small it can be

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1,alpine Linux is very small

First Alpine is very small, installed on the bash after the 5MB.
Golang don't need any other dependencies to see if they can run on Alpine.
Build a Golang environment, rather than putting the golang environment on the Alpine.

2, first build Golang environment in CentOS

Https://golang.org/doc/install
Download and unzip:

tar -C /usr/local -xzf go1.7.4PATH=$PATH:/usr/local/go/binGOPATH=/data/go

Suppose Gopath is below the/data/go directory.
Writing Http.go

package mainimport (        "fmt"        "net/http")func handler(w http.ResponseWriter, r *http.Request) {        fmt.Fprintf"Hi there, I love %s!", r.URL.Path[1:])}func main() {        http.HandleFunc("/", handler)        http.ListenAndServe("0.0.0.0:8080", nil)}

Very simple.
Go Build Http.go
./http

3, write the Docker build

VI Dockerfile

FROM       docker.io/alpine:latestMAINTAINER demo <juest a demo>RUN--update curl bash && \    rm -rf /var/cache/apk/*WORKDIR /dataONBUILDCOPY http /dataEXPOSE8080ENTRYPOINT ["/data/http"]

Created a minimal docker mirroring environment. Then set the source to Tsinghua. and the curl and bash tools are installed.

4, start

demo/go-http         1.0                 b180c236066d        11seconds ago      9.555 MB

There are only 10mb of containers, very small.
But the reality is brutal. Because the Alpine is too streamlined. Cause Golang not to get up.
See a kernel Linux is not bootable Golang.
The final dockerfile:

FROM       docker.io/golang:latestMAINTAINER demo <juest a demo>RUN"https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositoriesRUN--update curl bash && \    rm -rf /var/cache/apk/*WORKDIR /dataONBUILDCOPY http /dataEXPOSE8080ENTRYPOINT ["/data/http"]

Or use the Docker's official Golang environment in order to start.
Otherwise the error

fileordirectory

The executable file could not be found. The container cannot be started.

5, summary

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/53615757 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

Eventually the Golang was launched in an official environment.
Didn't figure out what the Golang really needed.
Final size is 679.6 MB
I thought 10mb would solve the problem. It seems to be a good exploration.
Other environments can also be studied.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.