在构建的前提下保证docker和docker-compose安装没问题
docker-compose.yml内容
version: "1.0.0"networks: #添加网络 web-net: driver: bridgeservices: php: image: php-test:7.4-fpm container_name: php-fpm-1 privileged: true #设置容器权限为root volumes: - /home/www/project1:/var/www/html networks: - web-net nginx: image: nginx:1.15 container_name: php-nginx-1 privileged: true ports: - "8080:80" volumes: - /home/www/project1:/usr/share/nginx/html - /home/log/nginx:/var/log/nginx - /home/conf/nginx/conf.d:/etc/nginx/conf.d networks: - web-net restart: always links: - php-fpm-1:php mysql: image: mysql:5.7 container_name: php-mysql-1 privileged: true ports: - "3307:3306" volumes: - /home/data/mysql:/var/lib/mysql - /home/conf/mysql:/etc/mysql/conf.d - /home/log/mysql:/var/log environment: - MYSQL_ROOT_PASSWORD=123456 networks: - web-net redis: image: redis:3.0 container_name: php-redis-master privileged: true ports: - "6375:6379" volumes: - /home/conf/redis/redis-master/myredis.conf:/etc/redis/redis.conf - /home/data/redis/redis-master:/data networks: - web-net restart: always command: redis-server /etc/redis/redis.conf #已对应的配置文件启动 redis1: image: redis:3.0 container_name: php-redis-slave-1 privileged: true ports: - "6376:6379" volumes: - /home/conf/redis/redis-slave1/myredis.conf:/etc/redis/redis.conf - /home/data/redis/redis-slave-1:/data networks: - web-net restart: always links: - php-redis-master:redis-master #连接到主redis command: redis-server /etc/redis/redis.conf redis2: image: redis:3.0 container_name: php-redis-slave-2 privileged: true ports: - "6377:6379" volumes: - /home/conf/redis/redis-slave2/myredis.conf:/etc/redis/redis.conf - /home/data/redis/redis-slave-2:/data networks: - web-net restart: always links: - php-redis-master:redis-master command: redis-server /etc/redis/redis.conf
在运行前安装相关文件目录
sudo mkdir -p /home/data/mysql && mkdir /home/data/redis && mkdir -p /home/conf/nginx && mkdir /home/conf/mysql && mkdir /home/conf/redis && mkdir /home/conf/redis/redis-master && mkdir /home/conf/redis/redis-slave1 && mkdir /home/conf/redis/redis-slave2 && mkdir -p /home/conf/php && mkdir -p /home/log/nginx && mkdir /home/log/redis && mkdir /home/log/mysql && mkdir /home/log/php && mkdir -p /home/www/project1
nginx的default.conf如下文件,内容放到/home/conf/nginx/conf.d目录下面(注意:我在/usr/share/nginx/html目录后面加了个pubulic主要是为了运行tp框架用的你们测试可以不建)
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html/public; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html/public; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/public/$fastcgi_script_name; include fastcgi_params; }}
/home/conf/redis/redis-master/myredis.conf 内容如下
port 6379timeout 65maxclients 10000databases 16maxmemory 1048576000appendonly yes
/home/conf/redis/redis-slave1/myredis.conf 内容如下
port 6379timeout 65maxclients 10000databases 16maxmemory 1048576000appendonly yesslaveof php-redis-master 6379 #我用--link中php-redis-master的别名redis-master没生效不知道什么原因所以直接用了容器名
/home/conf/redis/redis-slave2/myredis.conf 内容如下
timeout 65maxclients 10000databases 16maxmemory 1048576000appendonly yesslaveof php-redis-master 6379
现在的php-fpm镜像中没有mysql-pdo和redis扩展PHP环境不能链接到数据库和redis所有我们可以直接重新构建新的php-fpm镜像在yml中的php镜像直接用下面重新构建的镜像
FROM php:7.4-fpmRUN docker-php-ext-install pdo_mysql \&& curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/5.3.6.tar.gz \&& tar xfz /tmp/redis.tar.gz \&& rm -r /tmp/redis.tar.gz \&& mkdir -p /usr/src/php/ext \&& mv phpredis-5.3.6 /usr/src/php/ext/redis \&& docker-php-ext-install redis
(注意:新的php镜像名字与yml文件中php镜像名字保持一致)
上面所有步骤完成后运行:sudo docker-compose up -d
运行:sudo dokcer ps 可以看到相关容器都全部正常启动
查看redis主从配置也没有问题
1 这是主redis
2 这是从redis1
3 这是从redis2
thinkphp6的入口代码如下
thinkphp6的env配置内容
运行结果
如果不想提前安装php相关扩展也可以在php容器启动后重新进入容器安装
执行: docker-compose exec php bash
/usr/local/bin/docker-php-ext-install mysqli pdo_mysql #安装mysqli和pdo_mysql扩展
cd /usr/local/etc/php #进入php配置文件目录
mv php.ini-development php.ini #创建php.ini
apt-get update #更新软件源列表
apt-get install vim #安装vim
vim /usr/local/etc/php/php.ini 修改php.ini配置文件
修改 php.ini 扩展目录 extension_dir="/usr/local/lib/php/extensions/no-debug-non-zts-20180731/"
; 打开 mysqli 扩展 extension=mysqli
;extension=pdo_mysql
; 然后退出容器
重启容器 docker-compose restart
目前遇到三个问题还不知道什么原因的知道的可以告知下谢谢
1 我自己宿主机上的redis配置文件如果命名成是redis.conf挂载到容器中找不到报错:/etc/redis/redis.conf not found (好像把我的redis.conf当成文件夹了)
2 redis从机配置文件中用的--link 的别名后再从容器中的hosts文件中找不到(不知道是不是缓存的 原因)
3 docker-composer restart命令重启有缓存修改配置后不生效
来源地址:https://blog.csdn.net/palma_hezhengjie/article/details/126360546