🕑

Dockerで起動したMySQLの現在日時を変更

2022/07/25に公開

docker-compose.yml

version: '3'
services:
  db:
    # image: mysql:8.0.28
    build:
      dockerfile: mysql.Dockerfile
    environment:
      MYSQL_ROOT_PASSWORD: <MYSQL_ROOT_PASSWORD>
      MYSQL_DATABASE: <MYSQL_DATABASE>
      MYSQL_USER: <MYSQL_USER>
      MYSQL_PASSWORD: <MYSQL_PASSWORD>
      TZ: 'Asia/Tokyo'
      LD_PRELOAD: /usr/local/lib/faketime/libfaketime.so.1
      FAKETIME: 2022-07-31 12:00:00
    ports:
      - 3306:3306
    volumes:
      - ./docker/mysql/db:/var/lib/mysql
    command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_0900_as_cs' ]

mysql.Dockerfile

FROM mysql:8.0.28

WORKDIR /
RUN apt-get update
RUN apt-get install -y git
RUN git clone https://github.com/wolfcw/libfaketime.git
WORKDIR /libfaketime/src
RUN apt-get install -y build-essential
RUN make install
docker compose down && docker compose up -d
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2022-07-31 12:00:00 |
+---------------------+
1 row in set (0.01 sec)

mysql> 

Discussion