docker 部署PHP与golang项目

参考文章

目录

docker服务的启动
#启动 
systemctl start docker

#守护进程重启
systemctl daemon-reload

#重启docker服务
systemctl restart docker  /  service docker restart

#关闭
docker service docker stop / docker systemctl stop docker
目录结构

.
+--- files
|   +--- docker-compose.yml
|   +--- es
|   |   +--- esdata1
|   +--- golang
|   |   +--- .env_online.conf
|   |   +--- gh_data
|   +--- mysql
|   |   +--- Dockerfile
|   |   +--- initdb
|   |   |   +--- geagle.sql
|   +--- nginx
|   |   +--- conf.d
|   |   |   +--- dev.ghweb.com.conf
|   |   |   +--- dev.ghweb.html.com.conf
|   |   +--- Dockerfile
|   |   +--- nginx.conf
|   +--- php
|   |   +--- Dockerfile
|   |   +--- php-fpm.conf
|   |   +--- php-fpm.d
|   |   |   +--- www.conf.default
|   |   +--- php.ini
|   |   +--- supervisor
|   |   |   +--- supervisord.conf
|   |   |   +--- supervisord.d
|   |   |   |   +--- gh_asset_expire.conf
|   |   |   |   +--- gh_data.conf
|   |   |   |   +--- gh_logo_result.conf
|   |   |   |   +--- gh_msg_report.conf
|   |   |   |   +--- gh_op_asset.conf
|   |   |   |   +--- gh_overtime_event.conf
|   |   |   |   +--- gh_report.conf
|   |   |   |   +--- gh_send_notice.conf
|   |   |   |   +--- gh_tag.conf
|   +--- redis
|   |   +--- data
|   |   +--- Dockerfile
|   |   +--- redis.conf
+--- logs
|   +--- ghdata
|   |   +--- gh_error_log
|   |   +--- gh_normal_log
|   +--- mysql
|   +--- nginx
|   |   +--- access.log
|   |   +--- error.log
|   |   +--- gh_web.access.log
|   |   +--- gh_web.error.log
|   |   +--- gh_web_front.access.log
|   |   +--- gh_web_front.error.log
|   +--- php-fpm
|   |   +--- php-fpm.log
|   +--- redis
|   |   +--- redis.log
|   +--- supervisor
|   |   +--- cron.log
|   |   +--- gh_asset_expire.log
docker-compose.yml

docker-compose up,docker-compose down 进行对容器的操作


version: '2.0'
services:
  elasticsearch:
      image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/es:5.2.2
      environment:
        - cluster.name=docker-cluster
        - bootstrap.memory_lock=true
        - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      ulimits:
        memlock:
          soft: -1
          hard: -1
        nofile:
          soft: 65536
          hard: 65536
      mem_limit: 1g
      cap_add:
        - IPC_LOCK
      volumes:
        - ./es/esdata1:/usr/share/elasticsearch/data
      ports:
        - 9200:9200
      networks:
        - my-net

  php-fpm:
    image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/php

    depends_on:
      - elasticsearch
    ports:
      - "9002:9002"
    links:
      - mysql-db:mysql-db
      - redis-db:redis-db
      - elasticsearch:elasticsearch
    volumes:
      - ../app:/wwwroot:rw
      - ./php/php.ini:/usr/local/etc/php/php.ini:ro
      - ./php/php-fpm.conf:/usr/local/etc/php-fpm.conf:ro
      - ./php/supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - ./php/supervisor/supervisord.d/:/etc/supervisor/supervisord.d
      - ../logs/php-fpm:/var/log/php-fpm:rw
      - ../logs/supervisor:/var/log/supervisor
    restart: always
    command: /bin/sh -c "supervisord -c /etc/supervisor/supervisord.conf && /etc/init.d/cron start && echo '* * * * * export OS_STATUS="PRE" && /usr/local/bin/php /wwwroot/zctc_asset_manage/artisan schedule:run >> /tmp/cron.log 2>&1' | crontab -u www - && crontab -l -u www && php-fpm"
    #command: /bin/sh -c "supervisord -c /etc/supervisor/supervisord.conf && /etc/init.d/cron start && echo '* * * * * /usr/local/bin/php /wwwroot/gh_web/artisan schedule:run >> /dev/null 2>&1' | crontab -u www - && crontab -l -u www && php-fpm"
    networks:
        - my-net

  nginx:
    image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/nginx
    depends_on:
      - php-fpm
    links:
      - php-fpm:php-fpm
    volumes:
      - ../app:/wwwroot:rw
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ../logs/nginx:/var/log/nginx
    ports:
      - "8009:8009"
      - "8010:8010"
    restart: always
    command: nginx -g 'daemon off;'
    networks:
        - my-net

  mysql-db:
      image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/mysql
      ports:
        - "33062:3306"
      volumes:
        - ../logs/mysql:/var/lib/mysql-logs:rw
        - ./mysql/initdb:/docker-entrypoint-initdb.d
        - ./mysql/data:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: 123456
        MYSQL_DATABASE: geagle
        MYSQL_USER: geagle
        MYSQL_PASSWORD: 123456
      restart: always
      command: "--character-set-server=utf8"
      networks:
        - my-net

  redis-db:
      image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/redis
      ports:
        - "6380:6379"
      volumes:
        - ./redis/data:/data
        - ../logs/redis:/var/log/redis
      restart: always
      command: redis-server --requirepass 123456
      networks:
        - my-net

  ghdata:
      image: xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/ghdata
      depends_on:
        - elasticsearch
        - mysql-db
      environment:
        - GOENV=online
        - TZ=Asia/Shanghai
      links:
        - mysql-db:mysql-db
        - redis-db:redis-db
        - elasticsearch:elasticsearch
      volumes:
        - ./golang:/go
        - ../logs/ghdata:/tmp
      command: "./gh_data"
      restart: always
      networks:
        - my-net

networks:
  my-net:
    driver: bridge

其中 ./mysql/initdb:/docker-entrypoint-initdb.d 这个挂载是因为使用的镜像提供了sql语句初始化功能, 说明如下

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

PHP的Dockerfile

怎样查找和安装需要的扩展

FROM php:7.2-fpm

# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 更新安装依赖包和PHP核心拓展
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libxml2-dev \
        libcurl4-openssl-dev \
        libxslt1-dev  \
        supervisor \
        cron \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-install zip \
        && docker-php-ext-install xml \
        && docker-php-ext-install curl \
        && docker-php-ext-install mbstring \
        && docker-php-ext-install xsl \
        && docker-php-ext-install pdo_mysql \
        && docker-php-ext-install opcache \
        && docker-php-ext-install mysqli \
        && rm -r /var/lib/apt/lists/*

WORKDIR /wwwroot

# Write Permission
RUN useradd  www
RUN usermod -a -G root www
RUN usermod -u 1000 www
RUN groups www
RUN id www
RUN mkdir -p /var/log/php-fpm/
RUN mkdir -p /var/run/supervisor/
制作好的镜像上传
  1. 给镜像打上需要上传的仓库的tag
  2. 登录仓库
  3. 上传到仓库

> docker login http://xxxxxx.xxx.xxxxx.cn
Username: ytc_cloud_scan
Password:
Error: Password Required

> docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
<none>                                          <none>              92d380c9fde3        27 hours ago        546MB
<none>                                          <none>              f6ea2e9f4344        28 hours ago        546MB
files_php-fpm                                   latest              57bbc5e67c2f        28 hours ago        546MB
xxxxxx.xxx.xxxxx.cn/ytc_cloud_scan/box/php      latest              57bbc5e67c2f        28 hours ago        546MB
<none>                                          <none>              7bd2873cac09        44 hours ago        487MB
files_nginx                                     latest              a06abb1a0495        4 days ago          108MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/nginx    latest              a06abb1a0495        4 days ago          108MB
files_redis-db                                  latest              b729236afc7d        4 days ago          76MB
files_mysql-db                                  latest              7bd85a5751a3        4 days ago          437MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/mysql    latest              7bd85a5751a3        4 days ago          437MB
ghdata                                          latest              3024b4e742b0        6 days ago          359MB
golang                                          alpine              3024b4e742b0        6 days ago          359MB
golang                                          1.13.4-stretch      1e279d13e4cb        6 days ago          763MB
php                                             7.2-fpm             48432d192e1a        2 weeks ago         398MB
php                                             7.2-cli             7cae463c729f        2 weeks ago         398MB
bulletinboard                                   1.0                 bc4b9d136410        2 weeks ago         681MB
mysql                                           5.7                 cd3ed0dfff7e        3 weeks ago         437MB
centos                                          7                   67fa590cfc1c        2 months ago        202MB
redis                                           3.2                 87856cc39862        12 months ago       76MB
nginx                                           1.12                4037a5562b03        18 months ago       108MB
node                                            6.11.5              852391892b9f        2 years ago         662MB
docker.elastic.co/elasticsearch/elasticsearch   5.2.2               89315294a341        2 years ago         206MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/es       5.2.2               89315294a341        2 years ago         206MB

> docker tag golang:alpine xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/ghdata

> docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
<none>                                          <none>              92d380c9fde3        28 hours ago        546MB
<none>                                          <none>              f6ea2e9f4344        28 hours ago        546MB
files_php-fpm                                   latest              57bbc5e67c2f        28 hours ago        546MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/php      latest              57bbc5e67c2f        28 hours ago        546MB
<none>                                          <none>              7bd2873cac09        44 hours ago        487MB
files_nginx                                     latest              a06abb1a0495        4 days ago          108MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/nginx    latest              a06abb1a0495        4 days ago          108MB
files_redis-db                                  latest              b729236afc7d        4 days ago          76MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/mysql    latest              7bd85a5751a3        4 days ago          437MB
files_mysql-db                                  latest              7bd85a5751a3        4 days ago          437MB
ghdata                                          latest              3024b4e742b0        6 days ago          359MB
golang                                          alpine              3024b4e742b0        6 days ago          359MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/ghdata   latest              3024b4e742b0        6 days ago          359MB
golang                                          1.13.4-stretch      1e279d13e4cb        6 days ago          763MB
php                                             7.2-fpm             48432d192e1a        2 weeks ago         398MB
php                                             7.2-cli             7cae463c729f        2 weeks ago         398MB
bulletinboard                                   1.0                 bc4b9d136410        2 weeks ago         681MB
mysql                                           5.7                 cd3ed0dfff7e        3 weeks ago         437MB
centos                                          7                   67fa590cfc1c        2 months ago        202MB
redis                                           3.2                 87856cc39862        12 months ago       76MB
nginx                                           1.12                4037a5562b03        18 months ago       108MB
node                                            6.11.5              852391892b9f        2 years ago         662MB
docker.elastic.co/elasticsearch/elasticsearch   5.2.2               89315294a341        2 years ago         206MB
xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/es       5.2.2               89315294a341        2 years ago         206MB

> docker push  xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/ghdata
The push refers to repository [xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/ghdata]
a17f85ec7605: Pushed
2895b872dff5: Pushed
eed8c158e67f: Pushed
2033402d2275: Pushed
77cae8ab23bf: Pushed
latest: digest: sha256:679fe3791d2710d53efe26b05ba1c7083178d6375318b0c669b6bcd98f25c448 size: 1365
代码中的数据库连接

networks的配置使容器内部相互通信. 通过service名称可以连接到对应的容器。

如果ghdata容器想要请求nginx容器提供的api 那么请求地址为http://nginx:8010

示例如下:

REDIS_HOST=redis-db
REDIS_PASSWORD=xxx
REDIS_PORT=6379
REDIS_DB=0

API_URL=http://nginx:8010
镜像的导入和导出
#镜像的保存
docker save xxxxx.xxx.xxxxxx.cn/ytc_cloud_scan/box/engine:v1.0 -o engine

镜像的载入
docker load -i engine