1. 程式人生 > 實用技巧 >docker-compose 構建本地開發環境

docker-compose 構建本地開發環境

docker-compose 構建本地開發環境

​ 最近在折騰開發環境,在之前的開發中都是通過Dockerfile去構建單個專案,資料庫直接連公司開發環境的,終究是有點麻煩,弄錯資料,測試兄弟又要咋咋呼呼。

​ 這裡說明我的本地開發環境包括ServiceA、ServiceB均是Nginx+php-fpm+redis+MySQL

docker目錄

deploy_dev.sh 構建指令碼

docker

dev

config 配置檔案

fpm

mysql

nginx

php

redis

supervisor

files

html

docker-compose.yml

Dockerfile

Service_a.Dockerfile

Service_b.Dockerfile

startup.sh

docker-compose.yml

#
# 如果不需要mysql、redis 就註釋掉相關的配置
# 保證本機的  11008、11009、13306、16379 埠未被佔用或者自行修改配置檔案
#

version: '3.1'

services:
  # 服務a
  service_a:
    container_name: ServiceA
    labels:
      service.name: ServiceA
      service.description: For user
    # dockerfile 目錄  
    build: 
      context: ./
      dockerfile: Service_a.Dockerfile
    # 埠對映  
    ports:
      - "11008:80"
    # 資料掛載  
    volumes:
      - ${CODE_DIR}:/var/www/servicea
    networks:
      - outside
      - wby_dev
    # 環境變數
    environment:
      HOST_IP: ${host_ip}
  # 服務b
  service_b:
    container_name: ServiceB
    labels:
      service.name: ServiceB
      service.description: For user
    build:
      context: ./
      dockerfile: Service_b.Dockerfile
    ports:
      - "11009:80"
    volumes:
      - ${CODE_DIR}:/var/www/serviceb
    networks:
      - outside
      - wby_dev
    environment:
      HOST_IP: ${host_ip}
  # 資料庫
  db:
    container_name: wby_dev_mysql
    # 使用8.0的映象
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8
      --collation-server=utf8_general_ci
      --bind-address=0.0.0.0
      --port=3306
    environment:
      MYSQL_ROOT_PASSWORD: root
    # 初始化sql, 會自動執行  
    volumes:
    - ./config/mysql/:/docker-entrypoint-initdb.d/
    ports:
    - '13306:3306'
    networks:
      - outside
      - wby_dev
  redis:
    container_name: wby_dev_redis
    image: redis:6.0
    ports:
    - '16379:6379'
    command: redis-server /etc/conf/redis.conf
    volumes:
    - ./config/redis/redis.conf:/etc/conf/redis.conf
    environment:
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    networks:
      - outside
      - wby_dev
networks:
  outside:
      external: true
  wby_dev:

Dockerfile

FROM php:7.4.13-fpm-alpine3.11

# 修改系統源為阿里雲
RUN echo 'http://mirrors.aliyun.com/alpine/v3.11/main' > /etc/apk/repositories \
    && echo 'http://mirrors.aliyun.com/alpine/v3.11/community' >> /etc/apk/repositories \
    && echo 'http://mirrors.aliyun.com/alpine/edge/testing' >> /etc/apk/repositories \
    && apk update

# 修改系統時區為CST
RUN ln -snf /usr/share/zoneinfo/"Asia/Shanghai" /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone

# 安裝系統依賴包
RUN apk add --no-cache \
        ca-certificates \
        tzdata \
        libpng-dev \
        freetype-dev \
        jpeg-dev \
        zlib-dev \
        nginx \
        supervisor \
        grpc-dev \
        protobuf \
        curl-dev

# 安裝編譯安裝的依賴包(virtual 臨時)
RUN set -eux; \
	apk add --no-cache --virtual .build-deps \
        autoconf \
        dpkg-dev dpkg \
        file \
        g++ \
        gcc \
        libc-dev \
        make \
        pkgconf \
        re2c

# 安裝擴充套件(pecl擴充套件)
#RUN set -eux; \
#    pecl update-channels; \
#    pecl install redis; \
#    pecl install mongodb;

# 安裝擴充套件(系統擴充套件)
RUN set -eux; \
    docker-php-ext-configure gd --with-freetype --with-jpeg; \
    docker-php-ext-install gd bcmath pdo_mysql; \
    echo no | docker-php-ext-install opcache;

# 安裝擴充套件(三方擴充套件)
COPY files /root/files

# skywalking
RUN set -eux; \
    cd /root/files; \
    tar xf v4.1.1.tar.gz; \
    cd SkyAPM-php-sdk-4.1.1; \
    phpize && ./configure && make && make install;

# redis
RUN set -eux; \
    cd /root/files; \
    tar xf redis-5.3.2.tgz; \
    cd redis-5.3.2; \
    phpize && ./configure && make && make install;

# mongo
RUN set -eux; \
    cd /root/files; \
    tar xf mongodb-1.9.0.tgz; \
    cd mongodb-1.9.0; \
    phpize && ./configure && make && make install;

# apcu
RUN set -eux; \
    cd /root/files; \
    tar xf apcu-5.1.19.tgz; \
    cd apcu-5.1.19; \
    phpize && ./configure && make && make install;

# 啟用擴充套件
RUN set -eux; \
    docker-php-ext-enable apcu redis mongodb opcache skywalking;

# 清理依賴包
RUN set -eux; \
    apk del --no-network .build-deps; \
    docker-php-source delete; \
    rm -rf /root/*;

# nginx初始化
RUN set -eux; \
    deluser --remove-home www-data; \
    addgroup -g 1000 -S www; \
    adduser -u 1000 -D -S -G www www;

RUN set -eux; \
    mkdir -p /var/www; \
    mkdir -p /var/log/php; \
    chown -R www:www /var/www; \
    chown -R www:www /var/log/php; \
    chown -R www:www /var/log/nginx; \
    chown -R www:www /var/lib/nginx; \
    rm -rf /usr/local/etc/php-fpm.d/*;

# 拷貝配置檔案
COPY config/nginx/nginx.conf /etc/nginx/nginx.conf
COPY config/nginx/conf.d   /etc/nginx/conf.d
COPY config/fpm/php-fpm.conf /usr/local/etc/php-fpm.conf
COPY config/fpm/php-fpm.d /usr/local/etc/php-fpm.d
COPY config/php/php.ini /usr/local/etc/php/php.ini
COPY config/supervisor/supervisord.conf /etc/supervisord.conf
COPY config/supervisor/supervisor.d /etc/supervisor.d
COPY html /var/www/html
COPY startup.sh /opt/

# 配置專案名稱及 專案根目錄
ENV APP_NAME="newfinance" \
    APP_ROOT="/var/www/newfinance"

# 設定工作目錄
WORKDIR ${APP_ROOT}

VOLUME ['${APP_ROOT}', '/etc/nginx/conf.d', '/usr/local/etc/php-fpm.d']

# 暴露埠
EXPOSE 80 443

# 守護程序
CMD ["/opt/startup.sh"]

構建流程

將docker 資料夾copy到專案目錄,執行

sh ./deploy_dev.sh

最終會啟動四個容器

 gaoz@nobodyMBP  docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES
d1d00980fe7f        dev_service_a       "docker-php-entrypoi…"   2 hours ago         Up 2 hours          443/tcp, 9000/tcp, 0.0.0.0:11008->80/tcp   ServiceA
310b8e2b746c        dev_service_b       "docker-php-entrypoi…"   2 hours ago         Up 2 hours          443/tcp, 9000/tcp, 0.0.0.0:11009->80/tcp   ServiceB
630d33a4d5f2        mysql:8.0           "docker-entrypoint.s…"   4 hours ago         Up 2 hours          33060/tcp, 0.0.0.0:13306->3306/tcp         wby_dev_mysql
d3344ff1d9c4        redis:6.0           "docker-entrypoint.s…"   4 hours ago         Up 2 hours          0.0.0.0:16379->6379/tcp                    wby_dev_redis