Configure time zone to mysql docker container

value should be

default_time_zone=Europe/Sofia

check details

But this could give you error like this while restarting the mysql service so you should use below command before editing custom.cnf file.

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p

So you need to use a Dockerfile in this case and handle it like below

FROM mysql:5.7.21
RUN echo "USE mysql;" > /docker-entrypoint-initdb.d/timezones.sql &&  mysql_tzinfo_to_sql /usr/share/zoneinfo >> /docker-entrypoint-initdb.d/timezones.sql

This makes sure that when the mysql container loads it will load all the timezone info. Now you can use it using environment variables

Environment variable

mysqldb:
    #image: mysql:5.7.21
    build: .
    #container_name: mysql_container
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - TZ=Europe/Sofia

To use it with a config file is a problem. When you start the DB it will give your an error

mysqldb_1  | 2018-04-24T12:29:43.169214Z 0 [Warning] InnoDB: New log files created, LSN=45790
mysqldb_1  | 2018-04-24T12:29:43.215187Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
mysqldb_1  | 2018-04-24T12:29:43.281229Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2a87fec6-47bb-11e8-9f1e-0242ac110002.
mysqldb_1  | 2018-04-24T12:29:43.284010Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
mysqldb_1  | 2018-04-24T12:29:43.284404Z 0 [ERROR] Fatal error: Illegal or unknown default time zone 'Europe/Sofia'
mysqldb_1  | 2018-04-24T12:29:43.284567Z 0 [ERROR] Aborting

This is because it needs the timezone to have been already loaded. It is possible to fix this also, but too much of hassle. I will go with the environment variable only as that means when the container starts the timezone is already setup.

Time


For me, I'm using Keramatic https://kitematic.com configure by click on MySQL container General and add TZ specify with your current time show list in https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

enter image description here Cheers


The mariadb:10.3 docker container interprets the TZ environment variable, although it's not mentioned on the Readme.md for the docker image. In my case providing the value of America\New_York worked perfectly.