整個環境的結構是lnamp,nginx爲單獨容器作方向代理並配置HTTPS協議,Apache和PHP在同一個容器,配合PHP組件化開發的composer爲一個單獨容器,Redis與Mysql分別爲單獨容器。php
鏡像版本:html
Ubuntu:16.04(上面跑Apache-PHP)mysql
PHP:7.1.0(上面跑Composer)nginx
Mysql:8.0git
Redis:3.2.6redis
Nginx:latestsql
這些鏡像拉下來之後首先配置composer的容器:docker
DockerFileapache
FROM php:7.1.0ubuntu
RUN apt-get update
RUN apt-get -qqy install git
RUN php -r "copy('https://getcomposer.org/insta...', 'composer-setup.php');"
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;"
RUN php composer-setup.php --install-dir=/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"
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
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -qqy install curl
RUN apt-get -qqy install apache2
RUN apt-get -qqy install php
RUN apt-get -qqy install libapache2-mod-php
RUN apt-get -qqy install php-mysql
RUN apt-get -qqy install php-gd
EXPOSE 80
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
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
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
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
服務器命令運行
docker run --rm --name composer -v /var/www:/var/www -it 8a14 /bin/bash
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
docker run -d --name nginx -p 80:80 -p 443:443 --link chaoniuapi1 --link chaoniu1 --link chaoniurank1 xdh/nginx:1.1