使用Docker-compose搭建lnmp环境
一、下载安装docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#如果下载不动,则使用
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
#如果出现sudo docker-compose提示找不到命令,则使用
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
二、下载所需镜像
docker pull mysql:5.7
docker pull nginx:latest
docker pull php:5.6-fpm
//如果有从别的服务器倒过来的docker转为镜像也可以使用
三、编写docker-compose.yml文件
version: "3"
services:
pubfront: //nginx的docker名称
image: nginx:latest //镜像名
container_name: pubfront //dcoker名称
restart: always //自动启动
networks: //配置子网
cynetwork: //子网别名
ipv4_address: 172.18.0.2 //分配子网地址
ports:
- "8110:81" //开放映射端口
- "8111:443"
volumes: //文件路径映射
- /data/docker/pubfront/nginx/html:/var/www/html
- /data/docker/pubfront/nginx/conf.d:/etc/nginx/conf.d
- /data/docker/pubfront/home:/home
backend1: //backend1的nginx容器
image: nginx:latest
container_name: backend1
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.3
ports:
- "8010:81"
- "8013:443"
volumes:
- /data/docker/backend1/nginx/html:/var/www/html
- /data/docker/backend1/nginx/conf.d:/etc/nginx/conf.d
- /data/docker/backend1/home:/home
php1:
image: my/php:5.6-fpm-alpine //backend1的php容器
container_name: php1
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.4
ports:
- "8019:9000" //php的端口通常是9000,然后在backend1的nginx配置中配置php地址为:172.18.0.4:8019
volumes: //backend1的php和nginx映射的文件路径要统一
- /data/docker/backend1/nginx/html:/var/www/html
- /data/docker/backend1/nginx/conf.d:/etc/nginx/conf.d
- /data/docker/backend1/home:/home
backend2: //backend2同backend1配置
image: nginx:latest
container_name: backend2
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.5
ports:
- "8020:81"
- "8023:443"
volumes:
- /data/docker/backend2/nginx/html:/var/www/html
- /data/docker/backend2/nginx/conf.d:/etc/nginx/conf.d
- /data/docker/backend2/home:/home
php2:
image: my/php:5.6-fpm-alpine
container_name: php2
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.6
ports:
- "8029:9000"
volumes:
- /data/docker/backend2/nginx/html:/var/www/html
- /data/docker/backend2/nginx/conf.d:/etc/nginx/conf.d
- /data/docker/backend2/home:/home
dbnode1: //数据库配置
image: my/mysql:5.7
container_name: dbnode1
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.7
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 1234
TZ: Asia/Shanghai
ports:
- "8036:3306"
volumes:
- /data/docker/dbnode1/home:/home
- /data/docker/dbnode1/conf.d:/etc/mysql/conf.d
- /data/docker/dbnode1/data:/var/lib/mysql
- /data/docker/dbnode1/backup:/data/backup
- /data/docker/dbnode1/log:/var/log
dbnode2:
image: my/mysql:5.7
container_name: dbnode2
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.8
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 1234
TZ: Asia/Shanghai
ports:
- "8046:3306"
volumes:
- /data/docker/dbnode2/home:/home
- /data/docker/dbnode2/conf.d:/etc/mysql/conf.d
- /data/docker/dbnode2/data:/var/lib/mysql
- /data/docker/dbnode2/backup:/data/backup
- /data/docker/dbnode2/log:/var/log
dbnode3:
image: my/mysql:5.7
container_name: dbnode3
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.9
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: 1234
TZ: Asia/Shanghai
ports:
- "8056:3306"
volumes:
- /data/docker/dbnode3/home:/home
- /data/docker/dbnode3/conf.d:/etc/mysql/conf.d
- /data/docker/dbnode3/data:/var/lib/mysql
- /data/docker/dbnode3/backup:/data/backup
- /data/docker/dbnode3/log:/var/log
mycat: //mycat配置
image: mycat1:1.0
container_name: mycat
restart: always
networks:
cynetwork:
ipv4_address: 172.18.0.10
ports:
- "8066:8066"
volumes:
- /data/docker/mycat/home:/home
command: ["/home/auto_start_mycat.sh"] //运行容器时执行脚本
stdin_open: true
tty: true
networks: //子网配置
cynetwork:
external: true
四、运行compose.yml文件
docker-compose -f docker-compose.yml up -d //启动compose文件
docker-compose -f docker-compose.yml down //停止compose文件
//运行成功后:可以用以下命令查看docker状态
docker ps -a
//然后进入创好的容器命令:
docker exec -it 容器名 /bin/bash
五、用Dockerfile制作镜像
Dokcerfile文件(php镜像):
FROM php:5.6-fpm-alpine
#国内repo源,让本地构建速度更快。
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
#安装GD依赖库
&& apk update && apk add --no-cache --virtual .build-deps \
bzip2 bzip2-dev libmcrypt libxml2 openssl curl libcurl libzip libzip-dev freetype-dev libjpeg-turbo-dev libpng-dev libmcrypt-dev \
#添加php源码中的扩展,添加gd,mysqli,pdo-mysql,opcache,gettext,mcrypt等扩展
&& docker-php-ext-install -j$(nproc) iconv bz2 mysql pdo_mysql mysqli hash sockets zip sockets \
&& docker-php-ext-configure gd \
--with-freetype-dir=/usr/include \
--with-jpeg-dir=/usr/include \
--with-png-dir=/usr/include \
&& docker-php-ext-install gd \
#redis属于pecl扩展,需要使用pecl命令来安装,同时需要添加依赖的库
&& apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install redis-4.2.0 \
&& pecl install xdebug-2.5.5 \
&& docker-php-ext-enable redis xdebug \
&& apk del .phpize-deps \
#更改配置文件
&& cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini \
#安装Composer
&& curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer \
&& composer config -g repo.packagist composer https://packagist.phpcomposer.com
备注
建议将最后compose的最后的下载地址换为
https://mirrors.aliyun.com/composer/
然后运行build命令生成镜像
docker build -f Dockerfile -t my/php:5.6-fpm-alpine .
备注
如果运行出错,多尝试重新build几次
如果运行不成功可以将Dockerfile文件内安装compose区域删除