Keegan Turner
  • πŸ‘‹Hello World.
  • πŸ“šHow-To Guides
    • Integrating Git on your local host
    • βš–οΈLoad Balancing with HAProxy
    • πŸ‘¨β€πŸ’»Code Server - Self Hosted VSCode alternative
    • πŸ•ΈοΈWordPress + LAMP - Self Hosted CMS
    • ☁️Nextcloud - Self Hosted file sync & sharing solution
    • ⏳Grafana Cloud - Cloud Hosted Monitoring
    • βŒ›Prometheus + Grafana - Self Hosted monitoring stack solution
    • πŸ›³οΈPortainer + Docker - Self Hosted container management UI
    • 🐬MariaDB - Self Hosted database server
    • πŸ€–Let's Encrypt + Certbot - Secure your web services with SSL certificates
    • ◀️Nginx Proxy Manager - Self Hosted domain controller with LE SSL certificates
    • πŸ–₯️LXD - Linux OS containers
    • 🐧Linux basics: commands
    • 🐧Linux basics: file paths
    • 🐧Linux basics: SWAP file
  • 🚫Bytes of Caution
Powered by GitBook
On this page
  1. How-To Guides

LXD - Linux OS containers

LXD containers are lightweight virtual machines that provide an environment similar to a virtual machine but with lower overhead. They allow users to run multiple isolated Linux systems (containers) on a single host, leveraging the Linux kernel's capabilities for resource isolation and virtualization. LXD extends LXC (Linux Containers) by providing a more user-friendly interface, advanced management features, and enhanced security.

Step 1 - Launch instance

Create an instance on your Cloud Provider to serve as the host system for the containers

I am using Ubuntu 24.04 as my host operating system and the instance has 4GB ram with 2CPUs.

Step 2 - Update the host system

sudo apt update 
sudo apt dist-upgrade

Step 3 - Install LXD

snap install lxd
lxd init 

Follow the prompts on screen

Step 4 - Launch container

lxc launch ubuntu:24.04 ubuntu-lxc-container-01

I repeated the above process to launch four LXD containers

To list all the running containers:

lxc list 

You can access a container directly with the below command:

lxc exec 'container name' -- /bin/bash

This will open a terminal session directly in the running container, where you can then install the require packages and applications in an isolated Linux virtual environment.

I have installed Apache webserver and will use the first container to build and host a static web application.

Since the container does not have a public IP address, as it runs within another host, I can access its services by mapping the container's service port to an open port on the host system. This allows me to use the host's IP address and the assigned port number to access the service in the container.

To access the static web page hosted in the container, I mapped port 81 on the host system to port 80 (the default port for HTTP traffic) on the container and opened port 81 on the host system firewall. Now, I can directly access the static web page from my web browser using http://ipaddress:81.

See the common LXD commands below:

  1. lxc launch Creates and starts a new container from an image.

    lxc launch <image> <container-name>

  2. lxc init Initializes a new container without starting it.

    lxc init <image> <container-name>

  3. lxc start Starts an existing container.

    lxc start <container-name>

  4. lxc stop Stops a running container.

    lxc stop <container-name>

  5. lxc restart Restarts a running container.

    lxc restart <container-name>

  6. lxc delete Deletes a stopped container.

    lxc delete <container-name>

  7. lxc list Lists all containers with their status.

    lxc list

  8. lxc info Shows detailed information about a specific container.

    lxc info <container-name>

  9. lxc config Manages container configuration options.

    lxc config set <container-name> <key> <value>
    lxc config show <container-name>

  10. lxc exec Executes a command inside a running container.

    lxc exec <container-name> -- <command>

  11. lxc file Manages files inside containers.

    lxc file pull <container-name>/<path> <host-path>
    lxc file push <host-path> <container-name>/<path>

  12. lxc snapshot Takes a snapshot of a container.

    lxc snapshot <container-name> <snapshot-name>

  13. lxc restore Restores a container from a snapshot.

    lxc restore <container-name> <snapshot-name>

  14. lxc copy Copies a container or snapshot to a new container.

    lxc copy <source-container> <target-container>

  15. lxc network Manages container networks.

    lxc network list
    lxc network attach <network> <container-name> <device>

LXD containers offer a powerful and efficient solution for running isolated Linux environments. Their lightweight nature, combined with the ability to manage multiple containers effortlessly, makes them ideal for developers, system administrators, and enterprises looking to optimize their infrastructure. With LXD, you gain the flexibility, scalability, and performance needed to handle diverse workloads while maintaining a high level of security and resource efficiency. Embracing LXD containers can significantly enhance your system's capabilities and streamline your operations, making it a valuable addition to any tech toolkit.

PreviousNginx Proxy Manager - Self Hosted domain controller with LE SSL certificatesNextLinux basics: commands

Last updated 11 months ago

πŸ“š
πŸ–₯️
List of running containers
Apache webserver running in LXC container
Apache default page customized and accessible on the container
Page cover image