docker-compose部署php项目实例详解

在本篇文章里小编给大家整理了关于docker-compose部署php项目的相关实例以及代码内容,有需要的朋友们可以学习参考下。

1.制定特定扩展的PHP镜像

sudo mkdir -p /www/docker

sudo cd /www/docker

sudo vi Dockerfile

  1. FROM php:7.2-fpm-alpine
  2. MAINTAINER diaocheweide
  3. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  4. RUN apk update && apk add --no-cache --virtual .build-deps \
  5. $PHPIZE_DEPS \
  6. curl-dev \
  7. imagemagick-dev \
  8. libtool \
  9. libxml2-dev \
  10. postgresql-dev \
  11. sqlite-dev \
  12. libmcrypt-dev \
  13. freetype-dev \
  14. libjpeg-turbo-dev \
  15. libpng-dev \
  16. && apk add --no-cache \
  17. curl \
  18. imagemagick \ mysql-client \
  19. postgresql-libs \
  20. && pecl install imagick \
  21. && pecl install mcrypt-1.0.1 \
  22. && docker-php-ext-install zip \
  23. && docker-php-ext-install pdo_mysql \
  24. && docker-php-ext-install opcache \
  25. && docker-php-ext-install mysqli \
  26. && docker-php-ext-enable mcrypt \
  27. && docker-php-ext-enable imagick \
  28. && docker-php-ext-install \
  29. curl \
  30. mbstring \
  31. pdo \
  32. pdo_mysql \
  33. pdo_pgsql \
  34. pdo_sqlite \
  35. pcntl \
  36. tokenizer \
  37. xml \
  38. zip \
  39. && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" iconv \
  40. && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
  41. && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" gd \
  42. && pecl install -o -f redis \
  43. && rm -rf /tmp/pear \
  44. && docker-php-ext-enable redis \
  45. && rm -r /var/cache/apk/*
  46. EXPOSE 9000

2.编写yml文件

sudo vi docker-compose.yml

  1. version: '3.1'
  2. services:
  3. nginx:
  4. image: nginx
  5. container_name: nginx
  6. restart: always
  7. ports:
  8. - "80:80"
  9. - "443:443"
  10. volumes:
  11. - /www/data/nginx/conf.d:/etc/nginx/conf.d
  12. - /www/default:/www/default
  13. networks:
  14. csl:
  15. ipv4_address: 172.18.0.2
  16. php:
  17. image: php:7.2-fpm-alpine-dcwd
  18. container_name: php7.2
  19. restart: always
  20. volumes:
  21. - /www/default:/www/default
  22. networks:
  23. csl:
  24. ipv4_address: 172.18.0.3 mysql5:
  25. image: mysql:5.7
  26. container_name: mysql5
  27. restart: always
  28. ports:
  29. - "3306:3306"
  30. environment:
  31. MYSQL_ROOT_PASSWORD: thisyourpassword
  32. volumes:
  33. - /www/data/mysql/mysql5:/var/lib/mysql #- /www/data/mysql/mysql5/conf/my.cnf:/etc/my.cnf
  34. #- /www/data/mysql/mysql5/init:/docker-entrypoint-initdb.d
  35. networks:
  36. csl:
  37. ipv4_address: 172.18.0.4 mysql8:
  38. image: mysql:8
  39. container_name: mysql8
  40. restart: always
  41. environment:
  42. MYSQL_ROOT_PASSWORD: thisyourpassword
  43. volumes:
  44. - /www/data/mysql/mysql8:/var/lib/mysql networks:
  45. csl:
  46. ipv4_address: 172.18.0.5
  47. networks:
  48. csl:
  49. driver: bridge
  50. ipam:
  51. config:
  52. - subnet: 172.18.0.0/16

3.配置default.conf文件

sudo vi /www/data/nginx/default.conf

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/host.access.log main;
  6. location / {
  7. root /www/default;
  8. index index.php index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. location ~ \.php$ {
  25. root /www/default;
  26. fastcgi_pass php7.2:9000;#php容器名或者php容器ip
  27. fastcgi_index index.php;
  28. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  29. include fastcgi_params;
  30. }
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }

4.修改mysql允许远程连接并新建数据库

  1. docker exec -it mysql5 bash
  2. mysql -u root -p
  3. use mysql;
  4. update user set host='%' where user='root';
  5. flush privileges;

5.创建并且启动容器

docker-compose up -d

6.新建index.php测试mysql连接

  1. <?php
  2. $con = mysqli_connect("172.18.0.4", "root", "thisyourpassword", "shop");
  3. if ($con) {
  4. echo '连接 MYSQL 成功';
  5. } else {
  6. echo "连接 MySQL 失败: " . mysqli_connect_error();
  7. } mysqli_close($con);