[JAVA] Time is wrong with the application launched on the Docker container

Introduction

Hi, this is @chan_kaku This time, a problem occurred when the in-house tool was converted to Docker, so I will introduce the problem, the countermeasures taken this time, and the difficult points.

problem

This time I had a problem with my Java application. The problem was that at some point, the time to be persisted in the DB was made persistent by taking the current time with new Date (). Until now, I didn't use Docker and depended on the timezone of the host server, so I was able to take time with JST and there was nothing wrong with it. However, after converting this tool to Docker, there was a problem that the current time on this new Date () was shifted.

Guess the cause

In the first place, I didn't know much about Docker and guessed that there was a problem with the code on the Java side. However, as mentioned above, the current time was taken as new Date (), so that guess was wrong. The next guess was the host server timezone. However, when I typed the date command, it was displayed as JST, so this was also missed. I've run out of my possible guesses, so when I asked people familiar with Docker, I got the information that it seems that there is time inside Docker, and that this time may be off. So, I entered Docker with the following command and checked the time zone in the Docker container in the same way.

docker run -t -i (Target docker image)  /bin/bash → start docker and go inside
# date

Here, it was JST as before.

Countermeasures

When I was lamenting that all the measures had been exhausted here, a voice of a crane came from a certain person. "Look at the contents of / etc / timezone in Docker ~ " Believing in this person, I entered Docker and looked at / etc / timezone, and found that this was ʻUTC. So I modified the Dockerfile to change / etc / timezone to ʻAsia / Tokyo as follows

ENV TZ="Asia/Tokyo"
RUN echo $TZ > /etc/timezone

//Descriptions other than timezone related are omitted

The reason why I changed it with echo this time is that the time zone of the time system was required to be JST due to the requirements of the tool, so I decided that this should be described in the Dockerfile.

At the end

I had a hard time in various places because I didn't have much knowledge about Docker, but I'm glad I solved it for the time being! We are waiting for Masakari, who says that these people are smarter.

Recommended Posts

Time is wrong with the application launched on the Docker container
Run GUI application on Docker container
Launched Redmine with Docker on Raspberry Pi 3
[Docker] How to build when the source code is bind-mounted on the container
Run GUI application on Docker container (Japanese input)
Update container image with KUSANAGI Runs on Docker
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
Run the Android emulator on Docker using Android Emulator Container Scripts
Display ROS application on Docker with GUI on host side
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
Proceed with Rust official documentation on Docker container (1. Getting started)
Wait for the container service to start with docker healthcheck
[Docker] Delete only the volume associated with a specific container
Web application built with docker (1)
Now is the time to get started with the Stream API
How to build an environment with Docker, which is the minimum required to start a Rails application
When requested access to the resource is denied when pushing with Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
I tried deploying a Docker container on Lambda with Serverless Framework
Deploy a Docker application with Greengrass
Run React on a Docker container
Programming with ruby (on the way)
WordPress with Docker Compose on CentOS 8
Run PureScript on a Docker container
Web application creation with Nodejs with Docker
[Linux] Start Apache container with Docker
Build WebRTC Janus with Docker container
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
For the time being, run the war file delivered in Java with Docker
Starting with installing Docker on EC2 and running Yellowfin in a container
Put Zabbix in Ubuntu with Docker and monitor Docker on the same host
Try Alfresco 6.0 on Windows (Docker will be introduced at the same time)
Launch docker container on EC2 (personal memorandum)
Run NordVPN on Docker (Windows) Ubuntu container
The story of updating SonarQube's Docker Container
Scraping with puppeteer in Nuxt on Docker.
How the website is displayed on the screen
Communicate Gitlab and Gitlab Runner launched with Docker
[Docker] Start the container as soon as possible
Deploy your application with VPC + EC2 + Docker.
Build an environment with Docker on AWS
Try the Docker environment on AWS ECS
Run Ubuntu + ROS with Docker on Mac
Time shift measures with Docker for Windows
Explanation until the original application is completed
A quick note on using jshell with the official Docker image of the JDK
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
Start Elixir / Phoenix on your Android smartphone! … By the way, Ubuntu is the strongest container in the world (with precompiled Elixir installed)
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC