Magicpak: Build minimal Docker images without static linking

栏目: IT技术 · 发布时间: 4年前

内容简介:That's it! The resulting image shall only contain what your executable requires at runtime. You can find more useful examples of

magicpak

magicpak enables you to build minimal docker images without any bothersome preparation such as static linking.

# You prepare /bin/your_executable here...

ADD https://github.com/coord-e/magicpak/releases/latest/download/magicpak-x86_64-unknown-linux-musl /usr/bin/magicpak
RUN chmod +x /usr/bin/magicpak

RUN /usr/bin/magicpak -v /bin/your_executable /bundle

FROM scratch
COPY --from=0 /bundle /.

CMD ["/bin/your_executable"]

That's it! The resulting image shall only contain what your executable requires at runtime. You can find more useful examples of magicpak under example/ .

Feature

magicpak is a command-line utility that analyzes and bundles runtime dependencies of the executable. magicpak basically collects all shared object dependencies that are required by a dynamic linker at runtime. Additionally, magicpak 's contributions are summarized as follows:

  • Simple . You can build a minimal image just by adding a few lines to your Dockerfile .
  • Full-featured . You can bundle, test, and compress your executable at once. You can focus on your business because magicpak handles all Dockerfile -specific matters to decrease image size.
  • Dynamic analysis . --dynamic flag enables a dynamic analysis that can discover dependencies other than dynamically linked libraries.
  • Flexible . We expose a full control of resulting bundle with a family of options like --include and --exclude . You can deal with dependencies that cannot be detected automatically.
  • Stable . We don't parse undocumented and sometimes inaccurate ldd(1) outputs. Instead, we use dlopen(3) and dlinfo(3) in glibc to query shared library locations to ld.so(8).

Usage

You can start with magicpak path/to/executable path/to/output . This simply analyzes runtime dependencies of your executable statically and put everything your executable needs in runtime to the specified output directory. Once they've bundled, we can simply copy them to the scratch image in the second stage as follows.

RUN magicpak path/to/executable /bundle

FROM scratch
COPY --from=0 /bundle /.

Some executables work well in this way. However, others fail to run properly because magicpak 's static analysis isn't enough to detect all files needed by them at runtime. For this case, magicpak has --include <GLOB> option to specify the missing requirements manually. Moreover, you can use --dynamic to automatically include files that are accessed by the executable during execution.

Despite our careful implementation, our analysis is unreliable in a way because we can't completely determine the runtime behavior before its execution. To ensure that magicpak collected all dependencies to perform a specific task, --test option is implemented. --test enables testing of the resulting bundle using chroot(2).

The size of the resulting image is our main concern. magicpak supports executable compression using upx . You can enable it with --compress .

Supported options

magicpak [OPTIONS] <INPUT> <OUTPUT>

    -r, --install-to <PATH>          Specify the installation path of the executable in the bundle
    -e, --exclude <GLOB>...          Exclude files/directories from the resulting bundle with glob patterns
    -i, --include <GLOB>...          Additionally include files/directories with glob patterns
        --mkdir <PATH>...            Make directories in the resulting bundle
    -d, --dynamic                    Enable dynamic analysis
        --dynamic-arg <ARG>...       Specify arguments passed to the executable in --dynamic
        --dynamic-stdin <CONTENT>    Specify stdin content supplied to the executable in --dynamic
    -t, --test                       Enable testing
        --test-command <COMMAND>     Specify the test command to use in --test
        --test-stdin <CONTENT>       Specify stdin content supplied to the test command in --test
        --test-stdout <CONTENT>      Test stdout of the test command
    -c, --compress                   Compress the executable with npx
        --upx-arg <ARG>...           Specify arguments passed to upx in --compress
        --upx <PATH or NAME>         Specify the path or name of upx that would be used in compression
        --busybox <PATH or NAME>     Specify the path or name of busybox that would be used in testing
        --cc <PATH or NAME>          Specify the path or name of c compiler
        --log-level <LEVEL>          Specify the log level
    -v, --verbose                    Verbose mode, same as --log-level Info
    -h, --help                       Prints help information
    -V, --version                    Prints version information

Docker images

We provide some base images that contain magicpak and its optional dependencies to get started.

name description
library/debian with magicpak
library/debian with build-essential , clang , and magicpak
library/haskell with magicpak
library/rust with magicpak

Example

The following is a dockerfile using magicpak for a docker image of clang-format , a formatter for C-like languages. ( example/clang-format )

FROM magicpak/debian

RUN apt-get -y update
RUN apt-get -y --no-install-recommends install clang-format

RUN magicpak $(which clang-format) /bundle -v  \
      --compress                               \
      --upx-arg --best                         \
      --upx-arg --brute                        \
      --test                                   \
      --test-stdin "int main(  ){ }"           \
      --test-stdout "int main() {}"            \
      --install-to /bin/

FROM scratch
COPY --from=0 /bundle /.

WORKDIR /workdir

CMD ["/bin/clang-format"]

Disclaimer

magicpak comes with absolutely no warranty. There's no guarantee that the processed bundle works properly and identically to the original executable. Although I had no problem using magicpak for building various kinds of images, it is recommended to use this with caution and make a careful examination of the resulting bundle.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

美铁之战

美铁之战

[英]帕特里克·蒂利 / 黑曜、超侠 / 百花文艺出版社 / 2018-9 / 44.80元

本书的故事发生在未来,一场核战毁灭了北美大陆上的人类文明,残存下来的人类分化成两拨:生活在地面上退化到刀耕火种时代的平原人;躲藏在地下苟延残喘的沙穴人。几百年后,当保留着战前文明的沙穴人尝试着登上地面,和平原人的同室操戈将不可避免地上演……一起来看看 《美铁之战》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具