创建存放目录

# 存放数据目录
mkdir -p /usr/local/src/mysql/data
# 存放配置的目录,这里的配置项会覆盖默认的配置项
mkdir -p /usr/local/src/mysql/conf.d

创建 docker-compose.yml 文件

/usr/local/src/mysql 目录下创建

version: '3'
services:
  mysql:
    image: mysql:5.7
    restart: always
    container_name: mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: admin123
    volumes:
      - $PWD/data:/var/lib/mysql
      - $PWD/conf.d:/etc/mysql/conf.d
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci

启动

docker-compose up -d

参考

Docker Hub Mysql

写的不好,仅供参考