How to set up an LXC Container in Ubuntu Linux.

How to set up an LXC Container in Ubuntu Linux.

What is LXC?

A container allows us to isolate one or more processes from the underlying system while sharing common functions. For example, we can create a web server in a container that serves up a web page. When a new version of the website becomes available, we can replace the original container without needing to worry about the underlying system. They are a great way of isolating software dependencies to only the processes that need them, and in my case, a great way of spinning up a test environment that can be destroyed when no longer needed.

 LXC - which stands for Linux Containers, are an open source implementation of the container concept. Linux containers contains two implementations LXD and LXC, we're not going to discuss LXD in this quick article, we'll just focus on LXC.

 What we're trying to do:

No alt text provided for this image

Assuming you have an Ubuntu 20.04 system, we'll need to install LXC first.


$ sudo apt-get install lxc


You may need to initialize some items, for now I have everything set to default

$ lxc init


After that's completed, create and launch a container by running

lxc launch ubuntu:20.04 ny-new-container


If everything has been successful, you should be able to see your new container by running


$ lxc list
+------------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|      NAME      | STATE |        IPV4        |                    IPV6                     |  TYPE   | SNAPSHOTS |
+------------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| my-new-container | RUNNING | <snip> (eth0)       | <snip> (eth0)                                | CONTAINER | 0        |
+------------------+---------+----------------------+-----------------------------------------------+-----------+-----------+


You can copy files into the container by running lxc push.

$ ls files_to_send/
hello_world.txt

$ lxc file push ~/files_to_send/hello_world.txt my_new_container/root/


You can attach into the container by running

$ lxc exec my-new-container /bin/bash
root@my-new-container:~# ls
hello_world.txt snap


And there you have it, you have an isolated linux environment to test or debug anything that you please.


I hope this helps, I couldn't really find a straightforward guide to containers, so I made one :)

Manraj Tatla

Sr. Software Engineer @ Google

1y

great article Vinay !

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics