Alpine's Go app, how small do you think he can be?

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

Pay rise! Pay rise! Pay rise! Nowadays Docker technology has become one of the most popular reasons for a pay rise. Haven Court is the leader in Docker's private cloud, leveraging Docker to deliver better cloud computing products, training, consulting, and more! The advent of Docker solves the problem of micro-service granularity, using Docker container technology, enabling us to develop independent code to build a very perfect microservices runtime environment where microservices are not restricted to bloated virtual machines.

When we first approached Docker, the most common way to get started was to build virtual machines using systems such as Ubuntu or Debian, the drawback of which was that the image was too large to have 187.9MB of the underlying image of Ubuntu, and that most of the installed software and dependent libraries would not be used.

The main difference between a container and a virtual machine is that the container runs inside an isolated process space in the host system, so it doesn't need to have separate kernels and other system files, so as long as you install what you need on top of it, will we need Python to run the Go service? This is not a difficult question to answer.

Micro-base image

First we want to abandon the idea of using Ubuntu or Debian as a base image, and we look at lighter choices.

Alpine Linux is a miniature Linux distribution that can be used to build a base container with a volume of only 5 m, which is based on an embedded system BusyBox transformation. Then why not just use BusyBox? There are several advantages to using Alpine, first of all, there is a package management system on Alpine, apk which can simplify the maintenance of the container, and secondly, the Alpine kernel has been patched with Pax, which can protect against a large number of 0 day vulnerabilities.

As you can see in the list below, Alpine only adds 4.1MB to the BusyBox base, but at the same time increases the convenience, which I think is worth the cost.

Mirror Volume
Ubuntu 187.90 MB
Nicholasjackson/microservice-basebox 15.82 MB
Alpine 5.24 MB
BusyBox 1.10 MB

Compiling the Go app for Alpine Linux

One advantage of using go to build a microservices is that it will be compiled into a binary package so that it does not require a framework or run dependency, which is advantageous because, as mentioned earlier, Alpine is a very lightweight distribution, not all C-dependent libraries are installed, So go's dynamic library dependencies are probably not. Fortunately, there is a special way to disable the CGO dependency, you can put the application through the link to compile, we just need to tell the compiler to rebuild all of our application packages can be:

$ CGO_ENABLED=0-a-installsuffix.

Let's talk more about the details of the above command:
Cgo_enabled=0 is a compile flag that causes the build system to ignore CGO and statically link all dependencies;
-A will force recompilation, even if all packages are compiled from the latest code;
-installsuffix Cgo adds a suffix to the newly compiled package directory to separate the compiled output from the default path.

Run the App

So in the binary package how to use the configuration file, if you are using the micro-service architecture, then you are likely to use Consul, if not, then you really should study it, it is indeed very powerful. In this article, I will assume that you are already familiar with Consul, and you are using this consul template to manage your configuration files.

We usually need to use the guardianship process to run these services, such as supervisor, but this tool needs to use Python, which violates our lightweight intent, so we use Skaware S6.

S6 is a simple and effective tool that uses /etc/s6 the following configuration file to start the S6-svscan process. You can see from my base image repository that this directory contains a series of shell scripts.

. S6-svscan

This directory contains two scripts "Crash" and "finish", and when the application managed by S6 is terminated by error, the crash script is called and the finish script is called when the S6 process is closed.

App

This directory contains "run" and "Finish" two scripts, run is used to start the main service, and will be configured on the Go app execution path. In finish, you can set the script to be executed when the application shuts down.

Consul-template

This directory also contains the two scripts, but this time it is used to configure the Consul-template application, Consul-template generated the configuration files used by the microservices, the relevant configuration files will be stored on the consul server, In the entire life cycle of Go microservices, consul applications need to run to provide configuration services, so we also have to monitor it through S6.

Docker base Image

To facilitate testing, I have used Alpine to build a mirror with Skaware S6 and Consul template, and this new image is only 15.82MB. Even if you build a bloated application based on this image, it's estimated to be around 30MB, but it's still only 13% of the size of an Ubuntu base mirror.

Conclusion

In this article, Haven Court mainly discusses go applications, but can be used on Ruby or Python based on similar techniques, and the Alpine apk already provides the dependent packages for both languages, and you can also try to deploy a JRE environment on top.

For more information on Docker, please watch the training video: https://csphere.cn/training!

If you need Docker related products, please visit Haven Court homepage: https://csphere.cn!

cSphere1.0 version has been released, officially commercial! Welcome to Inquire 400-686-1560

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.