Dockerfile制作基于Ubuntu的MySQL镜像

基于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已经启动成功)
    在这里插入图片描述

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

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值