Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
341

Go to list of users who liked

279

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

超軽量なAlpine Linuxについて調べた

Last updated atPosted at 2018-12-11

alpine-logo.jpg

背景

Dockerイメージの軽量化の話に出てくるAlpine Linuxについて気になったので調べてみました。

Alpine Linuxとは

組み込み系でよく使われているBusyBoxとmuslをベースにしたLinuxディストリビューション。
BusyBoxについては下記がまとまっていて見やすかったです。
http://monoist.atmarkit.co.jp/mn/articles/0802/04/news114.html

Docker界隈でよく見かける言葉ですが特にDocker用とかそういったものでは無い。

BusyBox + パッケージマネージャ(apk)がAlpineLinuxの概要とのことです。
公式サイトは下記です。

https://alpinelinux.org/
(GitHub)https://github.com/alpinelinux

Alpineの特徴に軽量とありますがどれくらい軽量化というと下記比較を見れば一目瞭然

CentOS 約4GB
Ubuntu 約700MB
Alpine 約100MB

ファイル構成

Alpine Linuxがどのような構成になっているかをdockerを使ってサクッと確認してみます。

$docker container run-it alpine /bin/ashUnable to find image'alpine:latest' locallylatest: Pulling from library/alpine4fe2ade4980c: Already exists Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528Status: Downloaded newer imageforalpine:latest/#/# ls -l /total 52drwxr-xr-x    2 root     root          4096 Sep 11 20:23 bindrwxr-xr-x    5 root     root           360 Dec 11 06:29 devdrwxr-xr-x    1 root     root          4096 Dec 11 06:29 etcdrwxr-xr-x    2 root     root          4096 Sep 11 20:23 homedrwxr-xr-x    5 root     root          4096 Sep 11 20:23 libdrwxr-xr-x    5 root     root          4096 Sep 11 20:23 mediadrwxr-xr-x    2 root     root          4096 Sep 11 20:23 mntdr-xr-xr-x  303 root     root             0 Dec 11 06:29 procdrwx------    1 root     root          4096 Dec 11 06:30 rootdrwxr-xr-x    2 root     root          4096 Sep 11 20:23 rundrwxr-xr-x    2 root     root          4096 Sep 11 20:23 sbindrwxr-xr-x    2 root     root          4096 Sep 11 20:23 srvdr-xr-xr-x   13 root     root             0 Dec 11 06:29 sysdrwxrwxrwt    2 root     root          4096 Sep 11 20:23 tmpdrwxr-xr-x    7 root     root          4096 Sep 11 20:23 usrdrwxr-xr-x   11 root     root          4096 Sep 11 20:23 var

FHSに準拠して作られている事がわかりますね。
(当たり前といえば当たり前)

/# ls -l /bin/l*lrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/link -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/linux32 -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/linux64 -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/ln -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/login -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/ls -> /bin/busyboxlrwxrwxrwx    1 root     root            12 Sep 11 20:23 /bin/lzop -> /bin/busybox

また、/bin配下はすべて/bin/busyboxへのシンボリックリンクとなっている。
busyboxとは何かというと、Alpine LinuxのベースとなっているLinuxのディストリビューション
標準UNIXコマンドで重要な多数のプログラムを単一の実行ファイルに「詰め込んで」提供する、
特殊な方式のプログラムです。詳細は下記wikiをご参照ください。
https://ja.wikipedia.org/wiki/BusyBox
busyboxのサイズもとても小さい

~# ls -lh /bin/busybox-rwxr-xr-x    1 root     root      777.6K Jul 17 15:22 /bin/busybox

デフォルトで設定されている環境変数を見るとミニマムさに驚きです。
ちなみにbashはデフォルトでないので必要な方はインストールが別途必要です。
ashというシェルが採用されています。

~# envHOSTNAME=de9c296717d8SHLVL=1HOME=/rootOLDPWD=/rootTERM=xtermPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binPWD=/root

パッケージマネージャ「apk」

パッケージやアプリケーションのインストールにはapkを使用します。
apt-getやらyumやら色々使ってると正直ごっちゃになりますが、Alpineではさらに
別のパッケージマネージャを利用します。

とりあえずupdateとaddとdelとそれらに関するオプションを覚えておけば事は足りそう。

apk help

usageが見たければここらへん。
まあよくあるパッケージマネージャーですね。
特筆して思うことはなし。

$sudoapk--helpapk-tools 2.10.1, compiledforx86_64.Installing and removing packages:  add       Add PACKAGEs to'world' andinstall(or upgrade) them,whileensuring that all dependencies are met  del       Remove PACKAGEs from'world' and uninstall themSystem maintenance:  fix       Repair package or upgrade it without modifying main dependencies  update    Update repository indexes from all remote repositories  upgrade   Upgrade currently installed packages to match repositories  cache     Download missing PACKAGEs to cache and/or delete unneeded files from cacheQuerying information about packages:  info      Give detailed information about PACKAGEs or repositories  list      List packages by PATTERN and other criteria  dot       Generate graphviz graphs  policy    Show repository policyforpackagesRepository maintenance:  index     Create repository index file from FILEs  fetch     Download PACKAGEs from global repositories to alocaldirectory  verify    Verify package integrity and signature  manifest  Show checksums of package contentsUse apk <command>--helpforcommand-specific help.Use apk--help--verbosefora fullcommandlisting.This apk has coffee making abilities.

インストール済みパッケージの参照 info

初期段階のパッケージの少なさにはほんとに驚きますね。

$apk infoWARNING: Ignoring APKINDEX.adfa7ceb.tar.gz: No such file or directoryWARNING: Ignoring APKINDEX.efaa1f73.tar.gz: No such file or directorymuslbusyboxalpine-baselayoutalpine-keyslibressl2.7-libcryptolibressl2.7-libssllibressl2.7-libtlsssl_clientzlibapk-toolsscanelfmusl-utilslibc-utils

パッケージアップデート update

/# apk updatefetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gzv3.8.1-142-g179c136670[http://dl-cdn.alpinelinux.org/alpine/v3.8/main]v3.8.1-142-g179c136670[http://dl-cdn.alpinelinux.org/alpine/v3.8/community]OK: 9548 distinct packages available

パッケージ検索 search

vimを使いたいので検索対象としています。
neovimなどもあるようですがとりあえずデフォルトでいいと思うのでそちらを入れる

/# apk search vimcharybdis-3.5.5-r4neovim-doc-0.3.0-r0vim-8.1.0115-r0faenza-icon-theme-vim-1.3.1-r4notmuch-vim-0.25.3-r1docker-vim-18.06.1-r0graphviz-2.40.1-r1neovim-0.3.0-r0vim-doc-8.1.0115-r0vim-editorconfig-0_git20170721-r0py-jinja2-vim-2.9.6-r0vimdiff-8.1.0115-r0asciidoc-vim-8.6.10-r0neovim-lang-0.3.0-r0nginx-vim-1.14.1-r0msmtp-vim-1.6.6-r3protobuf-vim-3.5.2-r0icinga2-vim-2.8.4-r0gst-plugins-base-1.14.0-r1mercurial-vim-4.6.1-r0

インストール add

上記で検索したvimをインストールしています。
=でバージョン指定もできます。
個人的にはaddというサブコマンドよりもinstallとかにしてくれたほうが覚えやすかった。。。

/# apk add vim(1/5) Installing lua5.3-libs(5.3.5-r1)(2/5) Installing ncurses-terminfo-base(6.1_p20180818-r1)(3/5) Installing ncurses-terminfo(6.1_p20180818-r1)(4/5) Installing ncurses-libs(6.1_p20180818-r1)(5/5) Installing vim(8.1.0115-r0)Executing busybox-1.28.4-r1.triggerOK: 39 MiBin18 packages/# ls -l /usr/bin/vim-rwxr-xr-x    1 root     root       2193672 Jun 25 15:52 /usr/bin/vim

パッケージ削除 del

パッケージ削除はdelを使用する。

/# apk del vim(1/5) Purging vim(8.1.0115-r0)(2/5) Purging lua5.3-libs(5.3.5-r1)(3/5) Purging ncurses-libs(6.1_p20180818-r1)(4/5) Purging ncurses-terminfo(6.1_p20180818-r1)(5/5) Purging ncurses-terminfo-base(6.1_p20180818-r1)Executing busybox-1.28.4-r1.triggerOK: 4 MiBin13 packages

ちなみにキャッシュは/var/cache/apk配下に設置されるので不要であれば削除しましょう

/# ls -l /var/cache/apk/total 1252-rw-r--r--    1 root     root        752920 Dec 11 06:50 APKINDEX.adfa7ceb.tar.gz-rw-r--r--    1 root     root        525738 Dec 11 06:50 APKINDEX.efaa1f73.tar.gz

また、リポジトリは下記ファイルへ追記することで追加する事ができます。

/# cat /etc/apk/repositorieshttp://dl-cdn.alpinelinux.org/alpine/v3.8/mainhttp://dl-cdn.alpinelinux.org/alpine/v3.8/community

まとめ

DockerのイメージにAlpineをつかう際に何も知らずにつかうとハマるらしい
Alpineの特性を抑えておく必要があるとのことなので少しでも知れたので良かったと思いました。

VirtualBoxでAlpine Linuxを構築してみるとかもいいのかなと思いました。

ちなみにashは使いにくいと思ってしまったので速攻でbashをインストールして遊んでみてます。
ベストな使い方的には正しくなさそうです。

/# apk add bash(1/5) Installing ncurses-terminfo-base(6.1_p20180818-r1)(2/5) Installing ncurses-terminfo(6.1_p20180818-r1)(3/5) Installing ncurses-libs(6.1_p20180818-r1)(4/5) Installing readline(7.0.003-r0)(5/5) Installing bash(4.4.19-r1)Executing bash-4.4.19-r1.post-installExecuting busybox-1.28.4-r1.triggerOK: 13 MiBin18 packages/# bashbash-4.4#

参考リンク

https://blog.stormcat.io/post/entry/alpine-entry-apk/
https://kakakakakku.hatenablog.com/entry/2016/04/25/223909

341

Go to list of users who liked

279
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
341

Go to list of users who liked

279

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp