Skip to main content

Installing Containers with LXC

Hands-On Lab

 

Photo of Matthew Pearson

Matthew Pearson

Linux Training Architect II in Content

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Installing Containers with LXC

Introduction

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 hands-on lab, you are tasked with installing and configuring LXC in order to create multiple Linux containers.

Solution

  1. Begin by logging in to the lab server using the credentials provided on the hands-on lab page:

    ssh cloud_user@PUBLIC_IP_ADDRESS

Install the LXC package

  1. Install the LXC package:

    sudo apt-get install lxc

Set up the network device quota for the unprivileged user

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
  2. 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
  2. Copy the lxc default.conf file to the newly created directory:

    cp /etc/lxc/default.conf ~/.config/lxc/default.conf
  3. Ensure a UID/GID map exists in /etc/subuid and /etc/subgid for the cloud user.

  4. Copy the UID/GID map and add it to the configuration file in the user's home directory:

    vim ~/.config/lxc/default.conf
  5. Append the following (UID/GID map numbers may vary):

    lxc.id_map = u 0 231072 65536
    lxc.id_map = g 0 231072 65536
  6. 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
  2. Supply the following information to the download template’s interactive installer:

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

    lxc-create -t download -n centos
  4. 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.

  1. Start up the containers using the -d option:

    lxc-start -n centos -d`
    lxc-start -n ubuntu -d

Conclusion

Congratulations — you've completed this hands-on lab!