Setup your time on the host and inside the Docker containers

If you want to set the server’s timezone and keep it syncronized and you are using Ubuntu like me you can do the following:

On the host you can change the timezone and configure a cron to syncronize it:

dpkg-reconfigure tzdata

Pick your continent and capitol and save;

Then setup a cron to update the time daily:

echo "ntpdate" > /etc/cron.daily/ntpdate && chmod 755 /etc/cron.daily/ntpdate

That’s it!

Now if you want to change the timezone inside a Docker container:

I usually have sshd running inside the containers which allows me to ssh inside them and make changes as well as reload the server process running inside to activate the changes:

docker inspect mysql_container

Now you see the ip of the container, for example: and you use it here:


Update: now you can use docker exec to enter a container environment. Forget about running sshd server in docker containers:

docker exec -ti mycontainername bash
export TERM=xterm
#do your stuff here

Inside the container:

dpkg-reconfigure tzdata

You are not allowed to use ntpdate inside the container but it probably gets the time from the host(I still have some things to learn) and you only need the timezone set.

Another(prefered) way to make changes inside the container and keep them is to run another container from the image that the one you are going to change is running from.

docker run -ti --name mysql_container_tmp iliyan/mysql bash

Then make your changes, exit and commit the new container over the same image. Stop and remove the original container as well as the _tmp one and run a new container from the updated image. Now your changes are there!

Check this Dockerfile that automates the process (the part on the bottom that works with timezones) and also this bash script.

How to change the time zone inside a Docker container running Ubuntu

More information can be seen in my other article.

You should always set the correct time zone in your app. However if you want the server’s time to be your time, you can use the following command to reconfigure it:

sudo dpkg-reconfigure tzdata

Check this Dockerfile and this bash script that automate the process.

More information with images.