LXD/LXC Containers

From iDempiere en


Credits

  • Canonical Ltd

Background

I have just started dabbling with LXC containers and I am excited with my experience thus far. I find them easy to use with command line instructions. I like how lightweight these containers are and that one can use various Linux distros as the os of the container. So stopping, starting, creating, moving, copying and deleting are all easily achievable and happens quickly. I will document my use case below. My intention, however, is to introduce this technology to others so that more people can experiment with these containers. Ultimately more innovative use cases may be shared thus raising the productivity of our iDempiere community.

Resources

The following were helpful to me:

...used a major portion of this article's profile as my own

Aliases

I use the following aliases:
This allows me to log into a container with user name 'yogan' like: lxc login <container>

lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@yogan - exec /bin/login -p -f '

Confirm before deleting a container

lxc alias add delete 'delete -f -i'

This allows me to log into a container with 'root' like: lxc shell <container>

lxc alias add shell 'exec @ARGS@ -- bash

My Use Case

My personal laptop is the host. The os is Ubuntu 20.04.2 LTS. The MEGA/sources is where the various customer/idempiere repositories are. I share this with all new containers that are created.

I created a container called ID82z using os image: Ubuntu 20.04.2 LTS. I downloaded Eclipse, OpenJDK 11, maven, etc in here. Used the source from the shared directory.

I created another container called database using os image: Ubuntu 20.04.2 LTS. I downloaded PostgreSql 13 here. Changed the access port from 5432 and ensured that the db is available from outside.

So my lxc list looks like:

Lxc list.png




The Steps

Install Lxd:

snap install lxd

LXD initial configuration:
I chose the defaults and set the storage to 'dir'

sudo /snap/bin/lxd init

Register your user into the lxd group and not to use sudo the entire time

sudo usermod -a -G lxd <user>

Set the Lxd network as follows:

lxc network set lxdbr0 ipv4.address 172.16.0.1/24

Set the other properties similarly. This how my bridge network has been set up

Lxd network.png







I chose the image that has cloud-init support. This is important if you want cloud-init to automate some of your profile tasks

lxc launch images:ubuntu/focal/cloud <container name>

Once the container has been created, go into the container and look at /var/log/cloud-init-output.log to see if there was any issues encountered

The Profile

The profile is the key to automate regular tasks when a container is built.
You can find my profile at: https://pastebin.com/AhAF2vVr
I found it a good idea to create your own file like default.yaml for your profile. I use this as my master file. The default that lxc uses sometimes has formatting issues in the cloud-int section
Then I update the default as follows

lxc profile edit default < default.yaml

This section in the profile is automatically creating a user and share my SSH public key. To copy the key, I had to 'cat' the key on the command line and copy the long key like that.

name: yogan
ssh_authorized_keys:

Created a physical network called eth0 using dhcp

network:
version: 1
config:
- type: physical
name: eth0
subnets:
- type: dhcp

Copied some of my profiles so that my experience in these containers are the same as in the host

- cp /home/yogan/Downloads/.bashrc /root/.bashrc
- cp /home/yogan/Downloads/.bashrc /home/yogan/.bashrc
- cp /home/yogan/Downloads/.profile /root/.bash_profile
- cp /home/yogan/Downloads/.profile /home/yogan/.bash_profile
- cp /home/yogan/Downloads/.profile /root/.profile
- cp /home/yogan/Downloads/.profile /home/yogan/.profile

Sharing some directories from the host with a new container

Downloadsdir:
path: /home/yogan/Downloads
source: /home/yogan/Downloads
type: disk
Sourcedir:
path: /home/yogan/sources
source: /home/yogan/MEGA/sources
type: disk

I followed https://blog.simos.info/ to add X11 graphics to my containers in the profile.

After the container is created you can see the log of cloud-init stuff in /var/log/cloud-init-output.log

What Has Been Achieved With this Use Case?

  • When I run Eclipse in one of the containers e.g ID82z, I am guaranteed to run this version of ID with the correct versions of software e.g Eclipse, Java and OS. So upgrading the host (a problem I typically had previously) will not affect the container.
  • My software versions are in the host under sources separated by different directories. Git works well in those directories as well.
  • The PostgreSQl database is in it's own container. I guess each db version you require could be in it's own container. The ID setup in each Eclipse container will point to it's relevant database container. Notice you can refer to other containers from a container like <container>.lxd. See below:
Db setup.png





  • Once the ID Server is running in it's Eclipse container I can browse to it from the host.
  • The containers are lightweight compared to VirtualBox with similar applications running

What I Still Want to Achieve

lxc stop ID82z --stateful

Other Stuff

If you have many containers and are resource constrained. You may want to constrain your containers like:

lxc config set <container> limits.memory 8GiB
lxc config set <container> limits.cpu 4


If you want to provide additional comments, please use the iDempiere forum post: iDempiere forum post

Cookies help us deliver our services. By using our services, you agree to our use of cookies.