基于ubuntu18.04制作MySQL的docker镜像

通过dockefile

1.版本信息
  • 镜像源:https://ogec0pyk.mirror.aliyuncs.com(阿里)
  • Ubuntu:18.04
  • MySQL:5.7
2.步骤
  1. 编写Dockerfile

     FROM ubuntu
     #将sources.list复制到镜像下的/etc/apt/下面,修改镜像源地址
     ADD sources.list /etc/apt/
     #将run.sh脚本复制到镜像中(我这里随便找的路径)
     ADD run.sh /home/
     #更新apt,因为更换了sources.list,所以比较快,使用官方镜像源在国外,速度特慢
     RUN apt-get clean && apt-get update
     #安装MySQL
     RUN apt-get -y install mysql-server
     RUN chmod +x /home/run.sh
     EXPOSE 3306 
     CMD ["/home/run.sh"]
    
  2. 编写sources.list如下,使用的是阿里的,可在官网查找其他版本的

    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    

3.编写run.sh

#!/bin/bash
sed -i 's/127.0.0.1/0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
chown -R mysql:mysql /var/lib/mysql /var/run/mysqld
service mysql start
mysql -uroot -e "grant all privileges on *.* to 'root'@'%' identified WITH mysql_native_password by '123456';" 
mysql -uroot -e "grant all privileges on *.* to 'root'@'localhost' identified WITH mysql_native_password by '123456';" 
service mysql restart
/bin/bash

4.构建镜像
docker build -t lyfts:v4 .
在这里插入图片描述
镜像构建成功

  1. 启动容器
    docker run -itd --name dfmysq4 -p 3323:3306 lyfts:v4
  2. 执行启动脚本,我将run.sh复制到/home目录下的
    docker exec -i 容器id /home/run.sh
  3. 远程连接测试(MySQL已经启动成功)
    在这里插入图片描述

到此结束,如有问题,请留言!!!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐