Friday, 14 July 2017

It's Docker Time

The time inside a docker container is not necessarily the same as the host's timezone, as by default a container will just give you UTC

At the time of writing (July) I ought to see British Summer Time which is seasonal GMT+1.

rob@graviton ~$ docker run -ti --rm alpine:3.6 date
Fri Jul 14 09:18:28 UTC 2017


rob@graviton ~$ docker run -ti --rm ubuntu:xenial date
Fri Jul 14 09:18:45 UTC 2017

rob@graviton ~$ docker run -ti --rm debian:stretch date
Fri Jul 14 09:20:39 UTC 2017

How to set the timezone?

how about an environment variable?


rob@graviton ~$ docker run -ti --rm -e TZ=Europe/London debian:stretch date
Fri Jul 14 10:21:37 BST 2017


Great, that was simple, how about the others.



rob@graviton ~$ docker run -ti --rm -e TZ=Europe/London ubuntu:xenial date
Fri Jul 14 09:21:57 Europe 2017


rob@graviton ~$ docker run -ti --rm -e TZ=Europe/London alpine:3.6 date
Fri Jul 14 10:14:39 GMT 2017


Bah!   


So, Debian (tested with jessie, stretch and 'stable' images only needs the TZ envrionment variable


Both Alpine and Ubuntu require their respective 'tzdata' package to be installed via your Dockerfile.  (but tested here just by running the commands on the base image)

rob@graviton ~$ docker run --rm -e TZ=Europe/London alpine sh -c "apk add --no-cache tzdata; date"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2017a-r0)
Executing busybox-1.26.2-r5.trigger
OK: 7 MiB in 12 packages
Fri Jul 14 11:23:33 BST 2017


rob@graviton ~$ docker run -ti --rm -e TZ=Europe/London ubuntu:xenial sh -c "apt-get update && apt-get install tzdata && date"
...downloading ....
Fetched 24.2 MB in 6s (3797 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 168 kB of archives.
After this operation, 2864 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 tzdata all 2016j-0ubuntu0.16.04 [168 kB]
Fetched 168 kB in 0s (1724 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package tzdata.
(Reading database ... 4764 files and directories currently installed.)
Preparing to unpack .../tzdata_2016j-0ubuntu0.16.04_all.deb ...
Unpacking tzdata (2016j-0ubuntu0.16.04) ...
Setting up tzdata (2016j-0ubuntu0.16.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype

Current default time zone: 'Etc/UTC'
Local time is now:      Fri Jul 14 10:44:21 UTC 2017.
Universal Time is now:  Fri Jul 14 10:44:21 UTC 2017.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

Fri Jul 14 11:44:21 BST 2017


Then the big However...

Using the TZ environment variable will probably suffice in most instances, especially when using Docker.  It is really a user setting and is described here: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
and suggests that it isn't needed if the system has been set up properly.

In docker's single process environment that would probably be OK for most scenarios, but if there's other forked processes that don't take the paren't environment, then these processes will be left with UTC again

So, how to go belt and braces and get the container setup with a system-wide timezone setting?

I'll demo this here with this final command, which runs a new shell for a system user, without the environment of the original login

su - daemon -s /bin/bash -c date

Debian

Debian already has tzdata, so...

rob@graviton ~$ docker run -ti --rm debian:stretch sh -c "ln -nfs /usr/share/zoneinfo/Europe/London /etc/localtime; dpkg-reconfigure -f noninteractive tzdata; su - daemon -s /bin/bash -c date"

Current default time zone: 'Europe/London'
Local time is now:      Fri Jul 14 12:59:51 BST 2017.
Universal Time is now:  Fri Jul 14 11:59:51 UTC 2017.

Fri Jul 14 12:59:51 BST 2017

Ubuntu

rob@graviton ~$ docker run -ti --rm ubuntu:xenial sh -c "apt-get update && apt-get install tzdata && ln -nfs /usr/share/zoneinfo/Europe/London /etc/localtime && dpkg-reconfigure -f noninteractive tzdata && su - daemon -s /bin/bash -c date"
... downloading ...
Fetched 24.2 MB in 6s (3779 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 168 kB of archives.
After this operation, 2864 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 tzdata all 2016j-0ubuntu0.16.04 [168 kB]
Fetched 168 kB in 0s (1652 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package tzdata.
(Reading database ... 4764 files and directories currently installed.)
Preparing to unpack .../tzdata_2016j-0ubuntu0.16.04_all.deb ...
Unpacking tzdata (2016j-0ubuntu0.16.04) ...
Setting up tzdata (2016j-0ubuntu0.16.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype

Current default time zone: 'Etc/UTC'
Local time is now:      Fri Jul 14 11:45:02 UTC 2017.
Universal Time is now:  Fri Jul 14 11:45:02 UTC 2017.
Run 'dpkg-reconfigure tzdata' if you wish to change it.


Current default time zone: 'Europe/London'
Local time is now:      Fri Jul 14 12:45:02 BST 2017.
Universal Time is now:  Fri Jul 14 11:45:02 UTC 2017.

Fri Jul 14 12:45:02 BST 2017


Alpine

rob@graviton ~$ docker run --rm  alpine sh -c "apk add --no-cache tzdata; cp /usr/share/zoneinfo/Europe/London /etc/localtime; su - daemon -s /bin/sh -c date"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2017a-r0)
Executing busybox-1.26.2-r5.trigger
OK: 7 MiB in 12 packages
Fri Jul 14 13:10:09 BST 2017


Note: I've no idea what the /etc/TZ file in alpine is for.


Saturday, 11 January 2014

Expect status from OpenVPN

An expect script to query the management interface of and OpenVPN server.


Loading ....