dockerfile用yum安装mysql服务-阿里云开发者社区

开发者社区> 科技小先锋> 正文

dockerfile用yum安装mysql服务

简介:
+关注继续查看

一、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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
商城项目02_环境搭建、安装vagrant、docker、mysql、redis、从0搭建各个微服务项目、数据库初始化(六)
商城项目02_环境搭建、安装vagrant、docker、mysql、redis、从0搭建各个微服务项目、数据库初始化(六)
6 0
宝塔一键部署项目及Linux服务器三种软件安装方式【JDK、Tmcat、Docker】
宝塔一键部署项目及Linux服务器三种软件安装方式【JDK、Tmcat、Docker】
7 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
11818 0
阿里云ECS服务器安装docker---(图文教程)
Docker属于Linux容器的一种封装,提供简单易用的容器使用接口。它是目前最流行的 Linux容器解决方案。理由很简单,用了的人都说好用。在多系统的分布式项目中,往往传统的部署发包等操作,那是让每个上线的程序员恨的牙痒呀。通常都是通宵奋战,上生产解决各种部署发包问题。
431 0
6967
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载