dockerfile用yum安装mysql服务

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

一、Dockerfile内容如下:

说明:

    本镜像是基于centos 7为基础镜像来构建,centos基础镜像源来自于csphere中,当然你可以修改为其它网站的镜像源也可以(比如docker官方源、网易风巢,时运云、阿里云等),


FROM centos:centos7.1.1503

MAINTAINER ganbing87@126.com


#定义变量

ENV TIME_ZONE "Asia/Shanghai"

ENV TERM xterm

ENV DATA_DIR /var/lib/mysql


#复制宿主机中阿里云的镜像源到镜像中

ADD aliyun-mirror.repo /etc/yum.repos.d/CentOS-Base.repo

ADD aliyun-epel.repo /etc/yum.repos.d/epel.repo


#安装系统相关工具(可根据自己的需求安装相应的工具,觉得不需要的就去掉)

RUN rpm --rebuilddb && \

   yum install -y curl wget tar bzip2 unzip vim-enhanced passwd sudo yum-utils hostname net-tools rsync man && \

    yum install -y gcc gcc-c++ git make automake cmake patch logrotate python-devel libpng-devel libjpeg-devel && \

    yum install -y --enablerepo=epel pwgen python-pip && \


# 安装Mariadb

RUN  yum install -y mariadb mariadb-server && \

         yum clean all


#配置服务器时区

RUN echo "${TIME_ZONE}" > /etc/timezone && ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime


#安装supervisord

RUN pip install supervisor

ADD supervisord.conf /etc/supervisord.conf


RUN mkdir -p /etc/supervisor.conf.d && \

    mkdir -p /var/log/supervisor


#复制宿主机mysqld_charset的配置文件到镜像中

ADD mysqld_charset.cnf /etc/my.cnf.d/


#复制宿主机scripts文件到镜像中

COPY scripts /scripts

RUN chmod +x /scripts/start


#暴露3306端口

EXPOSE 3306


#挂载卷

VOLUME ["/var/lib/mysql"]


ENTRYPOINT ["/scripts/start"]


二、附件内容


1、mysqld_charset.cnf

[mysqld]

character_set_server=utf8

character_set_filesystem=utf8

collation-server=utf8_general_ci

init-connect='SET NAMES utf8'

init_connect='SET collation_connection = utf8_general_ci'


2、scripts目录结构和内容

目录结构:

scripts/

├── firstrun

├── firstrun_maria

└── start


【firstrun_maria】内容:

#!/bin/bash


DB_USER=${DB_USER:-admin}

DB_PASS=${DB_PASS:-123456}


MARIADB_NEW=true


#

#  MariaDB setup

#

firstrun_maria() {


# First install mariadb

if [[ ! -d ${DATA_DIR}/mysql ]]; then

    echo "===> MariaDB not install..."


        echo "===> Initializing maria database... "

   mysql_install_db --user=mysql --ldata=${DATA_DIR}

        echo "===> System databases initialized..."


   # Start mariadb

        /usr/bin/mysqld_safe --user mysql > /dev/null 2>&1 &


        echo "===> Waiting for MariaDB to start..."


STA=1

while [[ STA -ne 0 ]]; do

            printf "."

sleep 5

mysql -uroot -e "status" > /dev/null 2>&1

STA=$?

done

        echo "===> Start OK..."


# 1. Create a localhost-only admin account

mysql -u root -e "CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PASS'"

mysql -u root -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS'"

mysql -u root -e "CREATE USER '$DB_USER'@'127.0.0.1' IDENTIFIED BY '$DB_PASS'"

mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'%' WITH GRANT OPTION"

        echo "===> Create localhost completed..."


# shutdown mariadb to wait for supervisor

mysqladmin -u root shutdown


else

        if [[ -e ${DATA_DIR}/mysql.sock ]]; then

            rm -f ${DATA_DIR}/mysql.sock

        fi


        MARIADB_NEW=false


   echo "===> Using an existing volume of MariaDB"

fi

}



【start】内容:

#!/bin/bash

set -e

#

# When Startup Container script

#


if [[ -e /scripts/firstrun ]]; then

# config mariadb

/scripts/firstrun_maria

    rm /scripts/firstrun

else

# Cleanup previous mariadb sockets

if [[ -e ${DATA_DIR}/mysql.sock ]]; then

rm -f ${DATA_DIR}/mysql.sock

fi

fi


exec /usr/bin/mysqld_safe


【aliyun-epel.repo】内容:

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

baseurl=http://mirrors.aliyun.com/epel/7/$basearch

#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch

failovermethod=priority

enabled=1

gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7


[epel-debuginfo]

name=Extra Packages for Enterprise Linux 7 - $basearch - Debug

baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug

#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch

failovermethod=priority

enabled=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

gpgcheck=0


[epel-source]

name=Extra Packages for Enterprise Linux 7 - $basearch - Source

baseurl=http://mirrors.aliyun.com/epel/7/SRPMS

#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch

failovermethod=priority

enabled=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

gpgcheck=0


【aliyun-mirror.repo】内容:

# CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and

# geographically close to the client.  You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you can try the 

# remarked out baseurl= line instead.

#

#

 

[base]

name=CentOS-$releasever - Base - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

 

#released updates 

[updates]

name=CentOS-$releasever - Updates - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

 

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras

gpgcheck=1

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

 

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus

gpgcheck=1

enabled=0

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

 

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - mirrors.aliyun.com

failovermethod=priority

baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib

gpgcheck=1

enabled=0

gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7You have new mail in /var/spool/mail/root


【supervisord.conf】内容:

[unix_http_server]

file=/var/run/supervisor.sock ; (the path to the socket file)

chmod=0700              ; socket file mode (default 0700)


[supervisord]

logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)

logfile_maxbytes=50MB

logfile_backup=10

loglevel=info

pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)

nodaemon=true           ; (Start in foreground if true; default false)

minfds=1024                 ; (min. avail startup file descriptors;default 1024)

minprocs=200                ; (min. avail process descriptors;default 200)


[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface


[supervisorctl]

serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket


[include]

files = /etc/supervisor.conf.d/*.conf


三、使用dockerfile构建mysql镜像


1、用dockerfile构建mysql镜像

docker build  -t mysql:5.5 .


2、运行mysql容器

docker run -d -p 3306:3306 --name dbserver mysql:5.5 



本文转自甘兵 51CTO博客,原文链接:http://blog.51cto.com/ganbing/2048679,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
关系型数据库 MySQL 数据安全/隐私保护
MySQL安装
MySQL安装
25 1
|
3天前
|
SQL 关系型数据库 MySQL
MySQL服务的状态如何查看?
【5月更文挑战第23天】MySQL服务的状态如何查看?
14 1
|
3天前
|
网络协议 关系型数据库 MySQL
MySQL服务
【5月更文挑战第23天】MySQL服务
7 1
|
5天前
|
关系型数据库 MySQL Linux
在Linux系统中安装MySQL数据库
在Linux系统中安装MySQL数据库
41 3
|
5天前
|
Ubuntu 关系型数据库 MySQL
|
8天前
|
安全 关系型数据库 MySQL
解决centos7.0安装mysql后出现access defind for user@'localhost'的错误
在使用yum 安装完mariadb, mariadb-server, mariadb-devel后
11 0
|
11天前
|
关系型数据库 MySQL Linux
Linux下安装MySQL
Linux下安装MySQL
30 0
|
11天前
|
SQL 分布式计算 关系型数据库
Ubuntu上安装MySQL以及hive
Ubuntu上安装MySQL以及hive
|
11天前
|
缓存
yum 如何设置可以将安装的rpm包都缓存下来
【4月更文挑战第17天】yum 如何设置可以将安装的rpm包都缓存下来
51 0
|
11天前
|
Linux C语言
linux yum安装ffmpeg 图文详解
linux yum安装ffmpeg 图文详解
70 0