Facets | Build Golang http with Alpinelinux to see how small it can be.

Source: Internet
Author: User

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.

New Wisdom Cloud www.enncloud.cn

2, first build Golang environment in CentOS

Https://golang.org/doc/install

Download and unzip:

Tar-c/usr/local-xzf go1. 7.4.linux-amd64.tar.gz Setting Environment variables: Export path=$PATH:/usr/local/go/binexportgopath=/data/go

Suppose Gopath is below the/data/go directory.

Writing Http.go

Package main import ( "FMT" "Net/http") Func handler (w http. Responsewriter, R *http. Request) {FMT. fprintf (W, "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:latest

Maintainer Demo <juest a demo>

RUN apk add --update Curl bash && \ rm-rf/var/cache/apk/*

Workdir/Data

Onbuild COPY http/data

EXPOSE 8080

entrypoint ["/data/http"]

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

4, start

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

Maintainer Demo <juest a demo>

RUN echo "Https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" >/etc/apk/repositories

RUN apk add --update Curl bash && \

rm-rf/var/cache/apk/*

Workdir/Data

Onbuild COPY http/data

EXPOSE 8080

entrypoint ["/data/http"]

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

Otherwise the error

No such file or directory

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

5, summary

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.

Facets | Build Golang http with Alpinelinux to see how small it can be.

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.