-
docker-compose部署php项目实例详解
在本篇文章里小编给大家整理了关于docker-compose部署php项目的相关实例以及代码内容,有需要的朋友们可以学习参考下。
1.制定特定扩展的PHP镜像
sudo mkdir -p /www/docker
sudo cd /www/docker
sudo vi Dockerfile
- FROM php:7.2-fpm-alpine
- MAINTAINER diaocheweide
- RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
- RUN apk update && apk add --no-cache --virtual .build-deps \
- $PHPIZE_DEPS \
- curl-dev \
- imagemagick-dev \
- libtool \
- libxml2-dev \
- postgresql-dev \
- sqlite-dev \
- libmcrypt-dev \
- freetype-dev \
- libjpeg-turbo-dev \
- libpng-dev \
- && apk add --no-cache \
- curl \
- imagemagick \ mysql-client \
- postgresql-libs \
- && pecl install imagick \
- && pecl install mcrypt-1.0.1 \
- && docker-php-ext-install zip \
- && docker-php-ext-install pdo_mysql \
- && docker-php-ext-install opcache \
- && docker-php-ext-install mysqli \
- && docker-php-ext-enable mcrypt \
- && docker-php-ext-enable imagick \
- && docker-php-ext-install \
- curl \
- mbstring \
- pdo \
- pdo_mysql \
- pdo_pgsql \
- pdo_sqlite \
- pcntl \
- tokenizer \
- xml \
- zip \
- && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" iconv \
- && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
- && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" gd \
- && pecl install -o -f redis \
- && rm -rf /tmp/pear \
- && docker-php-ext-enable redis \
- && rm -r /var/cache/apk/*
- EXPOSE 9000
2.编写yml文件
sudo vi docker-compose.yml
- version: '3.1'
- services:
- nginx:
- image: nginx
- container_name: nginx
- restart: always
- ports:
- - "80:80"
- - "443:443"
- volumes:
- - /www/data/nginx/conf.d:/etc/nginx/conf.d
- - /www/default:/www/default
- networks:
- csl:
- ipv4_address: 172.18.0.2
- php:
- image: php:7.2-fpm-alpine-dcwd
- container_name: php7.2
- restart: always
- volumes:
- - /www/default:/www/default
- networks:
- csl:
- ipv4_address: 172.18.0.3 mysql5:
- image: mysql:5.7
- container_name: mysql5
- restart: always
- ports:
- - "3306:3306"
- environment:
- MYSQL_ROOT_PASSWORD: thisyourpassword
- volumes:
- - /www/data/mysql/mysql5:/var/lib/mysql #- /www/data/mysql/mysql5/conf/my.cnf:/etc/my.cnf
- #- /www/data/mysql/mysql5/init:/docker-entrypoint-initdb.d
- networks:
- csl:
- ipv4_address: 172.18.0.4 mysql8:
- image: mysql:8
- container_name: mysql8
- restart: always
- environment:
- MYSQL_ROOT_PASSWORD: thisyourpassword
- volumes:
- - /www/data/mysql/mysql8:/var/lib/mysql networks:
- csl:
- ipv4_address: 172.18.0.5
- networks:
- csl:
- driver: bridge
- ipam:
- config:
- - subnet: 172.18.0.0/16
3.配置default.conf文件
sudo vi /www/data/nginx/default.conf
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log /var/log/nginx/host.access.log main;
- location / {
- root /www/default;
- index index.php index.html index.htm;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root /usr/share/nginx/html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- location ~ \.php$ {
- root /www/default;
- fastcgi_pass php7.2:9000;#php容器名或者php容器ip
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
4.修改mysql允许远程连接并新建数据库
- docker exec -it mysql5 bash
- mysql -u root -p
- use mysql;
- update user set host='%' where user='root';
- flush privileges;
5.创建并且启动容器
docker-compose up -d
6.新建index.php测试mysql连接
- <?php
- $con = mysqli_connect("172.18.0.4", "root", "thisyourpassword", "shop");
- if ($con) {
- echo '连接 MYSQL 成功';
- } else {
- echo "连接 MySQL 失败: " . mysqli_connect_error();
- } mysqli_close($con);
出处:http://www.phpfensi.com/php/20211208/18989.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式