1. 程式人生 > >dockerfile用yum安裝mysql

dockerfile用yum安裝mysql

甘兵 it docker dockerfile 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