In my previous blog post, I mentioned why I moved from Docker to LXC (reference: here ).
I also mentioned 7 basic commands you will need to get started with LXC. I will list them and walk-through each below.
Let us start by creating and starting a container:
sudo lxc-create -n myfirstcont -t ubuntu sudo lxc-start -n myfirstcont -d
The first command will create a container called
myfirstcont from the remote Ubuntu template.
After the first command has successfully executed, you can start the container using the second command (lxc-start).
Next, we will get into the container:
sudo lxc-attach -n myfirstcont
This command will take you into the container as the root user.
Once you are in the container, you can work with it as you normally would in a regular Ubuntu OS (although a number of regular/standard packages may not be present).
Now that we started our container and we have also managed to get within the container (via lxc-attach), we may need to obtain some information about the container to work with it externally.
A common example is when we start an HTTP-server within the container (and bind the server to the 0.0.0.0 IP). To obtain the IP-address assigned to the container (as well as other basic information), we will use:
sudo lxc-info -n myfirstcont
You will get a basic amount of information using the
The next 2 commands will stop and destroy your container.
sudo lxc-stop -n myfirstcont
sudo lxc-destroy -n myfirstcont
The container itself will be destroyed, but your template (somewhat similar to a Docker image) will still exist.
The last command is to list all containers on your system. The command will list all containers, regardless of state:
One important thing to mention about this tutorial is that we are using the default LXC instructions and creating priviledged containers by using
sudo. I recommend reading up about the priviledged/unpriviledged aspects of containers here .
In my first post, I mentioned that mounting from the host > container or vice-versa took a bit of time to figure out. For the purposes of simplicity and ease-of-use for anyone reading this, here is a set of commands to use for mounting.
sudo mount --bind /var/lib/lxc/myfirstcont/rootfs/home/ubuntu/myfolder/ /Project/path/to/hostfolder
sudo mount --bind /Project/path/to/hostfolder /var/lib/lxc/myfirstcont/rootfs/home/ubuntu/myfolder
Installing small Python library in a container
If you have not destroyed the container you created above, let us go ahead and restart the container:
sudo lxc-start -n myfirstcont -d sudo lxc-attach -n myfirstcont
You should be attached as the root-user within the container.
apt-get update apt-get upgrade apt-get install -y python3-pip pip3 install pelican markdown
You have now successfully created an LXC container for the static site-generator Pelican .
LXD as an easier LXC-manager
In the final part of this blog post, I would like to mention LXD .
Although the link above explains things better than I can, LXD is a very useful LXC-manager. It provides unpriviledged containers by default and uses the Docker image/container approach (instead of templates).
LXD provides a host of other features that would be useful for large-scale container-management.
I would strongly urge anyone interested in using LXC-containers in production to consider using LXD to manage your infrastructure.