Perfect solution to Docker Alpine image time zone problem

Perfect solution to Docker Alpine image time zone problem

Recently, when I was using Docker to deploy a Java application, I found that the time zone was wrong. Using JDK to get the current time was 8 hours slower than the standard time zone.

Solution:

Solution 1. Modify Dockerfile

Set the time zone of the Alpine Linux system. Find the documentation of Alpine Linux and learn that the time zone can be set through the tzdata package. When building the Docker image, add this sentence to the Dockerfile:

RUN apk --update add tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone && \
    apk del tzdata && \
    rm -rf /var/cache/apk/*

Complete Dockerfile

FROM openjdk:8-jre-alpine3.9

RUN apk --update add tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone && \
    apk del tzdata && \
    rm -rf /var/cache/apk/*

# copy the packaged jar file into our docker image
COPY application.jar /application.jar

Document link:

wiki.alpinelinux.org/wiki/Setting…

Solution 2. Set the JVM's system default time zone

When starting the Docker image, set the time zone by setting the user.timezone JVM environment variable

java -jar -Duser.timezone=Asia/Shanghai app.jar

Solution 3. Mount the host machine’s time zone file into the Docker container

The cluster solution uses K8S. When deploying, mount the host's time zone file into the Docker container.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: SERVICE_NAME
spec:
  replicas: 1
  selector:
    matchLabels:
      app: SERVICE_NAME
  template:
    metadata:
      labels:
        app: SERVICE_NAME
    spec:
      containers:
      - name: SERVICE_NAME
        image: IMAGE_TAG
        imagePullPolicy: Always
        ports:
        - containerPort: 80
      	volumeMounts:
        - name: tz-config
          mountPath: /etc/localtime
      volumes:
      - name: tz-config
        hostPath:
           path: /etc/localtime

Check if it is running normally

date -R

Reference Documents:

quaded.com/docker-apli…

blog.csdn.net/jeikerxiao/…

This is the end of this article about solving the time zone problem of Docker Alpine image. For more relevant content about Docker Alpine image time zone, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Alpine Docker image font problem solving operations
  • Implementation of tomcat image created with dockerfile based on alpine
  • Implementation of crawler Scrapy image created by dockerfile based on alpine
  • How to build php-nginx-alpine image from scratch in Docker

<<:  An in-depth summary of MySQL time setting considerations

>>:  Implementation code for adding slash to Vue element header

Recommend

CSS3 transition to implement notification message carousel

Vue version, copy it to the file and use it <t...

Achieve 3D flip effect with pure CSS3 in a few simple steps

As a required course for front-end developers, CS...

Detailed installation and use tutorial of mysql 8.0.15 under windows

This article shares with you the detailed install...

How to use crontab to backup MySQL database regularly in Linux system

Use the system crontab to execute backup files re...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

Implementation of the Pycharm installation tutorial on Ubuntu 18.04

Method 1: Download Pycharm and install Download a...

Detailed explanation of the correct use of the if function in MySQL

For what I am going to write today, the program r...

React implements dynamic pop-up window component

When we write some UI components, if we don't...

Web page production TD can also overflow hidden display

Perhaps when I name this article like this, someon...

The process of deploying and running countly-server in docker in win10

I have just come into contact with and become fam...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Turn off the AutoComplete function in the input box

Now we can use an attribute of input called autoco...

Use of Linux watch command

1. Command Introduction The watch command execute...