dockerfile用yum安装mysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dockerfile用yum安装mysql相关的知识,希望对你有一定的参考价值。

一、Dockerfile内容如下:

说明:

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


FROM centos:centos7.1.1503

MAINTAINER [email protected]


#定义变量

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?


以上是关于dockerfile用yum安装mysql的主要内容,如果未能解决你的问题,请参考以下文章

Centos7使用yum安装MySQL5.6的正确姿势

centos中yum安装mysql路径

linux环境安装mysql

Centos: 修改 yum安装的mysql路径

CentOS 7 yum安装mysql

使用Dockerfile创建带mysql的Centos Docker镜像