Docker 快速搭建php运行环境,Dockerfile详解,有附件

附件下载

创建PHP 镜像

编写 PHP Dockerfile

FROM php:7.4.11-fpm-alpine3.12
#RUN echo "memory_limit = 256M;" >> /usr/local/etc/php/php.ini
ENV LANG=C.UTF-8
#更新软件和必要库
#php扩展编译需要
#安装扩展
#Install PHP extensions phpredis
ENV REDIS_VERSION=5.3.2
#Install PHP extensions phalcon
ENV PHALCON_VERSION=4.1.0
ENV PSR_VERSION=1.0.0
#安装debug扩展
ENV XDEBUG_VERSION=2.9.8
#composer版本
ENV COMPOSER_VERSION=2.0.3
RUN  apk add --no-cache  vim jq gmp-dev freetype-dev  libjpeg-turbo-dev libzip-dev  openssl-dev openssh-client gzip \
      linux-headers bash $PHPIZE_DEPS  zip  \
  && docker-php-ext-configure gmp \
  && docker-php-ext-configure gd --with-freetype --with-jpeg \
  && docker-php-ext-install bcmath gmp sockets gd  pdo_mysql pcntl opcache \
  && pecl install psr-${PSR_VERSION} && docker-php-ext-enable psr \
  && echo "install psr ok !" \
  && curl -s -L https://codeload.github.com/phalcon/cphalcon/tar.gz/v${PHALCON_VERSION} | tar -xvzf - -C /tmp \
  && cd /tmp/cphalcon-${PHALCON_VERSION}/build && ./install && cd ../ && rm -rf /tmp/cphalcon-${PHALCON_VERSION} \
  && echo -e "extension = phalcon.so;\nphalcon.orm.exception_on_failed_save = On;\nphalcon.orm.column_renaming = Off;\nphalcon.orm.not_null_validations = Off;" > /usr/local/etc/php/conf.d/phalcon.ini  \
  && echo "install cphalcon ok !" \
  && pecl install redis-${REDIS_VERSION} && docker-php-ext-enable redis \
  && echo "install redis ok !" \
  && pecl install zip && docker-php-ext-enable zip \
  && pecl install xdebug-${XDEBUG_VERSION}  \
  && echo "install xdebug ok !" \
  && curl -L https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar  -o /usr/local/bin/composer \
  && chmod +x /usr/local/bin/composer \
  && echo -e "disable_functions = phpinfo,eval,passthru,exec,system,chroot,chgrp,chown,shell_exec,ini_alter,ini_alter,ini_restore,dl,pfsockopen,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockopen;\nexpose_php = Off;" > /usr/local/etc/php/conf.d/safety.ini  \
  && pecl clear-cache && apk del $PHPIZE_DEPS  \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/cache/apk/* \
           /usr/share/doc/* /name/usr/share/man/* /usr/share/info/* \
  && sed -i 's#root:x:0:0:root:/root:/bin/ash#root:x:0:0:root:/root:/bin/bash#g' /etc/passwd
WORKDIR /data/www

创建PHP镜像

进入目录

luwb@MacBook-Pro docker % ls
Dockerfile		docker-compose.yml	mysql			nginx			php.ini
dev.ini			log			mysql8			nginx.zip		redis

创建镜像

docker build -t <luwenbin8023/php:7.4.11-fpm-alpine3.12> .

查看创建是否成功

luwb@MacBook-Pro docker % docker image ls
REPOSITORY           TAG                     IMAGE ID            CREATED             SIZE
luwenbin8023/php     7.4.12-fpm-alpine3.12   8b770a1ad3fd        18 hours ago        157MB

创建nginx镜像

编写nginx Dockerfile

FROM nginx:1.19.0-alpine
#设置时区
RUN apk update && apk add ca-certificates && \
    apk add tzdata && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone
# config
RUN ln -sf /opt/config/nginx.conf       /etc/nginx/nginx.conf
RUN ln -sf /opt/config/fastcgi_params   /etc/nginx/fastcgi_params
RUN ln -sf /opt/config/sites-enabled    /etc/nginx/sites-enabled
RUN ln -sf /opt/config/certs            /etc/nginx/certs
EXPOSE 80

创建nginx镜像

进入目录

luwb@MacBook-Pro docker % cd nginx
luwb@MacBook-Pro nginx % ls
Dockerfile	certs		fastcgi_params	logcron.sh	logrotate.d	nginx.conf	sites-enabled	stream

创建镜像

docker build -t <luwenbin8023/nginx:1.19.0-alpine> .

查看创建是否成功

luwb@MacBook-Pro nginx % docker image ls
REPOSITORY           TAG                     IMAGE ID            CREATED             SIZE
luwenbin8023/nginx   1.19.0-alpine           c71d0828bec1        18 hours ago        22.9MB
luwenbin8023/php     7.4.12-fpm-alpine3.12   8b770a1ad3fd        18 hours ago        157MB

创建mysql镜像

直接拉去docker hub 上的镜像 docker hub mysql

docker pull mysql:5.7.32
luwb@MacBook-Pro nginx % docker image ls
REPOSITORY           TAG                     IMAGE ID            CREATED             SIZE
luwenbin8023/nginx   1.19.0-alpine           c71d0828bec1        18 hours ago        22.9MB
luwenbin8023/php     7.4.12-fpm-alpine3.12   8b770a1ad3fd        18 hours ago        157MB
mysql                5.7.32                  ae0658fdbad5        6 days ago          449MB

创建redis 镜像

直接拉去docker hub 上的镜像 docker hub redis

docker pull redis:6.0.9-alpine3.12
luwb@MacBook-Pro nginx % docker image ls
REPOSITORY           TAG                     IMAGE ID            CREATED             SIZE
luwenbin8023/nginx   1.19.0-alpine           c71d0828bec1        18 hours ago        22.9MB
luwenbin8023/php     7.4.12-fpm-alpine3.12   8b770a1ad3fd        18 hours ago        157MB
mysql                5.7.32                  ae0658fdbad5        6 days ago          449MB
redis                6.0.9-alpine3.12        c1949ec48c51        4 weeks ago         31.2MB

编写 docker-compose.yml

version: '2'
services:
  nginx:
     pid: "host"
     restart: always #设置自启动
     #build: ./nginx #构建docker的Dockerfile目录
     image: luwenbin8023/nginx:1.19.0-alpine #本地镜像
     expose:
       - "80"
     ports:
       - "80:80" #宿主端口(映射到)-》docker镜像端口
       - "8020:8020" #宿主端口(映射到)-》docker镜像端口
       - "8030:8030" #宿主端口(映射到)-》docker镜像端口
       - "443:443" #宿主端口(映射到)-》docker镜像端口
       - "85:85" #宿主端口(映射到)-》docker镜像端口
     volumes:
       - ./nginx/nginx.conf:/etc/nginx/nginx.conf #nginx配置
       - ./nginx/fastcgi_params:/etc/nginx/fastcgi_params #fastcgi配置
       - ./nginx/sites-enabled:/etc/nginx/sites-enabled #站点配置
       - ./nginx/certs:/etc/nginx/certs #ssl证书
       - ./nginx/logrotate.d:/etc/logrotate.d/ # 日志分割配置
       - ../www:/data/www #站点根目录
       - ./log/nginx:/data/log #nginx log目录
     links:
       - "php"  #连接其他docker镜像
       - "mysql"
       - "redis"
     external_links:
       - "graylog"
     container_name: nginx #docker镜像名称
     privileged: true #设定读写权限
  php:
      restart: always #设置自启动
      image: luwenbin8023/php:7.4.12-fpm-alpine3.12 #本地镜像
      #image: 64899484/php
      volumes:
        - ../www:/data/www #站点根目录
        ##- ./php:/opt/config #配置文件
        - ./log/php:/opt/log/php #php log目录
        ##- ./dev.ini:/usr/local/etc/php/conf.d/safety.ini
      ports:
        - "9501:9501" #宿主端口(映射到)-》docker镜像端口
        - "9502:9502" #宿主端口(映射到)-》docker镜像端口
      container_name: php
      privileged: true #设定读写权限
      environment:
        - SW_WORKER_NUM=10
        - SW_MAX_CONN=100
        - SW_TASK_NUM=5
        - GELF_TOKEN=ort75mte218kafjqeoqg3iqqb2r4uu8dq4bcgjm52r891f8m9tl
        - SITE_DEBUG=1
        - SERVER_ID=1
        - DAEMON=0
      #extra_hosts:
        #- "my.syq.com:172.16.107.51"
      ##dns:
        ##- 223.5.5.5
  mysql:
      restart: always #设置自启动
      #build: ./mysql
      image: mysql:5.7.32 #本地镜像
      ports:
        - "3308:3306" #“宿主机端口:容器端口” 容器的3306端口映射到宿主机的3308端口
      volumes:
        - ./mysql:/var/lib/mysql #数据目录
        #- ./mysql8/my.cnf:/etc/mysql/my.cnf  #配置文件
      environment:
        MYSQL_ROOT_PASSWORD: 123456
      container_name: mysql
      privileged: true #设定读写权限
  redis:
      restart: always #设置自启动
      #build: ./redis
      image: redis:6.0.9-alpine3.12 #本地镜像
      volumes:
        - ./redis:/data #redis持久存储目录
        - ./redis/redis.conf:/usr/local/etc/redis/redis.conf #配置文件
      ports:
        - "6379:6379"
      command: redis-server /usr/local/etc/redis/redis.conf #redis持久存储
      container_name: redis
      privileged: true #设定读写权限
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页