docker-compose部署php项目实例详解
1.制定特定扩展的PHP镜像
sudomkdir-p/www/docker
sudocd/www/docker
sudoviDockerfile
FROMphp:7.2-fpm-alpine MAINTAINERdiaocheweide RUNsed-i's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'/etc/apk/repositories RUNapkupdate&&apkadd--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\ &&apkadd--no-cache\ curl\ imagemagick\mysql-client\ postgresql-libs\ &&peclinstallimagick\ &&peclinstallmcrypt-1.0.1\ &&docker-php-ext-installzip\ &&docker-php-ext-installpdo_mysql\ &&docker-php-ext-installopcache\ &&docker-php-ext-installmysqli\ &&docker-php-ext-enablemcrypt\ &&docker-php-ext-enableimagick\ &&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-configuregd--with-freetype-dir=/usr/include/--with-jpeg-dir=/usr/include/\ &&docker-php-ext-install-j"$(getconf_NPROCESSORS_ONLN)"gd\ &&peclinstall-o-fredis\ &&rm-rf/tmp/pear\ &&docker-php-ext-enableredis\ &&rm-r/var/cache/apk/* EXPOSE9000
2.编写yml文件
sudovidocker-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.3mysql5: 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.4mysql8: image:mysql:8 container_name:mysql8 restart:always environment: MYSQL_ROOT_PASSWORD:thisyourpassword volumes: -/www/data/mysql/mysql8:/var/lib/mysqlnetworks: csl: ipv4_address:172.18.0.5 networks: csl: driver:bridge ipam: config: -subnet:172.18.0.0/16
3.配置default.conf文件
sudovi/www/data/nginx/default.conf
server{ listen80; server_namelocalhost; #charsetkoi8-r; #access_log/var/log/nginx/host.access.logmain; location/{ root/www/default; indexindex.phpindex.htmlindex.htm; } #error_page404/404.html; #redirectservererrorpagestothestaticpage/50x.html # error_page500502503504/50x.html; location=/50x.html{ root/usr/share/nginx/html; } #proxythePHPscriptstoApachelisteningon127.0.0.1:80 # #location~\.php${ #proxy_passhttp://127.0.0.1; #} #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000 # location~\.php${ root/www/default; fastcgi_passphp7.2:9000;#php容器名或者php容器ip fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; } #denyaccessto.htaccessfiles,ifApache'sdocumentroot #concurswithnginx'sone # #location~/\.ht{ #denyall; #} }
4.修改mysql允许远程连接并新建数据库
dockerexec-itmysql5bash mysql-uroot-p usemysql; updateusersethost='%'whereuser='root'; flushprivileges;
5.创建并且启动容器
docker-composeup-d
6.新建index.php测试mysql连接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。