Installing Containers with LXC

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

LXC, which stands for Linux Containers, is a containerization solution that takes advantage of existing kernel features (such as namespaces and cgroups) to create container environments that are as close as possible to normal Linux installations, without the overhead of simulating hardware or running multiple kernels. In this learning activity, you are tasked with installing and configuring LXC in order to create multiple Linux containers.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Install the LXC package.

# sudo apt-get install lxc

Set up the network device quota for the unprivileged user, `cloud_user`, to include 10 veth devices.
  1. Edit /etc/lxc/lxc-usernet:

    # sudo vim /etc/lxc/lxc-usernet

  • Add the following line:

    USERNAME veth lxcbr0 10

Copy the LXC configuration file

Copy the LXC configuration file to the cloud_users’s home directory and append the uid and gid map for the cloud_user to the file. Once completed, be sure to log out of the lab server and log back in to activate the new configuration.

  1. Create .config/lxc in the users home directory:

    # mkdir -p ~/.config/lxc

  • Copy the lxc default.conf file to the newly created directory:

    # cp /etc/lxc/default.conf ~/.config/lxc/default.conf

  • Ensure a uid/gid map exists in /etc/subuid and /etc/subgid for the cloud user.

  • Copy the uid/gid map and add it to the configuration file in the user’s home directory:

    # vim ~/.config/lxc/default.conf

  • Append the following (uid/gid map numbers may vary):

    lxc.id_map = u 0 231072 65536
    lxc.id_map = g 0 231072 65536
  • Once this step is completed, log out of the lab server and log back in to activate the new configuration file.

Create a container

Create a container named ubuntu running Ubuntu Xenial and a container named centos running CentOS 7; both should use the amd64 architecture.

  1. Create the ubuntu container using the download template:

    # lxc-create -t download -n ubuntu

  • Supply the following information to the download template’s interactive installer:

    Distribution: ubuntu
    Release: xenial
    Architecture: amd64
  • Create the centos container using the download template:

    # lxc-create -t download -n centos

  • Supply the following information to the download template’s interactive installer:

    Distribution: centos
    Release: 7
    Architecture: amd64
Start up the newly created servers as daemons running in the background.

Start up the containers using the -d option:

# lxc-start -n centos -d

# lxc-start -n ubuntu -d

To see that both of the containers are running run
lxc-ls -f

Additional Resources

The data center at your company is approaching max capacity. After an analysis of the resource usage, it is discovered that most of the systems are underutilized. In an effort to make more efficient use of the current infrastructure, the decision has been made to implement Linux containers. As one of the lead engineers, you have been put in charge of this initiative. Your first task is to set up a host machine running LXC as a proof of concept. Due to security concerns, the decision has been made to use unprivileged containers. For the proof of concept, you will need to install and configure LXC for the cloud_user user. Once LXC has been configured, you will need to create two containers - one that is running Ubuntu Xenial and the other that is running CentOS 7 (both should use amd64 architecture) - and ensure that they are running in the background as daemons.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?