docker lnmp

整个环境的结构是lnamp,nginx为单独容器做方向代理并配置HTTPS协议,Apache和PHP在同一个容器,配合PHP组件化开发的composer为一个单独容器,Redis与Mysql分别为单独容器。

镜像版本:

  • Ubuntu:16.04(上面跑Apache-PHP)

  • PHP:7.1.0(上面跑Composer)

  • Mysql:8.0

  • Redis:3.2.6

  • Nginx:latest

这些镜像拉下来以后首先配置composer的容器:

DockerFile

composer

FROM php:7.1.0

更新apt-get

RUN apt-get update

安装git

RUN apt-get -qqy install git

把composer从官网拉下来

RUN php -r “copy(‘https://getcomposer.org/insta…‘, ‘composer-setup.php’);”

验证这个composer是否是正版可用的

RUN php -r “if (hash_file(‘SHA384’, ‘composer-setup.php’) === ’55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30′) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”

运行composer安装程序

RUN php composer-setup.php –install-dir=/bin –filename=composer

删除composer安装程序

RUN php -r “unlink(‘composer-setup.php’);”

将composer的镜像源替换成国内的

RUN composer config -g repo.packagist composer https://packagist.phpcomposer…
在composer的Dockerfile目录下编译img镜像

docker build -t xdh/composer:1.0 .
完成以后可以运行命令进去查看一下,看看composer是否可以正常运行:

docker run –rm –name composer -it 容器ID /bin/bash
然后配置Ubuntu的Apache和PHP环境:

DockerFile

php-apache2

FROM ubuntu:16.04

更新apt-get镜像源

RUN apt-get update

安装curl

RUN apt-get -qqy install curl

安装Apache2

RUN apt-get -qqy install apache2

安装php

RUN apt-get -qqy install php

安装libapache2-mod-php

RUN apt-get -qqy install libapache2-mod-php

安装php-mysql

RUN apt-get -qqy install php-mysql

安装php-gd

RUN apt-get -qqy install php-gd

端口开放80

EXPOSE 80

前台运行Apache,有错误会将日志写入docker log

CMD [“/usr/sbin/apachectl”,”-D”,”FOREGROUND”]
在php-Apache的Dockerfile目录下编译img镜像

docker build -t xdh/php-apache2:1.0 .
完成以后可以将端口转发到宿主机查看一下,看看容器内部的Apache和PHP是否可以正常运行:

docker run –rm -d -p 80:80 -v /var/www:/var/www/html xdh/php-apache2:1.0
之后配置Redis和Mysql容器将他们实现互连:

docker run –name redis-server -d -P redis:3.2.6
docker run –name mysql -d -P -e MYSQL_ROOT_PASSWORD=你的密码 mysql:8.0
docker run –name apache-php -d -v /var/www:/var/www -v ${HOME}/docker/apache/etc/apache2/sites-available:/etc/apache2/sites-available –link mysql:mysql –link redis-server:redis-server xdh/php-apache2:1.0

mac下需要下面这个命令

docker run –name apache-php -d -v ${HOME}/docker/var/www:/var/www -v ${HOME}/docker/apache/etc/apache2/sites-available:/etc/apache2/sites-available –link mysql:mysql –link redis-server:redis-server xdh/php-apache2:1.0

完成以后可以将端口转发到宿主机查看一下,看看容器内部的Apache和PHP是否可以正常运行:
docker run –rm -d -p 80:80 -v /var/www:/var/www/html xdh/php-apache2:1.0
然后配置Nginx:

DockerFile

nginx

FROM nginx:latest
WORKDIR /etc/nginx
ADD cert cert/
ADD conf.d/chaoniu.conf conf.d/chaoniu.conf
VOLUME /etc/nginx/conf.d
RUN mkdir /var/www
EXPOSE 80
EXPOSE 443
CMD [“nginx”,”-g”,”daemon off;”]
在nginx的Dockerfile目录下编译img镜像

docker build -t xdh/nginx:1.0 .
之后配置Redis和Mysql容器将他们实现互连:

docker run –name redis-server -d -P redis:3.2.6
docker run –name mysql -d -P -e MYSQL_ROOT_PASSWORD=你的密码 mysql:8.0
docker run –name apache-php -d -v /var/www:/var/www -v ${HOME}/docker/apache/etc/apache2/sites-available:/etc/apache2/sites-available –link mysql:mysql –link redis-server:redis-server xdh/php-apache2:1.0

mac下需要下面这个命令

docker run –name apache-php -d -v ${HOME}/docker/var/www:/var/www -v ${HOME}/docker/apache/etc/apache2/sites-available:/etc/apache2/sites-available –link mysql:mysql –link redis-server:redis-server xdh/php-apache2:1.0

docker run -d –name nginx -p 8080:80 -p 443:443 -v ${HOME}/docker/nginx/etc/nginx/conf.d:/etc/nginx/conf.d –link apache-php xdh/nginx:1.0
服务器命令运行

composer

docker run –rm –name composer -v /var/www:/var/www -it 8a14 /bin/bash

php-apache

docker run –name chaoniu1 -d -v /var/www:/var/www –link redis-server:redis-server xdh/php-apache2:1.4
docker run –name chaoniuapi1 -d -v /var/www:/var/www –link redis-server:redis-server xdh/php-apache2:1.4
docker run –name chaoniurank1 -d -v /var/www:/var/www –link redis-server:redis-server xdh/php-apache2:1.4
docker run –rm –name chaoniurank2 -it -v /var/www:/var/www –link redis-server:redis-server xdh/php-apache2:1.4 /bin/bash

nginx

docker run -d –name nginx -p 80:80 -p 443:443 –link chaoniuapi1 –link chaoniu1 –link chaoniurank1 xdh/nginx:1.1

    原文作者:假装我是程序猿
    原文地址: https://segmentfault.com/a/1190000009280399
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞