docker-compose+ jenkins + gogs+ maven自動化構建與部署

rambler_kieran發表於2018-05-20

準備工作

  1. 版本說明

    版本如下所示:

    • gogs/gogs:0.11.43
    • jenkins/jenkins:2.107.2
    • nginx:1.13.12
    • tomcat:8.0.51-jre8-slim

    本教程所有工具和資料目錄存放於/docker目錄下

  2. 安裝docker

    詳情見docs.docker.com/install/lin…

  3. 安裝docker-compose

    這裡簡單介紹下兩種安裝docker-compose的方式,第一種方式相對簡單,但是由於網路問題,常常安裝不上,並且經常會斷開,第二種方式略微麻煩,但是安裝過程比較穩定。

    • 方式一

      curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
      chmod +x /usr/local/bin/docker-compose
      複製程式碼

      檢視版本資訊:

      docker-compose --version
      複製程式碼

      但是此方法會經常因為網路的原因而無法安裝

    • 方式二

      安裝python-pip

        ```
        yum -y install epel-release
        yum -y install python-pip
        ```
      複製程式碼

      安裝docker-compose

        ```
        pip install docker-compose
        ```
      複製程式碼

      待安裝完成後,執行查詢版本的命令,即可安裝docker-compose

    • 安裝完成如下所示:

      [root@VM_0_14_redhat ~]# docker-compose version
      docker-compose version 1.21.0, build 5920eb0
      docker-py version: 3.2.1
      CPython version: 2.7.5
      OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
      複製程式碼

    ps:參考www.ilanni.com/?p=13371

nginx配置

  1. nginx.conf配置
worker_processes  1;
events {
  worker_connections  1024;
}
http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  open_file_cache max=1000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;


########################## vhost #############################
  include vhost/*.conf;

}
複製程式碼
  1. gogs配置

此處配置https訪問,並將http訪問強制跳轉到https。

server {
    listen       80;
    server_name {your server_name};
    # 跳轉到HTTPS
    return  301 https://$server_name$request_uri;
}

server {
    listen 443;
    server_name {your server_name};

    ssl on;
    ssl_certificate /etc/nginx/ssl/{your server_name}/Nginx/1_{your server_name}_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/{your server_name}/Nginx/2_{your server_name}.cn.key;
    ssl_session_timeout 60m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://{your server_ip}:{your server_port};
    }
}
複製程式碼
  1. jenkins配置
server {  
    listen       80;         
    server_name {your server_name};  
    location / {
        proxy_pass http://{your server_ip}:{your server_port};
    }
}
複製程式碼
  1. mysql配置
aracter-set = utf8

[mysql_safe]
default-character-set = utf8

[client]
default-character-set = utf8

[mysqld]
pid-file      = /var/run/mysqld/mysqld.pid
socket        = /var/run/mysqld/mysqld.sock
datadir       = /var/lib/mysql
#log-error    = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# 修改mysql預設字符集
init_connect  = 'SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci
複製程式碼

gogs+jenkins+nginx+mysql之docker-compose.yml編寫

version: '2'
services:
  gogs:
    image: gogs/gogs:0.11.43
    ports:
        - "10022:22"
        - "10080:3000"
    volumes:
        - /docker/gogs:/data
    restart: always
    links:
        - mysql
    networks:
      study_net:
        ipv4_address: 192.168.8.2
    environment:
      - Domain=118.24.5.240
      - SSH_PORT=10022
      - HTTP_PORT=10080
  
  jenkins:
    image: jenkins/jenkins:2.107.2
    ports:
        - "10081:8080"
        - "50000:50000"
    volumes:
        - /docker/jenkins:/var/jenkins_home
        - /docker/maven/apache-maven-3.5.0:/usr/local/maven
        - /docker/java/jdk1.8.0_171:/usr/local/jdk
    networks:
      study_net:
        ipv4_address: 192.168.8.3
    environment:
        - JAVA_OPTS=-Duser.timezone=Asia/Shanghai
  
  nginx:
    image: nginx:1.13.12
    ports:
        - "80:80"
        - "443:443"
    links:
        - gogs
        - jenkins
    volumes:
        - /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
        - /docker/nginx/conf/vhost:/etc/nginx/vhost:ro
        - /docker/nginx/ssl:/etc/nginx/ssl:ro
        - /docker/nginx/data:/data/nginx:ro
    restart: always
    networks:
      study_net:
        ipv4_address: 192.168.8.4
    
  mysql:
    image: mysql:5.7.22
    volumes:
      - /docker/mysql-single/data:/var/lib/mysql
      - /docker/mysql-single/config/my.cnf:/etc/my.cnf
    ports:
      - "3306:3306"
    networks:
      study_net:
        ipv4_address: 192.168.8.5
    environment:
      MYSQL_ROOT_PASSWORD: sd{1Rj3i)aK3ew
      MYSQL_DATABASE: gogs
      MYSQL_USER: gogs
      MYSQL_PASSWORD: j2d4A{Hs3i)5E6ej
    restart: always

  zookeeper:
    image: chaimm/zookeeper-dubbo:1.0
    ports:
      - "2182:2181"
      - "10083:8080"
    networks:
      study_net:
        ipv4_address: 192.168.8.6
    restart: always

  tomcat-1:
    image:chaimm/tomcat:1.1
    ports:
      - "10101:8080"
    volumes:
      - /docker/web/mall-log:/opt/tomcat/mall-log
    networks:
      study_net:
        ipv4_address: 192.168.8.101

networks: # docker網路設定
  study_net: # 自定義網路名稱
    ipam: # 要使用靜態ip必須使用ipam外掛
      driver: default
      config:
      - subnet: 192.168.8.0/24
複製程式碼

redis(未完待續)

version: '2'
services:
  redis:
    ports:
        - "6379:6379"
    image: "redis:4.0.9"

複製程式碼

相關文章