How Can I Change the Date in a Docker Container?

Linode Staff

in my linux contain Docker

Can we change date in docker container?

for example,
if current date is 21 Dec 2021
if in our docker container we run
date --set='11 days ago'

then the date in container to be 10 Dec 2021
And the date on Host OS still current date, 21 Dec 2021

Do you any info and idea about my case?

1 Reply

Linode Staff

Hi there,

Basically, if you can access your host system, you can change the date of both your Linux host and your Linux container. The only thing to keep in mind is that some Linux distributions use different commands and configurations to set up the system date values.

For example, for my Linode host system running a CentOS 7 distribution, in the Singapore data center region, this is how I configure the date from default:

[root@139-162-4-22 ~]# timedatectl | grep -i 'time zone'
       Time zone: UTC (UTC, +0000)
[root@139-162-4-22 ~]# timedatectl set-timezone Asia/Singapore
[root@139-162-4-22 ~]# timedatectl | grep -i 'time zone'
timedatectl | grep -i 'time zone'
       Time zone: Asia/Singapore (+08, +0800)

This covers that in detail:

However, if my Linode host had a different distribution like Ubuntu or Debian, then a different method might be needed. Next you can find some excellent resources you can use to configure the date in your host system:

If you can only access the your container (no host access), you usually can only change your container's date. That said it looks like Docker allows you to change the host system date from inside the container if you configure you (re-)deploy your container with less secure flags during the container's startup process as describe in this post from the Docker Community post:

"If you are asking how to set the host OS’s clock from inside a container, you would need to relax security on your container by adding the --cap-add SYS_TIME flag to your container at startup.

ie: docker run --rm -it --cap-add SYS_TIME alpine date -s 12:00"

Good Luck!
Arty

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct