码迷,mamicode.com
首页 > 系统相关 > 详细

Linux下Dockerfile搭建lnmp(目前只有n和p)

时间:2018-05-07 14:49:09      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:docker

# 一:环境准备
centos7.4以及搭建好的Docker

[root@node01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@node01 ~]# docker -v
Docker version 17.03.2-ce, build f5ec1e2

# 二:搭建nginx和php的镜像
1.nginx
创建基础镜像

[root@node01 /]# vim Dockerfile
FROM centos

MAINTAINER liuning

CentOS-Base.repo.bak
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

/etc/yum.repos.d
COPY CentOS7-Base-163.repo /etc/yum.repos.d/CentOS7-Base-163.repo
COPY epel-release-latest-7.noarch.rpm /etc/yum.repos.d/

WORKDIR /etc/yum.repos.d/
RUN yum install -y epel-release-latest-7.noarch.rpm
RUN yum clean all

RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdate crontabs

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

生成基础镜像

[root@node01 /]# docker build -t centos_init .

在根目录下创建nginx文件夹并进入

[root@node01 /]# mkdir nginx;cd nginx

编写nginx的Dockerfile

[root@node01 nginx]# vim Dockerfile
FROM centos_init

MAINTAINER liuning

RUN useradd -M -s /sbin/nologin www
ADD nginx-1.8.1.tar.gz /usr/local/src

RUN yum install libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel

WORKDIR /usr/local/src/nginx-1.8.1
RUN ./configure --user=www --group=www --prefix=/usr/local/nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_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

COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params
RUN mkdir /usr/local/nginx/conf/vhost
COPY www.conf /usr/local/nginx/conf/vhost/www.conf
EXPOSE 80

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

从github上获取需要的文件

https://github.com/jsonhc/docker_project/tree/master/docker_dockerfile/lnmp

最终目录下有这些文件

[root@node01 nginx]# ls
Dockerfile fastcgi_params nginx-1.8.1.tar.gz nginx.conf www.conf

构建nginx镜像

[root@node01 nginx]# docker build -t nignx .

查看是否构建成功

[root@node01 nginx]# docker images | grep nginx
nginx latest f453aeaced4a About an hour ago 583 MB

2.php
在根目录下创建php文件夹并进入

[root@node01 /]# mkdir php;cd php

编写Dockerfile文件

[root@node01 php]#vim Dockerfile
FROM centos_init

MAINTAINER liuning

ADD libmcrypt-2.5.7.tar.gz /usr/local/src

WORKDIR /usr/local/src/libmcrypt-2.5.7
RUN ./configure && make && make install

ADD php-5.6.30.tar.bz2 /usr/local/src

RUN yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel

WORKDIR /usr/local/src/php-5.6.30
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd && make && make install

COPY php.ini-production /usr/local/php/etc/php.ini
COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

RUN useradd -M -s /sbin/nologin php
RUN sed -i -e ‘s@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g‘ -e ‘s@nobody@php@g‘ -e ‘s@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g‘ /usr/local/php/etc/php-fpm.conf
RUN sed -i ‘s@;daemonize = yes@daemonize = no@g‘ /usr/local/php/etc/php-fpm.conf

EXPOSE 9000

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

从github上获取需要的文件,如上最终php文件夹下有如下这些文件

[root@node01 php]# ls
Dockerfile init.d.php-fpm libmcrypt-2.5.7.tar.gz php-5.6.30.tar.bz2 php-fpm.conf.default php.ini-production

构建php镜像(过程漫长)

[root@node01 php]# docker build -t php .

查看是否构建成功

[root@node01 php]# docker images | grep php
php latest 15fe39a66344 35 minutes ago 1.12 GB

三:启动容器
启动php

[root@docker php]# docker run -d --name=php -v /www:/usr/local/nginx/html php

启动nginx

docker run -d --name=nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx

查看启动是否正常,若是容器一启动就是exited状态,则使用docker logs -f 容器名,查看报错信息

[root@node01 php]# docker ps -a

在/www下创建phpinfo和html文件进行测试

[root@node01 www]# ls
index.html test.php

四:访问测试
技术分享图片
技术分享图片

Linux下Dockerfile搭建lnmp(目前只有n和p)

标签:docker

原文地址:http://blog.51cto.com/11093860/2113499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!