Setting Up and Managing LXC Containers: A Simple Guide

Setting Up and Managing LXC Containers: A Simple Guide

Linux Containers (LXC) provide a lightweight virtualization solution that allows you to run multiple isolated Linux systems (containers) on a single host. This guide walks you through creating an LXC container, configuring networking, and managing your containers.

1. Installing and Checking LXC Version

First, ensure that LXC is installed and check the installed version:

lxc-start --version


2. Setting Up the Network Bridge

To enable networking for your containers, you need to ensure the lxcbr0 network bridge is active:

ip a | grep lxcbr0


If the bridge is down, bring it up:

sudo ip link set lxcbr0 up


3. Creating a New Container

Before creating the container, create the necessary directory structure and set appropriate permissions:

sudo mkdir -p /var/lib/lxc/ubuntu32/rootfs

sudo chown -R root:root /var/lib/lxc/ubuntu32

sudo chmod -R 755 /var/lib/lxc/ubuntu32


Now, create the container using a local template:

sudo lxc-create -n ubuntu32 -t local -- --tarball /media/lalatendu/Data/lxc-container/ubuntu-16.04-x86_64.tar.gz


Alternatively, you can extract the tarball manually:

sudo tar -xzf /media/lalatendu/Data/lxc-container/ubuntu-16.04-x86_64.tar.gz -C /var/lib/lxc/ubuntu32/rootfs


4. Configuring the Container

Open the container's configuration file:

sudo nano /var/lib/lxc/ubuntu32/config


Add the following configuration to define the container's hostname, root filesystem path, and networking:

lxc.uts.name = ubuntu32

lxc.rootfs.path = /var/lib/lxc/ubuntu32/rootfs


# Network configuration for LXC 5.x

lxc.net.0.type = veth

lxc.net.0.link = lxcbr0

lxc.net.0.flags = up

lxc.net.0.ipv4.address = 10.0.3.100/24

lxc.net.0.ipv4.gateway = 10.0.3.1

lxc.start.auto = 1


5. Starting and Managing the Container

Once configured, list the available containers:

sudo lxc-ls --fancy


Start the container:

sudo lxc-start -n ubuntu32


Attach to the container to interact with it:

sudo lxc-attach -n ubuntu32


Check container information:

sudo lxc-info -n ubuntu32


6. Debugging and Logs

If you encounter any issues, check the logs for detailed error information:

sudo lxc-start -n ubuntu32 -l DEBUG -o /tmp/lxc-ubuntu32.log


7. Restarting and Stopping the Container

To restart the LXC service and container:

sudo systemctl restart lxc

sudo lxc-stop -n ubuntu32

sudo lxc-start -n ubuntu32


If needed, restart the container directly:

sudo lxc-restart -n ubuntu32


8. Copying Files to the Container

You can copy files from the host to the container using the lxc file push command:

sudo lxc file push /path/to/local/file ubuntu32/path/in/container


For example:

sudo lxc file push /home/user/file.txt ubuntu32/root/file.txt


Verify the file inside the container:

sudo lxc-attach -n ubuntu32

ls -l /root


9. Alternative Methods for File Transfer

If SSH is configured in the container, you can use scp to transfer files:

scp /path/to/local/file user@10.0.3.100:/path/in/container


10. Mounting a Host Directory

To share a directory between the host and the container, you can mount a host directory into the container:


sudo nano /var/lib/lxc/ubuntu32/config


Add the following line to the configuration file:

lxc.mount.entry = /opt/lampp/htdocs/ /opt/ none bind 0 0


Then restart the container to apply the changes:

sudo lxc-restart -n ubuntu32


Conclusion

This guide provides a straightforward approach to setting up and managing LXC containers, from network configuration to file transfers. By following these steps, you can effectively create and manage your Linux containers on a single host.

For more advanced configurations, refer to the official LXC documentation. Happy containerizing.