skip to content
shipanjodder.com

Install LXC Container on Linux System

Updated:

Installing LXC:

  • Ubuntu/Debian System
bash
sudo apt update
sudo apt install lxc lxc-utils lxc-templates -y
  • Centos System
bash
sudo yum install epel-release -y
sudo yum install lxc lxc-utils lxc-templates -y

Verify LXC Installation:

bash
lxc-checkconfig

Enable LXC Service:

bash
sudo systemctl enable --now lxc

Create a New LXC Container:

bash
sudo lxc-create -n mycontainer -t ubuntu
  • Replace mycontainer with the name of your container.
  • The -t ubuntu flag specifies an Ubuntu template.

If you get an error about missing images,

Update the template cache:

bash
sudo lxc-create -t download -n mycontainer -- -d ubuntu -r focal -a amd64
  • -d ubuntu: Distribution like centos, debian, busybox
  • -r focal: Ubuntu version (change to your preferred version)
  • -a amd64: Architecture

Managing LXC Images

List Available LXC Images on Local:

  • images stored locally
bash
lxc image list local:
  • if using traditional LXC
bash
ls /var/lib/lxc

List Remote Images from the LXC Repository:

bash
lxc image list images:

Show detailed information about an image

bash
lxc image info <image-alias-or-fingerprint>

Replace the image name/fingerprint, example: abcd1234

Remove an LXC Image

bash
lxc image delete <image-alias-or-fingerprint>

Remove All Unused Images

bash
lxc image prune

Manually Delete an Image (If Needed)

bash
rm -rf /var/lib/lxd/<image-name>

or

bash
rm -rf /var/lib/lxd/images/<image-name>
  • Refresh LXC Images:
bash
lxc image refresh --all

Managing LXC Containers

List Available Containers

bash
lxc-ls --fancy

Start a Container

bash
lxc-start -n mycontainer -d

(-d runs it in the background)

Stop a Running Container

bash
lxc-stop -n mycontainer

Restart a Container

bash
lxc-restart -n mycontainer

Destroy (Delete) a Container

bash
lxc-destroy -n mycontainer

Interacting with Containers

Attach to a Running Container

bash
lxc-attach -n mycontainer

Run a Command Inside a Container

bash
lxc-attach -n mycontainer -- bash

Check Container Status

bash
lxc-info -n mycontainer

Show Container Logs

bash
lxc-info -n mycontainer --logfile /var/log/lxc/mycontainer.log

Networking and IP Address Management

List Container IP Addresses

bash
lxc-ls --fancy

Manually Assign an IP Address

Edit the Container’s Configuration File

bash
nano /var/lib/lxc/<container-name>/config

Add the Following Network Configuration

lxc.net.0.ipv4.address = 192.168.1.100/24
lxc.net.0.ipv4.gateway = 192.168.1.1

Replace ip(192.168.1.100) & gateway(192.168.1.1)

Enable NAT (Network Address Translation)

bash
sudo apt install iptables; \
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Forward a Port to a Container

bash
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 10.0.3.20:80

Replace destination port(8080), host port(80) & internal ip(10.0.3.20)

  • (This forwards traffic from 8080 on the host to port 80 in the container).