Getting started with LinuxKit and Moby Project

Aishik Saha
Aishik Saha
Published in
3 min readMay 9, 2017

You’ve probably read about LinuxKit and the Moby Project. If you haven’t it’s recommended that you do before we start.

Moby is a framework to assemble specialized container systems. It has a library of containerized components and a framework for assembling these components into a standalone container platform. Currently Docker is being broken into modular components. In the future Docker will be assembled from these components that are packaged by Moby.

LinuxKit is a toolkit for building minimal Linux distributions. It uses Moby to build the distro images and the LinuxKit tool to run them. It is designed to be secure by default.

Now lets get started. You’ll need to have Docker and Golang installed. If you need help then read Installing Docker and Installing Golang.

1. Install the Moby build tool.

$  go get -u github.com/moby/tool/cmd/moby

2. Install the LinuxKit tool.

$  go get -u github.com/linuxkit/linuxkit/src/cmd/linuxkit

3. Change directory to the LinuxKit repository. This will be inside your Golang installation directory.

$  cd /usr/local/go/src/github.com/linuxkit/linuxkit

Can’t find the LinuxKit repository? No problem. Just clone the repository from GitHub again. $ git clone https://github.com/linuxkit/linuxkit

4. Build the example configuration file ‘ linuxkit.yml ’.

$  moby build linuxkit.yml

This command pulls a Linux kernel, adds the components mentioned in the YAML file and builds a standalone Linux distribution. If you get any errors just run the command again. I had to run it 5 times. Here’s the output.

root@Aishik:~/work/src/github.com/linuxkit/linuxkit# moby build linuxkit.yml
Extract kernel image: linuxkit/kernel:4.9.x
Add init containers:
Process init image: linuxkit/init:f71c3b30ac1ba4ef16c160c89610fa4976f9752f
Process init image: linuxkit/runc:b0fb122e10dbb7e4e45115177a61a3f8d68c19a9
Process init image: linuxkit/containerd:60e2486a74c665ba4df57e561729aec20758daed
Process init image: linuxkit/ca-certificates:5fc6ba7f91534ddbfef975404c33e44581e6ed7a
Add onboot containers:
Create OCI config for linuxkit/sysctl:2cf2f9d5b4d314ba1bfc22b2fe931924af666d8c
Create OCI config for linuxkit/binfmt:aebd6ed6cc29921371ca78314697881086f4577a
Create OCI config for linuxkit/dhcpcd:8837289b78ecd80f59524883085424e115dd0b3a
Add service containers:
Create OCI config for linuxkit/rngd:c42fd499690b2cb6e4e6cb99e41dfafca1cf5b14
Create OCI config for nginx:alpine
Add files:
etc/docker/daemon.json
Create outputs:
linuxkit-kernel linuxkit-initrd.img linuxkit-cmdline
linuxkit.iso
linuxkit-efi.iso

5. Run the Linux image created in the previous step.

$  linuxkit run linuxkit

This command boots into the Linux image created by Moby. Here’s the output. It’s a minimal Linux distro. You can type commands and try it out. Use the halt command to terminate the console.

On some careful observation our minimal distro turned out to be Alpine Linux. See it yourself using the following command.

/ # cat /etc/alpine-release

It gives the following output.

cat /etc/alpine-release
3.5.2

Next we can see what’s installed in our minimal system.

/ # apk info

musl
busybox
alpine-baselayout
alpine-keys
libressl2.4-libcrypto
libressl2.4-libssl
zlib
apk-tools
scanelf
musl-utils
libc-utils

So we successfully built our own minimal Linux distribution using LinuxKit and Moby. You can edit the YAML configuration file as per your needs and build secure and lean Linux distros to run your containerized applications on.

LinuxKit and Moby are great but do you need it? If you need to build custom Linux distributions then this is super and saves you a ton of time. But if you’re just looking to run your apps on containers then you don’t have to necessarily tinker around with Moby. Docker will work fine for you.

Resources:
Docker Blog — Introducing Moby Project
Docker Blog — Announcing LinuxKit
GitHub — LinuxKit
Moby Project — Official Site

Thanks for reading this post. Spread some love by clicking the 💚 button. Follow me on Twitter to stay updated on containers and the cloud.

--

--

Aishik Saha
Aishik Saha

Engineer. Interested in machine learning, cloud, containers and open source. Built Slyme (https://5ly.me).