首页 > 其他分享 >基于docker镜像快速搭建开发环境

基于docker镜像快速搭建开发环境

时间:2023-03-03 11:22:06浏览次数:686  
标签:bbc server ssl 镜像 docker php fastcgi 搭建

准备工作

环境搭建(JAVA项目,mall-product-service为例)

方式一:docker运行

  • 拉取镜像:docker pull registry-t.iauto360.cn/g-mall/mall-product-service@sha256:38e2add4aead9939be5709791a84afc2c5021657b2d75979b812d8ec0d38f118
  • 编写docker-compose.yml

    version: '3'
    services:
      product:
        image: bb10770fa692
        volumes:
          - $HOME/work/mall-product-service/target/mall-product-service-assembly:/app   #替换为挂载本地parent代码maven编译后的目录,具体细节可以inspect一下java镜像。
          - $HOME/docker/shangcheng-uat/volumes/server.properties.java:/opt/settings/server.properties  #实现哪吒的文件挂载,文件内容参考哪吒
        ports:
          - "80:8111"
        environment: #实现哪吒环境变量
          ENV: DEV  #${ENV},也可以用.env环境变量控制
          APOLLO_META: http://10.1.1.200:3180 
        # 容器名称
        networks:
          - elastic
        container_name: product
        #profiles: ["product","all"]  #选择性启动,docker compose   --profile mkt up --remove-orphans
    networks:
      elastic:
        driver: bridge

     

  • 启动项目:docker compose up --remove-orphans 

方式二:idea运行,可以进行实时开发和debug模式

 

 

添加环境变量,并依赖主机的/opt/settings/server.properties 文件。

 

环境搭建(php-fpm项目,bbc为例)

  • 拉取镜像:docker pull registry-t.iauto360.cn/g-mall/bbc@sha256:384b08e12072aa7bbd395cea9708611f037b25fca0e263e6f7b190c6593baeb4
  • 编写docker-compose.yml

    version: '3'
    services:
      bbc:
        image: b08e7e60ed23
        volumes:
          - $HOME/work/b2b2c:/data/www/wwwroot  #挂载本地代码
          - $HOME/docker/shangcheng-uat/volumes/server.properties.php:/opt/settings/server.properties
          - $HOME/docker/shangcheng-uat/volumes/apollo-env.properties:/data/www/app/vendor/yamei/apollo-plugin/configs/apollo-env.properties
          - $HOME/docker/shangcheng-uat/volumes/bbc/default.conf:/etc/nginx/conf.d/default.conf  #这里需要支持https
          - $HOME/docker/shangcheng-uat/volumes/bbc/www.conf:/usr/local/etc/php-fpm.d/www.conf
          - $HOME/docker/shangcheng-uat/volumes/bbc/certi.php:/data/www/wwwroot/config/certi.php
          - $HOME/docker/shangcheng-uat/volumes/bbc/install.lock.php:/data/www/wwwroot/config/install.lock.php
          - $HOME/docker/shangcheng-uat/volumes/bbc/app.php:/data/www/wwwroot/config/production/app.php
          - $HOME/docker/shangcheng-uat/volumes/bbc/redis.php:/data/www/wwwroot/config/production/redis.php
          - $HOME/docker/shangcheng-uat/volumes/bbc/database.php:/data/www/wwwroot/config/production/database.php
          - $HOME/docker/shangcheng-uat/volumes/bbc/docker-php-ext-redis.ini:/usr/local/etc/php/conf.d/docker-php-ext-redis.ini
          - $HOME/docker/shangcheng-uat/volumes/ssl/ssl.crt:/etc/nginx/conf.d/ssl.crt  #挂载证书
          - $HOME/docker/shangcheng-uat/volumes/ssl/ssl.key:/etc/nginx/conf.d/ssl.key
        ports:
          - "80:80"
          - "443:443"
        environment:
          APOLLO_META: http://10.1.1.200:3180
          OCS_URL: http://wry-t-ocs.carrieym.com
          ENABLE_CROND: enable
          APOLLO_ENV_ROOT: /data/www/wwwroot
          APOLLO_ENV_NAME: env
          APOLLO_APPID: yame-bbc
          NGINX_ROOT: /data/www/wwwroot/public
        networks:
          - elastic
        container_name: bbc
    networks:
      elastic:
        driver: bridge

    default.conf

    server {
        listen 80 default_server;##
        listen [::]:80 default_server;
        server_name bbc.dev;
        index index.html index.htm index.php;
        root /data/www/wwwroot/public;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Headers' 'userTicket,ssotoken,token,Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        send_timeout 600;
        client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
        proxy_buffer_size 64k;
        proxy_buffers 8 64k;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 128k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;
        fastcgi_cache_valid 200 302 1h;
        fastcgi_cache_valid 301 1d;
        fastcgi_cache_valid any 1m;
    
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param HTTPS 'on';
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        access_log  /data/www/wwwlogs/access.log;
    
    }
    server {
        listen 443 default_server;##
        server_name bbc.dev;
        ssl on;
        ssl_certificate  /etc/nginx/conf.d/ssl.crt;
        ssl_certificate_key /etc/nginx/conf.d/ssl.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        index index.html index.htm index.php;
        root /data/www/wwwroot/public;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Headers' 'userTicket,ssotoken,token,Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        send_timeout 600;
        client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
        proxy_buffer_size 64k;
        proxy_buffers 8 64k;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 128k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;
        fastcgi_cache_valid 200 302 1h;
        fastcgi_cache_valid 301 1d;
        fastcgi_cache_valid any 1m;
    
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param HTTPS 'on';
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        access_log  /data/www/wwwlogs/access.log;
    
    }

     

  • 启动项目:docker compose up --remove-orphans 
  • 设置本地hosts :127.0.0.1  bbc.dev
  • 浏览器访问bbc.dev,输入thisisunsafe,绕开https证书安全问题

基于docker支持xdebug的配置:

镜像加装xdebug扩展:

apk add make gcc libc-dev libcurl curl-dev libpng libpng-dev openldap-dev postgresql-dev autoconf

pecl install xdebug-2.2.5

php.ini配置:

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so

xdebug.remote_enable=on

xdebug.remote_handler=dbgp

xdebug.remote_host=docker.for.mac.localhost  ;如果是远程调试需要开启,docker配置如此

xdebug.remote_port=9000

xdebug.idekey=phpStorm

xdebug.remote_autostart=1

注:此处可以提交镜像,compose用新提交的镜像,防止debug修改被清理。

docker  commit  bbc  bbc:xdebug

 

chrome浏览器安装xdebug扩展程序

idea配置如下:

 

注:

浏览器安装xdebug插件,作用是请求发送xdebug参数,告诉php服务端需要xdebug执行模式

Idea开启xdebug监听,并开启9000端口,监听php服务执行代码xdebug发送到此端口的信息。

远程xdebug执行的原理,其实就是将远程的php代码映射到本地,以配合idea(如果本地的不一致,以远程的为准),将代码挂载出来后,此问题解决。

环境搭建(swoole项目,mkt为例)

  • 拉取镜像:docker pull registry-t.iauto360.cn/g-mall/mkt@sha256:9d20e5e72beacb844fbf6bcaf6e8fbac41ce14add68d05ed465056d71e796c2d
  • 编写docker-compose.yml

    version: '3'
    services:
      mkt:
        image: 082f425b82b2
        volumes:
          - $HOME/work/mkt:/data/www/app #挂载本地代码
          - $HOME/docker/shangcheng-uat/volumes/server.properties.php:/opt/settings/server.properties
          - $HOME/docker/shangcheng-uat/volumes/apollo-env.properties:/data/www/app/vendor/yamei/apollo-plugin/configs/apollo-env.properties
        ports:
          - "80:19501"
        environment:
          ENABLE_CROND: enable
        networks:
          - elastic
        container_name: mkt
    networks:
      elastic:
        driver: bridge
    
    

     

  • 启动项目:docker compose up --remove-orphans 

基于docker支持debug模式:

由于swoole和xdebug拓展冲突,所以需要选用 yasd,安装参考https://github.com/swoole/yasd

需要注意的是swoole模式的debug,并不需要浏览器的debug插件。它的原理是idea的9000端口直接和php服务端通讯。但是启动easyswoole的时候,需要加上-e参数,并且需要指定php服务端的PHP_IDE_CONFIG环境变量,用于idea选择映射的server。所以修改compose如下:

  trade:
    image: 9e0582fca306
    volumes:
      - $HOME/work/mall-trade-service:/data/www/app
      - $HOME/docker/shangcheng-uat/volumes/server.properties.php:/opt/settings/server.properties
      - $HOME/docker/shangcheng-uat/volumes/apollo-env.properties:/data/www/app/vendor/yamei/apollo-plugin/configs/apollo-env.properties
    ports:
      - "80:9505"
    command: 
      - "/bin/sh"
      - "-c"
      - "php vendor/bin/ym-annotation compile && php -e easyswoole start" #重写启动命令,加上-e参数

    environment:
      ENV: ${ENV}
      APOLLO_META: ${APOLLO_META}
      PHP_IDE_CONFIG: "serverName=demo"# 此处需要设定idea-》preferences->language& frameworks->php->servers的Name
    # 容器名称
    networks:
      - elastic
    container_name: trade
    profiles: ["trade","all"]

 

开启idea的监听debug按钮,启动容器,debug就完成链接。

 

nginx统一管理各个服务的办法

  nginx:
    image: nginx:latest
    volumes:
      - $HOME/docker/shangcheng-uat/volumes/nginx/conf.d:/etc/nginx/conf.d
    ports:
      - "80:80"
      - "443:443"
    networks:
      - elastic
    container_name: nginx
    # depends_on:
    #   - bbc
    #   - mkt
    profiles: ["nginx","all"]

nginx配置

server {
    listen 443 ssl;
    server_name bbc.dev; #server并不能限制非次servername的域名指向到此处443,如果没有找到,则默认执行第一个server。
    ssl_certificate  /etc/nginx/conf.d/ssl.crt;
    ssl_certificate_key /etc/nginx/conf.d/ssl.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;


    location / {
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         resolver 127.0.0.11;  #此处解决host not found in upstream,其中docker-compose 启动的nginx 内部默认的dns解析服务是127.0.0.11
         set $ups  bbc:80;
         proxy_pass http://$ups;
    }
}

 

修改本地/etc/hosts文件

例如:bbc.dev 127.0.0.1

 

 

标签:bbc,server,ssl,镜像,docker,php,fastcgi,搭建
From: https://www.cnblogs.com/zenghansen/p/17174910.html

相关文章

  • 轻松搭建属于自己的远程控制服务-私有云-rustdesk
    安装NodeJs请点击https://nodejs.org/dist/v16.14.2/node-v16.14.2-x86.msi下载安装,可能会有点慢,如果卡顿太久,尝试关掉重新安装。NodeJs是pm2的运行时环境,所以要先安装No......
  • RL 基础:如何搭建自定义 gym 环境
    需实现的方法:__init__(self):需定义action_space和observation_space,使用space.Box之类来表示(fromgymimportspaces)。step(self,action):返回observation......
  • docker记录
    安装(Ubuntu)1.更新aptapt-getupdate2.安装相关依赖apt-getinstallapt-transport-httpsca-certificatescurlgnupg-agen3.添加docker的官方密钥curl-fsSLhttp......
  • 从0搭建Vue3组件库(三): 组件库的环境配置
    本篇文章将在项目中引入typescript,以及手动搭建一个用于测试组件库组件Vue3项目因为我们是使用Vite+Ts开发的是Vue3组件库,所以我们需要安装typescript、vue3,同......
  • vue3 门户网站搭建8-字体
    浏览器默认的可选字体比较少,如果没有合适的则需要额外下载并引入。一般使用开源字体即可,商用需要花钱~ 将下载好的ttf格式字体放入项目下文件夹: 样式文件中增加......
  • 轻量级CI/CD发布部署环境搭建及使用_05_安装宿主机环境(jdk、nodejs、maven、python2)
    轻量级CI/CD发布部署环境搭建及使用_05_安装宿主机环境(jdk、nodejs、maven、python2)尽自己的绵薄之力,为开源技术分享添砖加瓦本篇幅较多,请按步骤实验安装,截图中涉及的......
  • Docker环境下安装Redis 集群
    Redis三种解决方案哈希取余分区   优点:简单粗暴,直接有效缺点:不好扩缩容,某个节点宕机,会导致hash取余全部数据重新洗牌一致性哈希环优点:加入或......
  • kkfileview搭建实战
     配合FTP服务器搭建:(不支持中文目录或文件名)dockerrun-d-v/home/vsftpd:/home/vsftpd\-p20:20-p21:21-p21100-21110:21100-21110\-eFTP_USER=admin-eFT......
  • 彩虹个人发卡网源码+免签易支付源码接口+搭建教程
    在全球大流行的时代,一个平稳运行的支付网关发卡系统是经营具有大量在线业务的企业的关键。如果您已经在使用像BigCommerce这样的电子商务软件,那么您几乎不需要任何编码技能......
  • vue3 门户网站搭建7-eslint
    为了方便阅读和维护,代码规范还是有必要的 1、安装:npmieslint--save-dev 2、配置 .eslintrc.cjs文件,增加rules:rules:{'semi':['warn','always'],......