Module 27 - Custom Containers¶
Historical miniclass
This lesson was recovered from the former Sandia minimega site. It is preserved for reference and may describe obsolete software, operating systems, commands, or external resources. Consult the current documentation before applying it.
Introduction¶
We used a prebuilt filesystem in a prior module. Let's make custom filesystems and boot them.
WARNING Here be Dragons: When you build container filesystems with Docker and LXC you are diving into a realm of things I wouldn't expect to be supported.
You may find better luck running Docker and LXC containers in a nested manner in a KVM.
Adding static binaries to containerfs¶
cd ~
wget https://storage.googleapis.com/minimega-files/minimega-2.2-containerfs.tar.bz2
tar xf minimega-2.2-containerfs.tar.bz2
Create a go web server:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Server running...")
http.ListenAndServe(":8080", nil)
}
Compile the go web server to a binary
Copy this to containerfs:
Edit /home/ubuntu/containerfs/init
Before bash run the webserver binary
Booting the container filesystem¶
minimega -attach
$ vm config filesystem /home/ubuntu/containerfs/
$ vm config net 0
$ vm config snapshot true
$ vm launch container custom-cfs
$ vm start custom-cfs
There should be a web server running on port 8080 on the container.
Building a custom containerfs with shared libraries¶
The minirouter container has a clean build script you can tweak for your application.
github.com/sandia-minimega/minimega/blob/master/misc/uminirouter/build.bash
In summary:
- A basic file structure is made
- A 64-bit binary of busybox is downloaded.
- Symbolic links for busy box compiled programs are created
- The
PATHvariable is updated - Init and Preinit are copied
- Binaries are copied from the host
- The program
lddis used to locate shared libraries and they are copied over - Scripts get tweaked to use
/bin/sh - A root user is created with no password
- And a package is made.
LXC¶
This downloads and generates a root file system in /var/lib/lxc/ubuntu/rootfs/ of Ubuntu 16.04
Before we can boot this we need to add an init
cat > /var/lib/lxc/ubuntu/rootfs/init << EOF
#!/bin/sh
ifconfig lo up
ifconfig veth0 up
dhclient -v veth0
/usr/sbin/sshd &
bash
EOF
Now we need to fix the permissions on init
sshd needs a folder that doesn't already exist, let's fix that:
Booting the container filesystem¶
minimega -attach
$ vm config filesystem /var/lib/lxc/ubuntu/rootfs/
$ vm config net 0
$ vm config snapshot false
$ vm launch container ubuntu1604
$ vm start ubuntu1604
The mega_bridge adapter is configured in a bridged adapter manner which you can do following the later networking modules.
Any changes you make will be permanent, including applications you download from apt-get.
In order to get apt-get working you will need to fix the PATH variable with:
export PATH=$PATH:/usr/local/sbin/
export PATH=$PATH:/usr/sbin/
export PATH=$PATH:/sbin
export PATH=$PATH:/usr/local/bin/
export PATH=$PATH:/usr/bin/
export PATH=$PATH:/bin
I also needed to set a nameserver with:
From there apt-get worked and I was able to install packages like python and nano.
I typed exit and changed to snapshot true and booted 50 of these containers using <440mB RAM total
Docker¶
Install Docker
www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
Download the Docker Ubuntu image.
And run the image
From the Docker container's /bin/bash
Open another terminal:
Export the filesystem
Create an init file
Now we need to fix the permissions on init
Booting the container filesystem¶
minimega -attach
$ vm config filesystem /home/ubuntu/dubuntu/
$ vm config net 0
$ vm config snapshot true
$ vm launch container dubuntu1604
$ vm start dubuntu1604
Special Notes¶
There is no /dev/stdout or /dev/stderr so any image using those will need to be modified.
Depending on your base image, it may be difficult to install other packages, like dhclient.
Containers are still pretty experimental.