Docker deploy development environment

Docker deploy development environment

Foreword

       When Docker Docker Compose and deploy more of a rights issue, if the data volume is created, but can not start, and their respective rights under inspection data volume directory.

 

Front

SSH

       After the free SSH password is secret but also because .ssh directory permissions and owner.

       .ssh directory must be set if the current owner and user, such as user A: User A, to avoid user A: a group of

chmod 700 /home/username

chmod 700 ~/.ssh/

chmod 600 ~/.ssh/authorized_keys

       chown -R user: user / home / user /.ssh

 

Docker Compose Gogs

YML

version: '3'

 

services:

  gogs:

    #CPU name

    hostname: gogs

    network_mode: "host"

    # Mirror

    image: gogs / gogs: latest

    # Container name

    container_name: gogs

    # Startup mode

    restart: always

    environment:

     - TZ = Asia / Shanghai # time zone

    #port

    ports:

      - 80:80

      - 3000:3000

    volumes:

      - "/ etc / localtime: / etc / localtime: ro" # Set the host vessel region and consistent

      - / home / username / gogs / data: / data

 

docker-compose -f gogs.docker.yml up -d

Check the firewall

 

Configuration

Database suggestion is to use SQLlite, ease of migration

 

gogs migration

       Go to Control Panel

重新同步所有仓库的 pre-receive、update 和 post-receive 钩子;

      

Docker Compose MySQL

yml

version: '3'

services:

 #服务名

 mysql:

  network_mode: "host"

  #主机名

  hostname: mysql

  #容器名称

  container_name: mysql

  #环境变量

  environment:

   TZ: Asia/Shanghai

   #ROOT密码

   MYSQL_ROOT_PASSWORD: 'xxxxxxxxxxxx'

   #自定义用户

   MYSQL_USER: 'xxxxxxxxx'

   MYSQL_PASS: 'xxxxxxxxxxxx

  #镜像

  image: "mysql:5.7.27"

  #启动模式,当值为always时,容器总是重新启动;当值为no-failure时,即出现报错容器退出时,容器重新启动

  #restart: always

  restart: unless-stopped # 设置容器自启模式

  command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # 设置utf8字符集

  #同步文件

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

   - "/home/用户名/mysql/data:/var/lib/mysql"

   - "/home/用户名/mysql/logs:/logs"

   - "/home/用户名/mysql/my.cnf:/etc/my.cnf"

   - "/home/用户名/mysql/init.d:/docker-entrypoint-initdb.d/"

  #命令

  command:

    --default-authentication-plugin=mysql_native_password

  #端口

  ports:

   - "3306:3306"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

docker-compose -f mysql.docker.yml up -d

检查防火墙

 

Docker Compose MongoDB

yml

version: '3'

services:

 #服务名

 mongo:

  network_mode: "host"

  #主机名

  hostname: mongo

  #容器名称

  container_name: mongo

  #环境变量

  environment:

   TZ: Asia/Shanghai

   MONGO_INITDB_ROOT_USERNAME: root

   MONGO_INITDB_ROOT_PASSWORD: 111111

   MONGO_INITDB_USERNAME: xxxxxxx

   MONGO_INITDB_PASSWORD: 22222222

  #镜像

  image: "mongo:3.6"

  #启动模式,当值为always时,容器总是重新启动;当值为no-failure时,即出现报错容器退出时,容器重新启动

  restart: always

  #同步文件

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

   - "/home/用户名/mongo/data:/data/db"

   - "/home/用户名/mongo/log:/data/log"

  #端口

  ports:

   - "27017:27017"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

docker-compose -f mongo.docker.yml up -d

检查防火墙

权限配置

use admin

db.auth('root','密码')

db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]})

db.createUser({user:'账号',pwd:'密码',roles:[{role:'sysadmin',db:'admin'}]})

db.system.users.find()

 

Docker Compose Redis

yml

version: '3'

services:

 redis:

  network_mode: "host" 

  #主机名

  hostname: redis

  #镜像

  image: redis:5.0

  #容器名称

  container_name: redis

  #启动模式

  restart: unless-stopped # 设置容器自启模式

  command: redis-server /etc/redis/redis.conf # 启动redis命令

  environment:

     - TZ=Asia/Shanghai #时区

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

   - "/home/用户名/redis/data:/data" #数据

   - "/home/用户名/redis/redis.conf:/etc/redis/redis.conf" #配置文件

  ports:

   - "6379:6379"

 

 

 

 

 

 

 

 

 

 

 

 

 

docker-compose -f redis.docker.yml up -d

检查防火墙

 

Docker Compose Nexus

yml

version: '3'

services:

 #服务名

 nexus:

  #网络

  network_mode: "host"

  #启动模式,当值为always时,容器总是重新启动;当值为no-failure时,即出现报错容器退出时,容器重新启动

  restart: always

  #环境变量

  environment:

   TZ: Asia/Shanghai

  #同步文件

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

  #镜像

  image: sonatype/nexus3

  #容器名

  container_name: nexus

  #端口

  ports:

   - 8081:8081

   - 443:443

  volumes:

   - /home/用户名/nexus/data:/nexus-data

 

docker-compose -f nexus.docker.yml up -d

检查防火墙

 

配置免密拉取jar包

Nexus访问地址/#admin/security/anonymous

勾上:Allow anonymous users to access the server

 

Docker Compose Jenkins结合Tomcat发布

yml

version: '3'

services:

 jenkins:

  network_mode: "host"

  #镜像

  image: jenkins/jenkins

  #容器名称

  container_name: jenkins

  #启动模式

  restart: always # 设置容器自启模式

  environment:

     - TZ=Asia/Shanghai #时区

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

   - "/home/用户名/jenkins/data:/var/jenkins_home"

   - "/home/用户名/jenkins/docker.sock:/var/run/docker.sock"

   - "/home/用户名/jenkins/docker:/usr/bin/docker"

   - "/home/用户名/jenkins/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7"

  ports:

   - "8080:8080"

   - "5000:5000"

 

docker-compose -f jenkins.docker.yml up -d

检查防火墙

 

配置

安装插件加速:

Jenkins访问地址/pluginManager/advanced

Update Site:URL:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

安装插件

Publish Over SSH

Git

Maven Integration plugin
Pipeline Maven Integration Plugin

 

Maven插件工作工作:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven/

 

配置上SSH

 

构建项目

拉取下来的项目工作空间:/var/jenkins_home/jobs/构建的项目名/workspce/

 

SSH配置

Source files               web/target/test.war

Remove prefix           web/target/

Remote directory      /home/用户名/docker/tomcat

Exec command          sh /home/用户名/docker/tomcat/tomcat.bash.sh docker容器名 /home/用户名/docker/tomcat/ web(war包名) 82(对外端口)

 

构建脚本

#!/bin/bash

set -e

#---------- 运行命令

# sh test.sh 容器名 /home/用户名/tomcat/ web(war包名) 82(对外端口)

#========================== 环境变量 ==========================

#服务名

SERVICE_NAME=$1

#Tomcat路径

TOMCAT_PATH=$2

#war包名

PACKET_NAME=$3

#外部访问的端口号

REQUEST_PORT=$4

 

echo "服务名:${SERVICE_NAME} - Tomcat路径:${TOMCAT_PATH} - 包名:${PACKET_NAME} - 外部访问的端口号:${REQUEST_PORT}"

 

#========================== 容器是否创建 ==========================

#判断容器是否已创建

if [[ `docker ps -a | grep ${SERVICE_NAME}` ]];

then

    # 容器已创建,只重启

    #docker restart ${SERVICE_NAME}

    echo "容器已创建"

else

    # 如果容器未创建,创建容器

    echo "开始创建容器"

    docker run -d -p ${REQUEST_PORT}:8080 --name ${SERVICE_NAME} -v /etc/localtime:/etc/localtime:ro -v ${TOMCAT_PATH}/${SERVICE_NAME}/logs:/usr/local/tomcat/logs/ -v ${TOMCAT_PATH}/${SERVICE_NAME}/webapps:/usr/local/tomcat/webapps/ --privileged=true --restart=always tomcat:8.5

    echo "${SERVICE_NAME}容器已创建"

    echo "容器创建命令:docker run -d -p ${REQUEST_PORT}:8080 --name ${SERVICE_NAME} -v ${TOMCAT_PATH}/${SERVICE_NAME}/logs:/usr/local/tomcat/logs/ -v ${TOMCAT_PATH}/${SERVICE_NAME}/webapps:/usr/local/tomcat/webapps/ --privileged=true --restart=always tomcat:8.5"

fi;

 

# ==========================包处理  ==========================

 

#进入目录

cd ${TOMCAT_PATH}

 

#war重置为zip压缩文件

mv ${PACKET_NAME}.war ${PACKET_NAME}.zip

 

echo "源文件名:${PACKET_NAME}.war - 新文件名:${PACKET_NAME}.zip"

 

#创建ROOT文件夹

mkdir ROOT

 

#解压到ROOT

unzip ${PACKET_NAME}.zip -d ROOT

touch ROOT/$(date +%Y%m%d-%H%M%S)

 

#删除docker-tomcat的ROOT文件夹

echo "Start 删除ROOT文件夹"

#sudo docker exec -i ${SERVICE_NAME} rm -rf /usr/local/tomcat/webapps/ROOT

#sudo docker exec -i ${SERVICE_NAME} ls -l /usr/local/tomcat/webapps/

sudo rm -rf ${TOMCAT_PATH}/${SERVICE_NAME}/webapps/ROOT

sudo ls -l ${TOMCAT_PATH}/${SERVICE_NAME}/webapps/

echo "End 删除ROOT文件夹"

 

#复制到webapps中

#sudo docker cp ${TOMCAT_PATH}/ROOT ${SERVICE_NAME}:/usr/local/tomcat/webapps

sudo mv ROOT ${TOMCAT_PATH}/${SERVICE_NAME}/webapps/

 

#查看目录

#sudo docker exec -i ${SERVICE_NAME} ls -l webapps

sudo ls -l ${TOMCAT_PATH}/${SERVICE_NAME}/webapps/

 

#删除本地ROOT文件夹

rm -rf ROOT

rm -rf ${PACKET_NAME}.zip

echo "End 删除本地文件"

 

#重启

sudo docker restart ${SERVICE_NAME}

echo "${SERVICE_NAME} 重启完成"

 

#运行中的容器

sudo docker ps

 

# 容器在运行

#if [[ `docker ps | grep ${SERVICE_NAME}` ]];

#then

    # 停止运行中的容器

    #docker stop ${SERVICE_NAME}

#fi;

 

Docker Compose Nginx

version: '3'

services:

 #服务名

 nginx:

  network_mode: "host"

  #主机名

  hostname: nginx

  #容器名称

  container_name: nginx

  #环境变量

  environment:

   TZ: Asia/Shanghai

  #镜像

  image: "nginx:latest"

  #启动模式,当值为always时,容器总是重新启动;当值为no-failure时,即出现报错容器退出时,容器重新启动

  restart: always

  #同步文件

  volumes:

   - "/etc/localtime:/etc/localtime:ro" # 设置容器时区与宿主机保持一致

   - ./conf/nginx.conf:/etc/nginx/nginx.conf            #映射nginx的配置文件到容器里

   - ./logs/:/var/log/nginx/

   #- ./html/:/var/share/nginx/html/                     #映射nginx的网页目录到容器里

  

  #端口

  ports:

   - "80:80"

   - "443:443"

 

nginx.conf

user  nginx;

worker_processes  1;

 

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

 

events {

    worker_connections  1024;

}

 

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

    access_log  /var/log/nginx/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    keepalive_timeout  65;

 

    #gzip  on;

 

    include /etc/nginx/conf.d/*.conf;

   

    #负载均衡

    upstream test {

        server 127.0.0.1:82;

        server 127.0.0.1:82 backup;  #热备    

        #ip_hash; #指定同一IP只能访问同一Tomcat,解决session问题

        #server 127.0.0.1:82 weight=1;

    }

    #代理配置

    server {

        listen       80;

        server_name  www.test.com;

 

        #首页地址特殊,访问首页

        location = / {

            proxy_pass http://test/main;

        }

        #其他访问则作为普通访问处理

        location / {

            proxy_pass http://test;

        }

        #避免图片等元素无法加载

        location ~ .*\.(html|htm|ico|png|jpg|jpeg|js|css|bmp)$ {

            proxy_pass http://test;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

 

 

发布了103 篇原创文章 · 获赞 34 · 访问量 7万+

Guess you like

Origin blog.csdn.net/Su_Levi_Wei/article/details/101531835