`
liu86th
  • 浏览: 117917 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[docker] Docker构建LNMP笔记

 
阅读更多
 Dockerfile构建LNMP环境
 
 1. 先构建一个Nginx/PHP/Mysql共用的Centos基础镜像centos:v1

[1] Dockerfile内容如下:
#base image
FROM centos

#maintainer
MAINTAINER liu86th@sina.com

#add epel and 163 aliyun yum
RUN yum install wget epel-release -y \
    && mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back \
    && wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo \
    && wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo \
    && yum clean all \
	&& yum makecache \
    && yum -y install pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel

#change timzone to Asia/Shanghai
#RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

[2] 执行构建命令
[root@server ~]# docker build -t centos:v1 .
[root@server ~]# docker images

2. 构建Nginx

[1] Dockerfile内容如下:
# base image
FROM centos:v1

MAINTAINER liu86th@sina.com

ADD nginx-1.17.2.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.17.2

# 安装依赖包
RUN groupadd www && useradd -M -s /sbin/nologin www -g www \
    && yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel libxml2 libxml2-dev libxslt-devel \
    && ./configure --user=www --group=www --prefix=/usr/local/nginx --with-file-aio --with-ipv6 --with-http_ssl_module  --with-http_v2_module --with-http_realip_module    --with-http_addition_module    --with-http_xslt_module   --with-http_image_filter_module    --with-http_geoip_module  --with-http_sub_module  --with-http_dav_module --with-http_flv_module    --with-http_mp4_module --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_auth_request_module  --with-http_random_index_module   --with-http_secure_link_module   --with-http_degradation_module   --with-http_stub_status_module && make && make install \
	&& mkdir /usr/local/nginx/conf/conf.d
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params

EXPOSE 80

# docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker容器pid=1挂了,那么docker容器便会直接退出。
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

[2] 构建镜像
[root@server ~]# docker build -t nginx:v1 .
[root@server ~]# docker images
[root@server ~]# docker run -d --name nginx -p 8888:80 -v E:\Docker\wwwroot:/home/wwwroot --link=php:php nginx:v1

3. 构建PHP镜像

[1] Dockerfile内容如下:
# base image
FROM centos:v1

MAINTAINER liu86th@sina.com

#安装libzip依赖包
ADD libzip-1.2.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/libzip-1.2.0
RUN ./configure && make && make install

#安装依赖包和编译php7.3,并设置php-fpm进程前台运行,与容器生命周期绑定
ADD php-7.3.8.tar.gz /usr/local/src
WORKDIR /usr/local/src/php-7.3.8
RUN yum -y install libxml2 libxml2-devel libzip bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel \
    && echo '/usr/local/lib64' >> /etc/ld.so.conf && echo '/usr/local/lib' >> /etc/ld.so.conf && echo '/usr/lib' >> /etc/ld.so.conf && echo '/usr/lib64' >> /etc/ld.so.conf && ldconfig -v && cp -frp /usr/lib64/libldap* /usr/lib/ && cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h \
    && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv --with-iconv-dir  --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-sysvshm --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-libmbfl --enable-fileinfo --enable-opcache --with-xsl --enable-calendar --enable-exif --with-pcre-regex && make && make install \
    && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
	&& groupadd www && useradd -M -s /sbin/nologin -g www www \
    && sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@www@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf \
    && sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf

EXPOSE 9000

CMD ["/usr/local/php/sbin/php-fpm"]

#注意: 上面的sed,有可能不准确,可能需要手动修改www.conf中的listen = 127.0.0.1:9000 => listen = 0.0.0.0:9000的监听端口变化

[2] 构建镜像
[root@server ~]# docker build -t php:v1 .
[root@server ~]# docker images
[root@server ~]# docker run -d --name php -v E:\Docker\wwwroot:/home/wwwroot php:v1

3. 构建Mysql,因为本地使用所以直接用官方的镜像启动

[1] 创建容器前,我们创建一个数据卷mysql-volume,把它挂载到mysql容器中,实现数据持久化
[root@server ~]# docker volume  create  mysql-volume

[2] 启动mysql容器,如果你本地有mysql镜像,它会引用本的的镜像,如果没有它会去docker hub中拉取:
[root@server ~]# docker run -itd  --name mysql --network lnmp -p 13306:3306 --mount src=mysql-volume,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

4. 运行容器

[1] 先创建一个自定义网络,运行ningx、php这些容器的时候加入到lnmp网络中来:
[root@server ~]# docker network ls
[root@server ~]# docker network  create  lnmp

[2] 运行php
[root@server ~]# docker run -d --name php --network lnmp -v E:\Docker\wwwroot:/home/wwwroot -v E:\Docker\phplogs:/usr/local/php/var/log -v E:\Docker\phpconf:/usr/local/php/etc php:v1

[3] 运行nginx
[root@server ~]# docker run -d --name nginx --network=lnmp -p 8888:80 -v E:\Docker\wwwroot:/home/wwwroot -v E:\Docker\wwwlogs:/usr/local/nginx/logs -v E:\Docker\wwwconf:/usr/local/nginx/conf nginx:v1 

[4] 运行mysql
[root@server ~]# docker run -itd  --name mysql --network lnmp -p 13306:3306 --mount src=mysql-volume,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics