NGINX
一, 准备好源码包和配置文件
1, 目录结构如下
- [root@ecs-5704-0004 nginx]# tree ./
- ./
├── Dockerfile
├── nginx-1.14.2.tar.gz
└── nginx.conf
2,nginx.conf 里的配置信息主要如下
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- location ~ \.PHP$ {
- root html;
- fastcgi_pass lnmp_php:9000;
- fastcgi_index index.PHP;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
其他的配置信息都是默认的配置, 主要更改这个位置, 它是与 PHP 建立连接的 location 模块, 其中 fastcgi_pass 相当于 proxy_pass,lnmp_php 是我们后面创建 PHP 的 docker 名字, 因为我后面会把所有的服务器都加入到同一个网络中, 所以可以直接这么写.
3, 写 Dockerfile, 内容如下:
- FROM docker.io/CentOS
- LABEL "auth"="Uncle.Joker" "mail"="unclejoker520@163.com"
- ENV TIME_ZOME Asia/Shanghai
- RUN yum install -y gcc gcc-c++ make openssl-devel prce-devel
- ADD nginx-1.14.2.tar.gz /opt/
- RUN cd /opt/nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make -j 4 && make install
- #-- 清理系统
- RUN rm -rf /opt/nginx* && yum clean all && echo "${TIME_ZOME}"> /etc/timezone && ln -sf /usr/share/zoneinfo/${
- TIME_ZOME
- } /etc/localtime
- COPY nginx.conf /usr/local/nginx/conf/
- WORKDIR /usr/local/nginx/
- EXPOSE 80
- CMD ["./sbin/nginx","-g","daemon off;"]
daemon off 的作用就是让 nginx 在前台运行, 因为 docker 和应用是共存亡的, 如果是在后台运行, 运行结束后, docker 会认为你这个应用已经运行完了, 所以 docker 就结束了.
4,Build 镜像
cd /root/dockerfile/nginx && docker bulid -t mynginx:1.14.2 .
然后查看镜像
- # docker image ls mynginx
- mynginx 1.14.2 ad10dee662f1 46 hours ago 392 MB
5, 运行镜像
- # 运行镜像之前先创建一个网络
- # docker network create lnmp
- # 运行镜像
- # docker run -itd --name lnmp_nginx --network lnmp -p 8888:80 -v /root/dockerfile/App/PHP/wwwroot:/usr/local/nginx/HTML mynginx:1.14.2
6, 测试一下能否正常访问
# cd /root/dockerfile/App/PHP/wwwroot && echo 'Dockerfile test'>index.HTML
PHP
一, 准备源码包和配置
1, 目录结构如下
- [root@ecs-5704-0004 PHP]# tree ./
- ./
├── Dockerfile
├── PHP-7.0.32.tar.gz
└── PHP.INI
2,Dockerfile 里的内容如下:
- FROM docker.io/CentOS
- LABEL 'auth'='Uncle.Joke' 'mail'='unclejoker520@163.com'
- ENV TIME_ZOME Asia/Shanghai
- RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
- ADD PHP-7.0.32.tar.gz /opt/
- RUN cd /opt/PHP-7.0.32 && ./configure --prefix=/usr/local/PHP --with-config-file-path=/usr/local/PHP/etc --with-MySQL --with-mysqli --with-openssl --with-zlib --with-curl --with-gd --with-jpeg-dir --with-PNG-dir --with-iconv --enable-fpm --enable-zip --enable-mbstring && make -j 4 && make install
- RUN cp /usr/local/PHP/etc/PHP-fpm.conf.default /usr/local/PHP/etc/PHP-fpm.conf && sed -i 's/;daemonize = yes/daemonize = no/g' /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 && sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/PHP/etc/PHP-fpm.d/www.conf && echo "${TIME_ZOME}"> /etc/timezone && ln -sf /usr/share/zoneinfo/${
- TIME_ZOME
- } /etc/localtime
- RUN rm -rf /opt/PHP* && yum clean all
- WORKDIR /usr/local/PHP/
- EXPOSE 9000
- CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]
3,Build 镜像
# cd /root/dockerfile/PHP && docker build -t myphp:7.0.32_v3 .
查看一下镜像
myphp 7.0.32_v3 b3c32cede454 43 hours ago 1.05 GB
4, 运行 PHP
- # run -itd --name lnmp_php --network lnmp -v /root/dockerfile/App/PHP/wwwroot:/usr/local/nginx/HTML myphp:7.0.32_v3
- MySQL
MySQL 没有去做专门的源码编译, 太慢了, 不过可以下载二进制包进行, 和上面 nginx 的 Dockerfile 配置差不到多少, 我这里就直接拉取的官方 mysql5.7 的镜像
1, 创建一个存储卷
# docker volume create MySQL-volume
2, 拉去镜像启动容器
# docker run -itd --name lnmp_mysql --network lnmp -p 3306:3306 -v MySQL-volume:/var/lib/MySQL -e MYSQL_ROOT_PASSWORD=lnmp123456 docker.io/CentOS/MySQL-57-centos7
官方镜像需要在拉去的时候指定一个密码
3, 测试创建数据库
# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"-e"create database wordpress"'
4, 查看数据库
# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"-e"show databases"'docker exec lnmp_mysql sh -c'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"show databases"'
5, 可以在本地查看一下是否同步
# ls /var/lib/docker/volumes/MySQL-volume/_data/
来源: http://www.bubuko.com/infodetail-2916057.html