docker-compose搭建php環境

rayken發表於2022-03-24

安裝docker-compose Install Docker Compose | Docker Documentation

首先建立環境目錄

  • data 目錄用來存放redis mysql資料
  • mysql nginx php redis 目錄用來構建映象,存放Dokerfile等
  • www目錄為專案根目錄,index.php輸出phpinfo()
  • docker-compose.yml 為啟動配置檔案
  • .env 儲存全域性變數

配置如下
nginx Dockerfile

FROM nginx
COPY default.conf  /etc/nginx/conf.d/

nginx default.conf

server {
    listen      80;
    listen  [::]:80;
    server_name  localhost;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root  /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    #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          html;
        fastcgi_pass  php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$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;
    #}
}

php Dockerfile

FROM php:7.4-fpm
RUN 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

redis Dockerfile

FROM redis

mysql Dockerfile

FROM mysql

.env

# 設定時區
TZ=Asia/Shanghai
# 設定網路模式
NETWORKS_DRIVER=bridge
# PATHS ##########################################
# 宿主機上程式碼存放的目錄路徑
WWW_PATH_HOST=./www
# 宿主機上Mysql Reids資料存放的目錄路徑
DATA_PATH_HOST=./data
# Mysql 服務對映宿主機埠號,可在宿主機127.0.0.1:3306訪問
MYSQL_PORT=3306
MYSQL_USERNAME=admin
MYSQL_PASSWORD=123456
MYSQL_ROOT_PASSWORD=123456
# Redis 服務對映宿主機埠號,可在宿主機127.0.0.1:6379訪問
REDIS_PORT=6379
#Php-fpm 埠號
PHP_PORT=9000

docker-compose.yml

version: '3.0'
# 網路配置
networks:
  backend:
    driver: ${NETWORKS_DRIVER}
# 服務容器配置
services:
  nginx:                                # 自定義容器名稱
    build:
      context: ./nginx                  # 指定構建使用的 Dockerfile 檔案
    environment:                        # 設定環境變數
      - TZ=${TZ}
    volumes:                            # 設定掛載目錄
      - ${WWW_PATH_HOST}:/usr/share/nginx/html  # 引用 .env 配置中 WWW_PATH_HOST 變數,將宿主機上程式碼存放的目錄掛載到容器中 /usr/share/nginx/html 目錄
    ports:                              # 設定埠對映
      - "80:80"
      - "8000:8000"
      - "8001:8001"
      - "8002:8002"
      - "8003:8003"
    networks:
      - backend
    restart: always                      # 指定容器退出後的重啟策略為始終重啟
  php:
    build:
      context: ./php
    environment:
      - TZ=${TZ}
    volumes:
      - ${WWW_PATH_HOST}:/usr/share/nginx/html      # 引用 .env 配置中 WWW_PATH_HOST 變數,將宿主機上程式碼存放的目錄掛載到容器中 /usr/share/nginx/html 目錄
    ports:
      - "${PHP_PORT}:9000"                          # 設定容器9000埠對映指定宿主機埠
    networks:
      - backend
    restart: always
  mysql:
    build:
      context: ./mysql
    environment:
      - TZ=${TZ}
      - MYSQL_USER=${MYSQL_USERNAME}                  # 設定 Mysql 使用者名稱稱
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}              # 設定 Mysql 使用者密碼
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}    # 設定 Mysql root 使用者密碼
    volumes:
      - ${DATA_PATH_HOST}/mysql:/var/lib/mysql        # 引用 .env 配置中 DATA_PATH_HOST 變數,將宿主機上存放 Mysql 資料的目錄掛載到容器中 /var/lib/mysql 目錄
    ports:
      - "${MYSQL_PORT}:3306"                          # 設定容器3306埠對映指定宿主機埠
    networks:
      - backend
    restart: always
  redis:
    build:
      context: ./redis
    environment:
      - TZ=${TZ}
    volumes:
      - ${DATA_PATH_HOST}/redis:/data                # 引用 .env 配置中 DATA_PATH_HOST 變數,將宿主機上存放 Redis 資料的目錄掛載到容器中 /data 目錄
    ports:
      - "${REDIS_PORT}:6379"                          # 設定容器6379埠對映指定宿主機埠
    networks:
      - backend
    restart: always

後臺構建並啟動

docker-compose up -d

出現done代表安裝成功

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章