How to create Linux containers

Maen ArtimyJanuary 14, 2018January 28, 2018Comments Off on How to create Linux containers

Linux containers isolate applications from the host system that they run on and from other containers. Containers behave much like virtual machines, but unlike virtual machines, containers include only the software components they need to operate rather than an entire operating system. This improves performance and reduces the size of the application.

I am going to show you how to create a privileged container. Privileged containers are containers created by root and running as root.

Prerequisites

A physical Linux machine with at least one Ethernet port, or a virtual machine with bridged network connection. I’m using an Ubuntu VM.

Installing LXC

On an Ubuntu system, install LXC:

$ sudo apt-get install lxc lxc-templates

LXC Networking

There are five types of container networking. We will try two of them: veth and vlan. First, we want to disable the default bridge “lxcbr0“ that is created as part of LXC installation.

$ sudo nano /etc/default/lxc-net

Set “USE_LXC_BRIDGE” to “false”.

Creating a Container with veth Network Type

The veth network type veth connects the container to a Linux bridge, therefore you need to configure a bridge first. Start by editing the file “/etc/network/interfaces/” to add a bridge name of your choice.

Note that in the above configuration, we attached interface ens33 to the bridge. Your system may have a different interface available, such as eth0. Edit the file “/etc/lxc/default.conf” to include the name of the bridge that you’ve defined.

Now you can create a container from an Ubuntu template (there are also other templates to choose from):

$ sudo lxc-create -n cont_a -t ubuntu

Executing the command for the first time takes a few minutes. Once the container is created, take note of the username and password that are displayed. You will need these to access your container.

...
Current default time zone: 'Etc/UTC'
Local time is now: Sun Jan 14 20:22:42 UTC 2018.
Universal Time is now: Sun Jan 14 20:22:42 UTC 2018.
##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##

Issue the following command to check everything is okay to run containers.

$ sudo lxc-checkconfig

Start the container using the command:

$ sudo lxc-start -n cont_a -F

Once inside the container, you can exit using:

ubuntu@cont_a:~$sudo poweroff

Following the above steps allows the container to obtain its address via DHCP. To change this behavior, edit the file “/var/lib/lxc/cont_a/config” to add static IP address configuration:

Creating a Container with VLAN Network Type

Using the vlan network type allows the network traffic from the container to be tagged with a VLAN ID. If you create multiple containers, each of them can have a different VLAN ID. You will need to connect your physical interface to a managed switch to separate the VLANs traffic.