Facets | Built with Alpinelinux Golang http started, only 15MB

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

1, about alpine environment

The HTTP server for the next Golang was studied yesterday.

Found an error when booting:

No such file or directory

New Wisdom Cloud www.enncloud.cn found this error, began to think is Alpine system Lib library less,

You must use the official Docker Golang image.

Later the study understood that it was because my host was CentOS.

I compiled the Golang on CentOS and then copied it to the alpine environment.

Solution.

1, compile with golang:alpine image 241 MB, map a folder.

2, then copy the compiled files and put them on the Alpine mirror.

Such a 15.24 MB Golang environment is good.

Because a bash is also installed, you can enter the system to view it.

2, Operation Process

First, build a Golang build environment.

From Docker. Io/golang:alpine RUN echo "Https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" >/etc/a Pk/repositories RUN apk add --update Curl bash && \ rm -RF/var/cache/apk/* /c9>

To compile the image:

Docker build -t demo/go-build:1.0 .

Start mirroring and map the/data/go directory to the/data/go directory, where – RM indicates that mirroring is removed after exiting.

Docker run -it- v/data/go:/data/go --rm demo/go-build:1.0/bin/bash

#cd/Data/go

#go build http. Go

where Http.go file:

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)}

Excerpt from the official Httpdemo of Golang.

3, Package alpine and go http

The HTTP packets compiled in the alpine environment are then mirrored and copied to the Alpine system:

From Docker.io/alpine: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/*

RUN mkdir-p/data/go

COPY http/data/go

EXPOSE 8080

entrypoint ["/data/go/http"]

Pack and start the HTTP.

Docker build -t demo/go-http:1.0 .

Docker run- D- p 8080:8080 --name go-http demo/go-http:1.0

Direct access to Curl localhost:8080.

4, summary

This is a very good implementation, no wonder Golang claims to be the language of cloud computing design. Run directly on the Alpine 5MB system, in the installation of a bash, a total of only a total of MB, I do version upgrade, version backup is OK.

My brother, Golang really liked it.

Prepare the backstage system design with Golang rewrite it.

In fact, Java can do Golang can do it.

Java has played a JRE above the last. Look at others Golang only a few megabytes.

Everything is all up. Golang really is the language of cloud computing.

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.