NEW: Micro containers based on Alpine Linux
For perspective the WordPress container based on Debian Wheezy is around 170MB compressed and takes up around 620MB disk space when uncompressed. The WordPress micro container in contrast is a mere 23MB compressed and takes 145MB disk space uncompressed.
There are huge savings in space and bandwidth with zero tradeoff in performance or functionality. All this is of course thanks to Alpine Linux which is a lightweight, network focused and security oriented distribution.
To start with Nginx and WordPress micro containers are available and we will expand that depending on user response. The Nginx micro container is a mere 4.3MB download and takes up a minuscule 18MB disk space.
Following the Flockport convention the container root password is the name of the distribution ie alpine. Please change this with the 'passwd' command on first login.
Alpine Linux
Alpine Linux is based on musl libc and driven by the need to keep things simple and lightweight. It uses OpenRC for init and APK for package management. For those used to Debian its quite similar in many ways.
It has a number of surprisingly useful and nifty features that makes it suitable both as a container or VM host and a container OS.
As a host it’s extremely secure and uses a grsec kernel by default, supports diskless installs with a nifty use of apk package management called LBU and has a fairly healthy collection of packages available.
As a container OS its lightweight nature makes it perfect for containers where you need a lightweight execution environment for your apps. The preview containers gives users a taste of the Alpine Linux environment.
Quick overview of Alpine
Alpine is fairly similar to Debian in most functions. Let's quickly go over basic services and package management here to get your familiar with the basics.
Alpine uses OpenRC as its init. To get a list of currently running services use rc-status.
rc-status
To start or stop a service use rc-service for instance
rc-service nginx restart
You can also use service nginx restart.
To add a service to the default run level
rc-update add nginx default
To add and remove packages you use the apk program. For instance to update the repos.
apk update
To add programs
apk add nginx
To uninstall programs
apk del nginx
The repositories like apt are in /etc/apk and the cache is in /var/cache/apk. You can add and delete user and groups with the addgroup, adduser and delgroup, deluser commands.
Its fairly simple and straight forward. For more head over to the Alpine wiki and tutorials website.
Create your own Alpine Linux container
You need to be familiar with LXC before attempting this so head over to our LXC Getting started guide.
lxc-create -t alpine -n p1 -- -R v3.l
To create a 32 bit container append -a i386 after the double hyphen to the lxc-create command.
The Alpine Linux OS template does not have networking enabled by default. To enable it first edit the container config file and add the networking bits.
Usually the values below is what you need to enable the container networking if you are using the default LXC networking with lxcbr0 bridge.
lxc.network.type = veth lxc.network.flags = up lxc.network.link = lxcbr0 lxc.network.name = eth0 lxc.network.hwaddr = 00:16:3e:xx:xx:xx
But its much better to add this values to /etc/lxc/default.conf so lxc-create populates the networking values in the config file automatically every time it creates a container.
Note: If you add the values manually to the container config file you need to replace the mac address 'xx' bits with random alphanumeric characters. If you add it to /etc/lxc/default.conf lxc-create is smart enough to automatically generate values for the 'xx' bits.
With that done you can start the container
lxc-start -n p1 -d
Networking is not yet setup inside the container so you will need to use lxc-console to enter the container.
lxc-console -n p1
Login with user root. There is no password set by default. Change it with the passwd command. Once inside check run ifconfig to see if eth0 is up. It should be up but without an IP. Check that /etc/networking/interface file reads as below for eth0
auto eth0 iface eth0 inet dhcp
Now start the networking service
rc-service networking start or /etc/init.d/networking start
This should give you an IP. Check with ifconfig. Once you have confirmed that works add the networking service to boot.
rc-update add networking default
Now setup apk and openssh so you can log into the container normally. Alpine has scripts for these you can run.
setup-apkrepos setup-sshd
Now poweroff and exit the container. Start the container again and lxc-ls should show networking working with the container getting an IP and you should be able to login with ssh.
Note: When setting up we usually use lxc-console. lxc-attach works most of the time but there are rare occasions most likely due to namespace issues when some service or app will error out inside the container, so its often better to use lxc-console and ssh to enter the container just to be sure.