多个docker镜像部署lnmp开发环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多个docker镜像部署lnmp开发环境相关的知识,希望对你有一定的参考价值。

与其说是lnmp环境,还不如说是3个docker环境的mysqlphpnginx镜像。下面我们将以依次按照mysql、php和nginx镜像的顺序描述它们的搭建过程:

环境要求:
docker-compose
docker-ce
centos7 2CPU 4Gb 50G

1、mysql

这里采用官方mysql:5.7的镜像,首先需要创建mysqld.conf文件,生产环境的mysql配置需要自行优化,这里囧供开发环境使用。

[[email protected] lnmp]# cat /data/mysql/conf/mysqld.cnf |grep -v ^#|grep -v ^$
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
max_connections=200
character-set-server=utf8
skip-name-resolve
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
default-time_zone=‘+8:00‘
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[[email protected] lnmp]#

启动mysql容器

docker run --name lnmp_mysql -p 3306:3306 --privileged=true -v /data/mysql/data:/var/lib/mysql -v /data/mysql/conf:/etc/mysql/mysql.conf.d -e MYSQL_ROOT_PASSWORD=root -d 10.0.2.10/library/mysql:5.7

说明:10.0.2.10/library/mysql:5.7 这个镜像其实和 mysql:5.7官方的一样,只是将他打了个tag push到自己的harbor仓库永久保存,方便下载使用。

2、php

这里采用Dockerfile的模式,基于centos:7的官方镜像制作,直接上Dockerfile-php文件:

[[email protected] lnmp]# cat Dockerfile-php
From centos:7
MAINTAINER zhujinbing
RUN yum install epel-release -y &&
yum install -y gcc gcc-c++ make gd-devel libxml2-devel
libcurl-devel libjpeg-devel libjpeg libpng-devel libpng openssl-devel
libmcrypt-devel libmcrypt mcrypt mhash libxslt-devel libtidy-devel autoconf
iproute net-tools telnet wget curl &&
yum clean all &&
rm -rf /var/cache/yum/*

ADD php-5.6.36.tar.gz /
RUN cd php-5.6.36 &&
./configure --prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--enable-fpm --enable-opcache
--with-mysql --with-mysqli --with-pdo-mysql
--with-openssl --with-zlib --with-curl --with-gd
--with-jpeg-dir --with-png-dir --with-freetype-dir
--enable-mbstring --with-mcrypt --enable-hash &&
make -j 4 && make install

RUN cp /php-5.6.36/php.ini-production /usr/local/php/etc/php.ini
RUN cp /php-5.6.36/sapi/fpm/php-fpm.conf.in /usr/local/php/etc/php-fpm.conf
RUN useradd -M -s /sbin/nologin php
RUN sed -i -e ‘s;pid = run/php-fpm.pidpid = run/php-fpm.pidg‘ -e ‘slisten = 127.0.0.1:9000listen = 0.0.0.0:9000g‘ /usr/local/php/etc/php-fpm.conf
RUN sed -i ‘s;daemonize = yesdaemonize = nog‘ /usr/local/php/etc/php-fpm.conf
RUN sed -i ‘s;error_log = log/php-fpm.logerror_log = /usr/local/php/log/php-fpm.logg‘ /usr/local/php/etc/php-fpm.conf
RUN sed -i -e ‘suser = @[email protected]user = phpg‘ -e ‘sgroup = @[email protected]group = phpg‘ /usr/local/php/etc/php-fpm.conf
RUN sed -i -e ‘s;date.timezone =date.timezone = Asia/Shanghaig‘ /usr/local/php/etc/php.ini

RUN mkdir /usr/local/php/log &&
cd / && rm -rf php* &&
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/php/sbin:/usr/local/php/bin
#COPY php.ini /usr/local/php/etc/
#COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm"]
[[email protected] lnmp]#

php-5.6.36.tar.gz文件自行从官网php.net下载,放到dockfile-php同一级目录。
curl -L "https://www.php.net/distributions/php-5.6.36.tar.gz" -o php-5.6.36.tar.gz

根据dockerfile-php文件制作自己的镜像文件:
docker build -t 10.0.2.10/library/php:5.6.36 -f Dockerfile-php .

看到后面Successfully说明镜像制作成功:
技术图片

下面我们看一下刚刚制作的镜像:
技术图片

启动php容器:

docker run --name lnmp_php --link=lnmp_mysql:lnmp_mysql -d -v /root/lnmp/wwwroot:/home/wwwroot -v /root/lnmp/wwwlogs:/home/wwwlogs -v /root/lnmp/tmp:/tmp -v /etc/localtime:/etc/localtime --privileged=true 10.0.2.10/library/php:5.6.36

3、nginx

这里采用官方nginx:1.15.11来启动。
先在宿主机创建 /root/lnmp/nginx_conf/default.conf配置文件:
[[email protected] lnmp]# cat /root/lnmp/nginx_conf/default.conf |grep -v ^#|grep -v ^$
server {
listen 80;
server_name localhost;
access_log /home/wwwlogs/access.log main;
location / {
root /home/wwwroot;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php$ {
root /home/wwwroot;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[[email protected] lnmp]#

启动nginx容器

docker run
--name lnmp_nginx
--link=lnmp_php:phpfpm
--link=lnmp_mysql:mysql
-d -p 8088:80
-v /root/lnmp/wwwroot:/home/wwwroot
-v /root/lnmp/nginx_conf:/etc/nginx/conf.d
-v /root/lnmp/wwwlogs:/home/wwwlogs
10.0.2.10/library/nginx:1.15.11

说明:10.0.2.10/library/nginx:1.15.11 这个镜像其实和官方 nginx:1.15.11的一样,只是将他打了个tag push到自己的harbor仓库永久保存,方便下载使用。

镜像和容器信息截图

1、镜像信息:
技术图片

2、容器信息:
技术图片

测试验收

1、静态页面测试
[[email protected] lnmp]# cat wwwroot/index.html
hello php

技术图片

2、php动态页面测试
[[email protected] lnmp]# cat wwwroot/index.php
<?php
echo "hello php";
phpinfo();
?>
技术图片

3、数据库连接测试
[[email protected] lnmp]# cat wwwroot/mysql.php
<form action="" method="post">
<select name="host">
<option value="localhost" selected>localhost</option>
<option value="127.0.0.1">127.0.0.1</option>
<option value="lnmp_mysql">lnmp_mysql</option>
</select>
<br><br>
user:<input type="text" name="user" value=""><br><br>
pwd :<input type="password" name="pwd" value=""><br><br>
<input type="submit" value="connent">
<input type="reset" value="reset">
</form>
<?php
error_reporting(~E_ALL);
$host = $_POST[‘host‘];
$user = $_POST[‘user‘];
$pwd = $_POST[‘pwd‘];
if(isset($_POST[‘host‘]) && isset($_POST[‘user‘]) && isset($_POST[‘pwd‘])){
if(strlen($host)<1 or strlen($user)<1 or strlen($pwd)<1){
echo "请完善数据库链接信息";
exit(0);
}
$conn = mysql_connect($host, $user, $pwd) or die("Error数据库连接失败");
if($conn){
echo "OK数据库连接成功";
}
}
?>

技术图片

参考链接:
https://idc.wanyunshuju.com/Dr/572.html
https://www.cnblogs.com/mushou/p/9489479.html/php-5.6.36/sapi/fpm/php-fpm.conf.in
https://blog.51cto.com/yw666/1890598
https://www.cnblogs.com/myall/p/4792402.html

以上是关于多个docker镜像部署lnmp开发环境的主要内容,如果未能解决你的问题,请参考以下文章

DockerFile部署lnmp+memcached+redis+mongodb开发环境for Memcached

DockerFile部署lnmp+memcached+redis+mongodb开发环境for MongoDB

DockerFile部署lnmp+memcached+redis+mongodb开发环境for MySQL

DockerFile部署lnmp+memcached+redis+mongodb开发环境for Redis

DockerFile部署lnmp+memcached+redis+mongodb开发环境for Nginx

DockerFile部署lnmp+memcached+redis+mongodb开发环境for PHP