一、在Docker中安裝環(huán)境(PHP渣触、Nginx、MySQL壹若、Redis嗅钻、memcached皂冰、MongoDB)
1、安裝Docker
2养篓、查看安裝軟件版本
? ? # ??docker -v ?/ ?docker --version
? ? # ??docker-compose -v
3秃流、在代碼根目錄新建docker安裝環(huán)境所屬目錄:/data1/project/docker
4、在 /data1/project/docker 新建 init.sh 腳本
5柳弄、# ??chmod a+x init.sh
6舶胀、添加以下命令加入到??/data1/project/docker/init.sh 文件中保存(#####中的所有內(nèi)容)
##########################################################################
mkdir app
mkdir app/memcached
mkdir app/mysql
mkdir app/nginx
mkdir app/nginx/conf
mkdir app/php
mkdir app/redis
mkdir app/mongo
mkdir db
mkdir logs
mkdir tools
mkdir tools/phpmyadmin
mkdir tools/console
mkdir web
touch app/memcached/Dockerfile
touch app/mysql/Dockerfile
touch app/nginx/Dockerfile
touch app/php/Dockerfile
touch app/redis/Dockerfile
touch app/mongo/Dockerfile
touch tools/phpmyadmin/Dockerfile
touch tools/console/Dockerfile
touch app/nginx/conf/default.conf
touch app/php/opcache-recommended.ini
touch app/php/php.ini
touch app/php/proxychains.conf
touch app/php/proxychains-ng
touch docker-compose.yaml
curl -L -o ./app/php/redis.tar.gzhttps://github.com/phpredis/phpredis/archive/3.0.0.tar.gz
curl -L -o ./app/php/memcached.tar.gz "https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/v3.0.2"
curl -L -o ./app/php/mongodb.tgzhttps://github.com/mongodb/mongo-php-driver/releases/download/1.3.2/mongodb-1.3.2.tgz
cat /dev/null > docker-compose.yaml
cat >> docker-compose.yaml <
version: "2"
services:
??mysql:
????build:
??????context: ./app/mysql
??????dockerfile: Dockerfile
????environment:
??????MYSQL_ROOT_PASSWORD: "root"
??????MYSQL_DATABASE: "app"
??????MYSQL_USER: "app"
??????MYSQL_PASSWORD: "111111"
????volumes:
??????- ./db:/var/lib/mysql
??php:
????build:
??????context: ./app/php
??????dockerfile: Dockerfile
????volumes:
??????- ./web:/opt/web
??nginx:
????build:
??????context: ./app/nginx
??????dockerfile: Dockerfile
????ports:
??????- 8080:80
????depends_on:
??????- php #此處代表nginx依賴php,php會在nginx之前啟動
????volumes_from:
??????- php
????volumes:
??????- ./app/nginx/conf:/etc/nginx/conf.d
??redis:
????build:
??????context: ./app/redis
??????dockerfile: Dockerfile
#????ports:
#??????- 6379:6379
??memcached:
????build:
??????context: ./app/memcached
??????dockerfile: Dockerfile
#????ports:
#??????- 11211:11211
??mongo:
????build:
??????context: ./app/mongo
??????dockerfile: Dockerfile
#????ports:
#??????- 27017:27017
??phpmyadmin:
????build:
??????context: ./tools/phpmyadmin
??????dockerfile: Dockerfile
????ports:
??????- "8081:80"
????environment:
??????PMA_HOST: "mysql"
??????PMA_USER: "root"
??????PMA_PASSWORD: "root"
??console:
??????build:
????????context: ./tools/console
????????dockerfile: Dockerfile
??????volumes_from:
????????- php
??????tty: true
#networks:
volumes:
??db:
????driver: local
EOF
cat /dev/null > ./app/php/Dockerfile
cat >> ./app/php/Dockerfile <
FROM php:7.1.11-fpm
# 以下幾行為安裝代理工具碧注,如不需要嚣伐,可將其屏蔽
# COPY proxychains-ng /opt/proxychains-ng
# COPY proxychains.conf /etc/proxychains.conf
# WORKDIR /opt/proxychains-ng
# RUN ./configure && make && make install
# 替換為阿里云源
RUN echo "debhttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
debhttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib" > /etc/apt/sources.list
# 安裝php擴展
WORKDIR /tmp
ENV PHPREDIS_VERSION 3.0.0
COPY ./redis.tar.gz /tmp/redis.tar.gz
COPY ./memcached.tar.gz /tmp/memcached.tar.gz
COPY ./mongodb.tgz /tmp/mongodb.tgz
COPY ./yar.tgz /tmp/yar.tgz
RUN tar xfz /tmp/redis.tar.gz \
??&& rm -r /tmp/redis.tar.gz \
??&& tar xf /usr/src/php.tar.xz \
??&& mv php-7.1.11 /usr/src/php \
??&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
??&& mkdir -p /usr/src/php/ext/memcached \
??&& tar -C /usr/src/php/ext/memcached -zxf /tmp/memcached.tar.gz --strip 1 \
??&& rm /tmp/memcached.tar.gz \
??&& mkdir -p /usr/src/php/ext/mongodb \
??&& tar -C /usr/src/php/ext/mongodb -zxf /tmp/mongodb.tgz --strip 1 \
??&& rm -r /tmp/mongodb.tgz \
??&& mkdir -p /usr/src/php/ext/yar \
??&& tar -C /usr/src/php/ext/yar -zxf /tmp/yar.tgz --strip 1 \
??&& rm -r /tmp/yar.tgz
# 安裝php
RUN apt-get clean \
??&& apt-get update \
??&& apt-get -y remove libssl1.0.0 \
??&& apt-get install -y libpng12-dev libjpeg-dev libmemcached-dev libssl-dev libcurl4-openssl-dev \
??&& rm -rf /var/lib/apt/lists/* \
??&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
??&& docker-php-ext-install gd mysqli pdo_mysql zip opcache redis \
??&& docker-php-ext-configure memcached \
??&& docker-php-ext-install memcached \
??&& docker-php-ext-configure mongodb \
??&& docker-php-ext-install mongodb \
??&& docker-php-ext-configure yar \
??&& docker-php-ext-install yar \
??&& docker-php-ext-configure bcmath \
??&& docker-php-ext-install bcmath
# 生成配置文件
COPY ./php.ini /usr/local/etc/php/conf.d/
COPY ./opcache-recommended.ini /usr/local/etc/php/conf.d/
EOF
echo "FROM nginx:1.12.1" > ./app/nginx/Dockerfile
echo "FROM mysql:5.7" > ./app/mysql/Dockerfile
echo "FROM memcached:1.5" > ./app/memcached/Dockerfile
echo "FROM redis:4.0.0" > ./app/redis/Dockerfile
echo "FROM mongo:3.5.13" > ./app/mongo/Dockerfile
echo "FROM phpmyadmin/phpmyadmin:4.7" > ./tools/phpmyadmin/Dockerfile
cat >> ./tools/console/Dockerfile <
FROM php:7.1.11
WORKDIR /mnt/app
# 替換為阿里云源
RUN echo "debhttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
debhttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib" > /etc/apt/sources.list
# 常用工具
RUN apt-get update && apt-get install -y git curl wget cron vim locales libfreetype6-dev mariadb-client \\
??&& rm -rf /var/lib/apt/lists/* \\
??&& docker-php-ext-install zip \\
??&& docker-php-ext-install mysqli pdo_mysql
# 把語言設(shè)置成簡體中文
RUN dpkg-reconfigure locales && \\
??locale-gen C.UTF-8 && \\
??/usr/sbin/update-locale LANG=C.UTF-8
RUN echo 'zh_CN.UTF-8 UTF-8' >> /etc/locale.gen && \\
??locale-gen
ENV LC_ALL C.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN.UTF-8
RUN php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \\
??&& php composer-setup.php \\
??&& php -r "unlink('composer-setup.php');" \\
??&& mv ./composer.phar /usr/local/bin/composer \\
??&& chmod +x /usr/local/bin/composer
RUN echo 'export PATH="\$PATH:\$HOME/.composer/vendor/bin"' >> ~/.bashrc \\
??&& . ~/.bashrc \\
??&& composer config -g repo.packagist composerhttps://packagist.phpcomposer.com
EOF
echo "opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=1
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.enable=0" > ./app/php/opcache-recommended.ini
echo "memory_limit = 256M
post_max_size = 100M
upload_max_filesize = 100M" > ./app/php/php.ini
cat >> ./app/nginx/conf/default.conf <
server {
??listen????????80;
??server_name???localhost;
??root??????????/opt/web;
??index?????????index.php index.html index.htm;
??location / {
?????try_files \$uri \$uri/ /index.php?\$query_string;
???}
??location ~ \.php$ {
?????fastcgi_pass???php:9000;
?????fastcgi_index??index.php;
?????fastcgi_param??SCRIPT_FILENAME??\$document_root\$fastcgi_script_name;
?????include????????fastcgi_params;
??}
}
EOF
##########################################################################
7、當前 /data1/project/docker 目錄下運行init.sh腳本
?? ?#? ./init.sh
8萍丐、若下載超時 或 失敗 轩端,多次執(zhí)行? #? ./init.sh 命令即可
9、執(zhí)行docker-compose up -d?來啟動環(huán)境逝变、并安裝
10基茵、確保Mac/Linux環(huán)境端口8080未被占用,執(zhí)行期間若error報錯端口已占用壳影,則更改Mac/Linux環(huán)境端口拱层,不被占用即可
11、Mac/Linux環(huán)境端口改完重啟當前系統(tǒng)宴咧,執(zhí)行
#????docker-compose down
12根灯、再次執(zhí)行編譯安裝
#????docker-compose up -d
13、查看已存在鏡像
#????docker images
14悠汽、查看服務(wù)是否已啟動
#????docker ps -a
此時可以查看到已經(jīng)安裝:nginx箱吕、mongo、php柿冲、mysql茬高、redis、phpmyadmin假抄、memcached
15怎栽、如果后續(xù)增加PHP擴展,vi /data1/project/docker/app/php/Dockerfile?增加擴展宿饱,然后關(guān)閉熏瞄,再啟動并且編譯
#? ? docker-compose down
#? ? docker-compose up -d --build
16、若要遠程連接mysql谬以、memcache强饮、redis ........? ? 則編輯? vi /data1/project/docker/docker-compose.yaml
(截圖范圍有限,若要遠程連接为黎,都增加端口開放即可)
17邮丰、關(guān)閉容器行您,在重啟容器,查看當前所有容器的端口均已開啟
#? ? docker-compose down
#? ? docker-compose up -d
#????docker ps? -a
18剪廉、測試mysql連接虛擬機Docker中的MySQL
打開Navicat(此處的3307端口是 /data1/project/docker/docker-compose.yaml 中MySQL增加)
IP:10.0.1.145
port:3307
username:root
password:root
二娃循、在Docker內(nèi)配置Nginx開發(fā)環(huán)境設(shè)置(此處配置laravel框架路由)
注意:默認安裝的容器訪問根目錄為:/opt/web/
1、Nginx配置文件默認為:/data1/project/docker/app/nginx/conf/default.conf
server {
? listen??????? 80;
? server_name? ?docker.laravel.com;
? root????????? /opt/web/laravel/public;
? index ??????? index.php index.html index.htm;
? location / {
???? try_files $uri $uri/ /index.php?$query_string;
? }
? location ~ \.php$ {
???? fastcgi_pass ? php:9000;
???? fastcgi_index? index.php;
???? fastcgi_param? SCRIPT_FILENAME? $document_root$fastcgi_script_name;
???? include??????? fastcgi_params;
? }
}
2斗蒋、重啟docker Nginx 環(huán)境
#????docker restart docker_nginx_1
17捌斧、本地主機配置host域名:127.0.0.1? docker.laravel.com? ,所有配置文件訪問均已映射到宿主機:8080端口去訪問docker:80端口泉沾,本地瀏覽器訪問
eg:docker.laravel.com:8080
18捞蚂、本地docker目錄:/data1/project/docker/web?與任意容器目錄:/opt/web/形成互相映射,在本地目錄或者容器目錄操作項目文件即可
19爆哑、進入docker_php_1容器
#????docker exec -it docker_php_1 /bin/bash
或
#????docker exec -it docker_php_1 bash