PostgreSQL in a LXC Container

June 27, 2018   

LXC postgresql container

What is LXC?

LXC is one of the container types that unlike the virtual machines which are operating system emulators, provides an abstract operating system while a virtual machine provides an abstract machine.

I’m going to talk about how to install a LXC container, how to configure networking and how to install postgresql into the container that will be created.

LXC Setup

The OS that I have:

Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:    18.04
Codename:   bionic

Let’s run this command:

$ apt install lxc1

to install LXC into our system. We are ready to create our container. I am going to create a container named postgres-cont but you can name your container as you want with the -n parameter. Run this command:

$ lxc-create -n postgres-cont -t download

This command will take us to a screen that we can choose the operating system with details that we will install to our container:

gif

The duration of the container creation may change depending on the quality of your internet, be patient :)

Next command shows the existing containers:

$ lxc-ls –fancy

Output:

    NAME          STATE   AUTOSTART GROUPS IPV4 IPV6    
    postgres-cont STOPPED 0         -      -    -    

But our container is not running. It is time to run the container that we’ve just created. Run this:

$ lxc-start -n postgres-cont

When we list our containers again:

$ lxc-ls --fancy

Current output is:

    NAME          STATE   AUTOSTART GROUPS IPV4      IPV6 
    postgres-cont RUNNING 0         -      10.0.3.98 -  

We can see that our container is running. We can access to our container. For this run this command:

$ lxc-attach -n postgres-cont

The first thing to install into our container is net-tools:

$ apt-get install net-tools

Vi editor should already installed in our container by default but I am going to install Nano just because of my personal preferences:

$ apt-get install nano

And of course postgreSQL:

$ apt-get install postgresql

Now postgresql is ready to be used. By this command:

$ sudo -u postgres psql

we can access to postgresql:

gif

Actually we run psql command with postgres user in the command that we’ve just run. The reason for that is, PostgreSQL cannot be accessible by any system user except postgres. So if we would run psql command with the root user of our container we would get an error:

gif


File Transfer From LXC Container to Host

We can access to root file system of our containers that we created before in /var/lib/lxc folder by default with lxc-create command. In this way we can transfer files between our host machine and container.

Here is a short example of transferring a .txt file named test.txt from our container toour host machine by accesing via our host machine:

gif


LXC Network Configuration

dnsmasq.conf

an IPv4 address is defined randomly between the range of the LXC_DHCP_RANGE variable, which is defined in lxc-net that is in the /etc/default folder, to our containers:

    LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"

If we want to remove this randomness and define IPv4 address of our container by our own, we need to make a few configurations.

Firstly let’s configure lxc-net file:

    #LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf

Uncomment this line that commented by default and create a file named dnsmasq.conf in /etc/lxc folder:

$ touch /etc/lxc/dnsmasq.conf

Add this line to the folder that we’ve just created, save it and quit:

    dhcp-host=postgres-cont,10.0.3.190

You can put any IP address that in the range of LXC_DHCP_RANGE variable which is in lxc-net file. I am going to use 10.0.3.190.

In order to apply changes to our container, we must stop our container and restart the lxc-net service:

$ lxc-stop -n postgres-cont
$ service lxc-net restart

Now when we start our container again it will take the IPv4 address 10.0.3.190 that we defined manuelly:

$ lxc-start -n postgres-cont
$ lxc-ls --fancy

Output:

    NAME          STATE   AUTOSTART GROUPS IPV4       IPV6  
    postgres-cont RUNNING 0         -      10.0.3.190 -  

We fixed our container to 10.0.3.190 address.

gif


As a result we have a running LXC container with fixed IPv4 address which contains PostgreSQL in it. My next post will be about accessing to PostgreSQL that is in a LXC container from the outside of the container. We are going to access to PostgreSQL in a container on PyCharm.