前言

为方便其他开源软件的配置实验,利用docker技术基于centos7 系统搭建了lamp 环境,其中将部署mysql 、apache、php5.6。提供基础的数据存储,还将部署phpmyadmin 、memadmin 等常用的数据层管理工具。由于大部分都是命令操作,将略去大段的语言描述,只需按照如下命令操作即可获得一个完整的LAMP 环境。
docker 的安装,见我之前的博客文章。

环境安装

docker pull centos
docker run --name centos -d -i -t centos /bin/bash
docker attach centos

#更新yum 并安装工具
yum update
yum install -y net-tools mariadb mariadb-server httpd epel-release autoconf automake vim wget
#配置php5.6 的源
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-gd
mkdir -p /var/run/mariadb
chown -R mysql:mysql /var/run/mariadb
chown -R mysql:mysql /var/lib/mysql
Ctrl-p Ctrl-q #退出

#保存镜像
docker commit -a "mir355" -m "Centos7 http mysql5 php56" centos private/lamp:v1

目录规划

mkdir -p /Volumes/MacSoft/data/lamp/config/phpd
mkdir -p /Volumes/MacSoft/data/lamp/mysql
mkdir -p /Volumes/MacSoft/data/lamp/logs/httpd
mkdir -p /Volumes/MacSoft/data/webapps/lamp
docker cp centos:etc/my.cnf lamp/config/
docker cp -L centos:etc/httpd lamp/config/

mysql 目录

配置文件
-v /Volumes/MacSoft/data/lamp/config/my.cnf:/etc/my.cnf
数据目录
-v /Volumes/MacSoft/data/lamp/mysql:/var/lib/mysql
日志文件
-v /Volumes/MacSoft/data/lamp/logs:/var/log/mariadb

apache 目录

配置文件
-v /Volumes/MacSoft/data/lamp/config/httpd:/etc/httpd
webapps 目录
-v /Volumes/MacSoft/data/webapps:/var/www
日志文件
-v /Volumes/MacSoft/data/lamp/logs/httpd:/var/log/httpd

运行镜像

docker run -d -i -p 3306:3306 -p 80:80 --restart=always -h lamp --name lamp \
-v /Volumes/MacSoft/data/lamp/config/my.cnf:/etc/my.cnf \
-v /Volumes/MacSoft/data/lamp/mysql:/var/lib/mysql \
-v /Volumes/MacSoft/data/lamp/logs:/var/log/mariadb \
-v /Volumes/MacSoft/data/lamp/config/httpd:/etc/httpd \
-v /Volumes/MacSoft/data/webapps:/var/www \
-v /Volumes/MacSoft/data/lamp/logs/httpd:/var/log/httpd \
-t private/lamp:v1

docker exec -it lamp bash

cd /etc/httpd
ln -s ../../var/log/httpd logs
ln -s ../../usr/lib64/httpd/modules modules
ln -s /run/httpd run
mkdir -p /var/www/html
chown -R apache:apache /var/www/html
mkdir -p /var/run/mariadb
chown -R mysql:mysql /var/run/mariadb
/usr/sbin/httpd -DFOREGROUND &
/usr/bin/mysqld_safe --basedir=/usr &
#设置外网访问权限
mysql -e "GRANT ALL PRIVILEGES ON . TO 'root'@'%' identified by '123456' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"
#修改root 密码
'/usr/bin/mysqladmin' -u root password '123456'

docker commit -a "mir355" -m "Centos7 http mysql5 php56" lamp private/lamp:v1

Dockefile 制作镜像

将上述手动操作生成dockerfile

FROM centos

MAINTAINER John.wang 

ADD lamp.conf /etc/supervisord.conf

RUN yum -y update 
RUN yum -y install mariadb mariadb-server httpd epel-release autoconf automake wget
RUN rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
RUN yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-gd

RUN mkdir -p /var/run/mariadb
RUN chown -R mysql:mysql /var/run/mariadb
RUN chown -R mysql:mysql /var/lib/mysql 
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python get-pip.py
RUN pip install supervisor 

EXPOSE 3306 80

ENTRYPOINT ["supervisord"]

生成镜像
docker build -t lamp .

supervisord.conf ,实现服务自启动

[unix_http_server]
file=/tmp/supervisor.sock
chmod=0777

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=10MB
logfile_backups=1
loglevel=info
pidfile=/tmp/supervisord.pid
nodaemon=true
user=root

[program:Mysql]
command=/usr/bin/mysqld_safe --basedir=/usr

[program:httpd]
command=/usr/sbin/httpd -DFOREGROUND

docker-compse 编排文件 docker-compose.yaml

lamp:
  image: 'lamp'
  restart: always
  hostname: 'lamp'
  ports:
    - '3306:3306'
    - '10080:10080'
  volumes:
    - '/Volumes/MacSoft/data/lamp/config/my.cnf:/etc/my.cnf'
    - '/Volumes/MacSoft/data/lamp/mysql:/var/lib/mysql'
    - '/Volumes/MacSoft/data/lamp/logs:/var/log/mariadb'
    - '/Volumes/MacSoft/data/lamp/config/httpd:/etc/httpd'
    - '/Volumes/MacSoft/data/webapps:/var/www'
    - '/Volumes/MacSoft/data/lamp/logs/httpd:/var/log/httpd'

执行镜像
docker-compose up -d


结语

1、安装过程还算顺利,遇到docker 下systemctl 无法使用的问题,经过查找确认是启动参数问题,用如下方法启用,支持 systemctl 服务(建议调试时使用,服务运行时意义不大)
docker run --privileged --name centos -d -i -t centos /usr/sbin/init
2、通过研究,利用Dockerfie 生成镜像指定入口点。配置supervisord 实现服务自启动。