docker本地環境搭建

vance發表於2020-11-24

執行環境:nginx,php,mysql,redis
檔案目錄

lnmp
 - mysql
 - - my.cny
 - php
 - - php74
 - - - etc
 - - - - php
 - - - - - conf.d    # 擴充套件配置檔案
 - - - - - php.ini
 - - - - - php.ini-development
 - - - - - php.ini-production
 - - - - php-fpm
 - - - - - docker.conf
 - - - - - www.conf
 - - - - - www.conf.default
 - - - - - zz-docker.conf
 - - - - pear.conf
 - - - - php-fpm.conf
 - - - - php-fpm.conf.default
 - - - - Dockerfile
 - nginx
 - - conf
 - - - nginx.conf
 - - conf.d
 - - - default.conf
 - redis
 - - conf
 - - - redis.conf
 - docker-compose.yml

docker-compose.yml

直接貼程式碼

version: '3.8'
services:
  nginx:
    # 容器名稱
    container_name: compose-nginx
    image: nginx:latest
    restart: always    # 重啟策略
    # 埠對映
    ports:
      - "80:80"
      - "443:443"
    environment:
      TZ: Asia/Shanghai
    # 依賴關係,需要先執行php
    depends_on:
      - php
    volumes:
      - "${PWD}/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
      - "${PWD}/nginx/conf.d:/etc/nginx/conf.d"    #將宿主機上nginx配置檔案對映到容器中
      #- "${PWD}/nginx/cert:/etc/nginx/cert"
      - "${PWD}/../../code:/var/www/html:cached"
      - "${PWD}/nginx/log:/var/log/nginx"          #預設日誌地址
    networks:
      app_net:
        ipv4_address: 172.18.0.5
  php:
    #指定build Dockerfile生成映象
    build: "${PWD}/php/php74"
    image: php:7.4
    container_name: compose-php
    restart: always
    ports:
      - "9000:9000"
      - "8282:8282"
    environment:
      TZ: Asia/Shanghai
    volumes:
      - "${PWD}/../../code:/var/www/html:cached"
      - "${PWD}/php/php74/etc:/usr/local/etc"
      - "${PWD}/php/php74/log:/var/log/php"
    stdin_open: true
    tty: true   #這兩條是防止啟動php失敗
    links:
      - "mysql"
    networks:
      app_net:
        ipv4_address: 172.18.0.4
  mysql:
    image: mysql:5.7
    container_name: compose-mysql
    restart: always
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "${PWD}/mysql/my.cnf:/etc/mysql/my.cnf"
      - "${PWD}/mysql/data:/var/lib/mysql"
      - "${PWD}/mysql/logs:/data/mysql/logs"
    ports:
      - "3306:3306"
    networks:
      app_net:
        ipv4_address: 172.18.0.3
  redis:
    image: redis:5.0
    container_name: compose-redis
    restart: always
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
    volumes:
      - "${PWD}/redis/conf/redis.conf:/etc/redis/redis.conf"
      - "${PWD}/redis/data/:/data"
    ports:
      - 6379:6379
    command:
      # 執行的命令
      redis-server /etc/redis/redis.conf --requirepass 123456 --appendonly yes
    networks:
      app_net:
        ipv4_address: 172.18.0.2
networks:             # 配置docker 網路
  app_net:
    name: app_net     # 設定網路自定義名稱
    driver: bridge    #
    ipam:
      config:
        - subnet: 172.18.0.0/16

code:是本人程式碼檔案目錄
其中mysql和redis日誌目錄沒用,沒去仔細研究
每次新增本地虛擬站點,在nginx/conf.d/裡面新建檔案就行

nginx/conf.d/default

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #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           /var/www/html;
        fastcgi_pass   php:9000;
        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;
    #}
}

PHP Dockerfile構建檔案

FROM php:7.4-fpm
RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib \n \
deb http://mirrors.aliyun.com/debian-security buster/updates main \n \
deb-src http://mirrors.aliyun.com/debian-security buster/updates main \n \
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \n \
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" > /etc/apt/sources.list \
    # 更新及安裝庫
    && apt-get update \
    && apt-get install -y \
    vim net-tools \
    build-essential \
    libmagickcore-dev \
    libmagickwand-dev \
    imagemagick \
    libmcrypt-dev \
    libzip-dev \
    libmemcached-dev \
    # GD庫擴充套件
    && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    # PHP擴充套件安裝
    && docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv \
    && pecl install memcached-3.1.4 \
    && pecl install redis-5.2.2 \
    && pecl install imagick mcrypt zip swoole \
    && docker-php-ext-enable memcached redis imagick mcrypt zip swoole

# 映象資訊
LABEL Author="vance"
LABEL Version="2020.8"
LABEL Description="PHP 7.4 開發環境映象."

這樣,根據docker-compose.yml裡面的ip以及埠,本地就可以使用了

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

相關文章