Docker: building an ntp server image with Alpine and chrony

If you need a lightweight NTP server, an Alpine based container image with a chrony daemon takes up minimal runtime resources and is about 8Mb in size.

I have pushed ‘fabianlee/docker-chrony-alpine‘ to docker hub.  The run command requires that you specify linux capabilities and a volume for the chrony.conf file, so the easiest way to test is use the make target I have supplied in the github project.

# required packages
sudo apt-get install make git -y

# pull project from github
git clone https://github.com/fabianlee/docker-chrony-alpine.git
cd docker-chrony-alpine

# first, make sure host does not already have ntp bound locally
sudo netstat -ulnp | grep ':123'

# pull and run container in background
make docker-run-bg

# view logs
make docker-logs

This creates a ntp listener on your host, which you can validate using:

# required ntp client package
sudo apt-get install ntpdate -y
# run client test
ntpdate -q 127.0.0.1

# for a deeper test, chronyc from inside container
make test

I am using the ‘–network host’ to put this exposed port/123 unto the local host.  This is not a requirement, but it makes it easier to find without needing worry about routing or forwarding to the container IP.

 

REFERENCES

chrony.conf description

alpine linux, setting the timezone

alpine packages, chrony

golinuxcloud, chrony on server and client with rhel

github cwadley, simple chrony/alpine docker image

linux-audit.com, capability sets

NOTES

commands for chronyc

chronyc tracking
chronyc dump
chronyc sources -v