The big news in the Docker world of late is the announcement of LinuxKit, which Docker developers created in conjunction with Intel, IBM, ARM and Microsoft. LinuxKit is a modular, compact, secure and portable framework for building tiny Linux-based operating systems that you can run inside a Docker container to serve as an application host.
Below, I provide a hands-on look at LinuxKit. I’ll explain in more detail what it is, then walk through an example of using it to create a Docker image.
What is Linuxkit?
LinuxKit, as its name implies, is a set of tools for creating minimal Linux distributions. It is developed inside a container and makes the notes of its dependencies inside this same container. Initially it seems a bit confusing, but once you understand how LinuxKit works, you'll realize it's a cool idea.
LinuxKit is minimized Linux that contains dependencies that will only be used according to the configuration that will be made in the generation of a distribution. This distribution can be run in a cloud like AWS, Azure and Google Cloud.
For those who have been using Linux for a long time, LinuxKit recalls the time when we recompiled the kernel to create Linux distributions. The difference is that with LinuxKit, this is done on a large scale, much more easily, without constant fear of upgrading the kernel and causing the distribution to stop working, or to start presenting compatibility problems with the dependencies used so that a new distribution has to be recompiled.
LinuxKit works with Moby, which will convert the yaml file into a bootable image. Yaml is a people-readable data encoding file, similar to XML.
Creating a LinuxKit image
We will create an image with LinuxKit using Docker. The steps below were performed on a computer running the MacOS Sierra operating system. Before we start, we need:
- A Docker account
- The Edge edition of Docker for Mac installed.
To run Docker:
$ docker run dockercloud/cli -h
$ brew install docker-cloud
Check the installed version of Docker:
$ docker-cloud -v
Download the LinuxKit:
$ git clone https://github.com/linuxkit/linuxkit.git
Access the LinuxKit folder:
$ cd linuxkit
Copy moby to your bin folder:
$ cp bin/moby /usr/local/bin/
Log into Docker:
$ docker login
Type your username, password and email, and list the files inside the LinuxKit folder:
Access the examples folder:
$ cd examples
This folder has examples that can be used initially for learning. in the future, they can be used as examples for creating new images.
As you can see, I’ve created an image file using the command:
$ moby build swap.yml
Adding the LinuxKit image to Docker
At this point, the swap.iso file has been generated, which can be used for the pull in Docker through the command:
$ docker image pull swap.iso
Now your image is in the cloud, and can be run from there. Below are the contents of the swap.yaml file to be analyzed, with some important comments to make it easier to understand:
- kernel: Contains a version of the kernel and the system modules.
- init: The base init of the system, decompressed as the base system.
- onboot: Containers of the systems executed sequentially.
- services: Services that run while running the active system.
- outputs: Creation process on different platforms, such as ISO.
cmdline: "console=ttyS0 console=tty0 page_poison=1"
- name: sysctl
- name: binfmt
- name: dhcpcd
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
- name: mount
rootfsPropagation: sharedcommand: ["/mount.sh", "/var/external"]
- name: swap
# to use unencrypted swap, use:
# command: ["/swap.sh", "--path", "/var/external/swap", "--size", "1G"]
command: ["/swap.sh", "--path", "/var/external/swap", "--size", "1G", "--encrypt"]
- name: rngd
- name: nginx
- format: kernel+initrd
- format: iso-bios
- format: iso-efi
As we can see in the code above, the kernel, init, onboot, services and output lines are where we will receive the changes we want. The whole file can be rewritten any way we’d like. With Linux, everything inside it is a file and can be edited.
As we can see, LinuxKit is not as complicated as it seems. On the contrary, it is a robust and extremely important, versatile tool. We can create a system based on Linux easily, with the possibility to write the whole system. In the example provided in this article, after writing the code, we asked to generate an image so that we can climb to the cloud inside Docker. And once inside Docker, LinuxKit runs and calls the dependencies that were written to the system, and that's it. Docker takes care of the rest, and the system is extremely lean.
About the Author
Brena Monteiro is a software engineer with experience in the analysis and development of systems. She is a free software enthusiast and an apprentice of new technologies.
We’re hiring! Check out the careers page for open positions in Amsterdam, London and San Francisco.
As usual, if you want to stay in the loop follow us on twitter @wercker or hop on our public slack channel. If it’s your first time using Wercker, be sure to tweet out your #greenbuilds, and we’ll send you some swag!