docker compose Deployment php Project Example Detailed Explanation

  • 2021-12-13 16:24:30
  • OfStack

1. Develop an PHP image for a specific extension


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. Write an yml file


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. Configure the default. conf file


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 Container name or php Container 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. Modify mysql to allow remote connections and create new databases


docker exec -it mysql5 bash

mysql -u root -p

use mysql;

update user set host='%' where user='root';

flush privileges;

5. Create and start the container


docker-compose up -d

6. New index. php Test mysql Connection


sudo cd /www/docker
0


Related articles: