DEV Community 👩‍💻👨‍💻

Migsar Navarro
Migsar Navarro

Posted on

Sync container's clock with host machine

I recently had a problem while trying to use Google sign-in in a development containerized environment. There was something like 36 seconds difference, I don't remember if it was ahead or behind, from the container to Google servers but it was working fine if I ran the app locally.

Then this morning I was trying to use some Amazon AWS S3 bucket and the system failed with the following error:

RequestTimeTooSkewed: The difference between the request time and the current time is too large.

I currently use Windows, not because I particularly liked it but because it was the preinstalled OS, but I must confess I am favorably surprised with WSL2 and all the effort Microsoft has been doing towards cloud development, aka Remote Containers. It took me about ten minutes to find out the things online so I decided it was time to write another small post to have it as a reference for future time.

The findings here are not mine, but part of this StackOverflow answer, that references this Github's issue in Docker for Mac repository.

The trick is done by this super simple command:

hwclock -s
Enter fullscreen mode Exit fullscreen mode

You can use it in a container without having to stop it or restart it, IF you are running in privileged mode.

Just as a tip, to be used with some common sense, I set the dev container I use to privileged in the docker-compose.yaml and add a small script to my repo's package to be able to quickly run it from yarn:

{
  "name": "my-repo",
  "scripts": {
    "clock-sync": "docker exec -it -u root my-container /bin/sh -c \"hwclock -s\""
  }
}
Enter fullscreen mode Exit fullscreen mode

I think the command can be used in any container and it affects all of them, I was not able to dig much into how all of this happens, so I would be more than happy to read similar experiences or detailed info about what is happening under the hood, if you know it please comment!

Top comments (0)

🤔 Did you know?

 
✍️ Writing your own article is easy (we even support markdown).