Dec 02 2017

How to set the timezone in LXD containers

See https://blog.simos.info/trying-out-lxd-containers-on-our-ubuntu/ on how to set up and test LXD on Ubuntu (or another Linux distribution).

In this post we see how to set up the timezone in a newly created container.

The problem

The default timezone for a newly created container is Etc/UTC, which is what we used to call Greenwich Mean Time.

Let’s observe.

$ lxc launch ubuntu:16.04 mycontainer
Creating mycontainer
Starting mycontainer

$ lxc exec mycontainer -- date
Sat Dec 2 11:40:57 UTC 2017

$ lxc exec mycontainer -- cat /etc/timezone 
Etc/UTC

That is, the observed time in a container follows a timezone that is different from the vast majority our computer settings. When we connect with a shell inside the container, the time and date is not the same with that of our computer.

The time is recorded correctly inside the container, it is just the way it is presented, that is off by a few hours.

Depending on our use of the container, this might or might not be an issue to pursue.

The workaround

We can set the environment variable TZ (for timezone) of each container to our preferred timezone setting.

$ lxc exec mycontainer -- date
Sat Dec 2 11:50:37 UTC 2017

$ lxc config set mycontainer environment.TZ Europe/London

$ lxc exec mycontainer -- date
Sat Dec 2 11:50:50 GMT 2017

That is, we use the lxc config set action to set, for mycontainer,  the environment variable TZ to the proper timezone (here, Europe/London). UTC time and Europe/London time happen to be the same during the winter.

How do we unset the container timezone and return back to Etc/UTC?

$ lxc config unset mycontainer environment.TZ

Here we used the lxc config unset action to unset the environment variable TZ.

The solution

LXD supports profiles and you can edit the default profile in order to get the timezone setting automatically applied to any containers that follow this profile. Let’s get a list of the profiles.

$ lxc profile list
+---------+---------+
| NAME    | USED BY |
+---------+---------+
| default |       7 |
+---------+---------+

Only one profile, called default. It is used by 7 containers already on this LXD installation.

We set the environment variable TZ in the profile with the following,

$ lxc exec mycontainer -- date
Sat Dec 2 12:02:37 UTC 2017

$ lxc profile set default environment.TZ Europe/London

$ lxc exec mycontainer -- date
Sat Dec 2 12:02:43 GMT 2017

How do we unset the profile timezone and get back to Etc/UTC?

lxc profile unset default environment.TZ

Here we used the lxc profile unset action to unset the environment variable TZ.

 

Permanent link to this article: https://blog.simos.info/how-to-set-the-timezone-in-lxd-containers/

1 ping

  1. […] How to set the timezone in LXD containers […]

Leave a Reply

%d bloggers like this: