Docker:Linux定时脚本(从jenkins容器取jar包部署)

Docker:Linux定时脚本(从jenkins容器取jar包部署)

主要是实现用linux定时脚本定时从jenkins容器取自动构建好的jar生成镜像并运行容器

crontab定时任务服务安装

  • 安装crontab定时任务服务
    yum install vixie-cron
    vixie-cron软件包是cron的主程序
    yum install crontabs
    crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序
  • 开启crontab服务
    service crond start
  • 查看crontab服务状态
    service crond status
  • 设置为开机启动
    chkconfig crond on
  • 设置定时任务
    crontab配置参考https://www.cnblogs.com/xd502djj/p/4292781.html
    • 换到root 的身份 su - root
    • crontab -e (按a增加下)
    • 0 12,18,23 * /robotservice2.0/robotservice.sh -h now
    • 保存退出 wq!
  • 列出当前的所有调度任务
    crontab -l

目录结构

"图片描述"

自定义网络

自定义bridge网络与默认bridge网络对比:

  • 默认桥接网络中的容器只能通过IP地址访问其他容器(除非使用遗留的-link指令连接两个容器),而自定义桥接网络提供DNS解析,可以通过容器的名字或是别名访问其他容器
  • 容器可以自由的进入或是退出自定义桥接网络,如果想要退出默认桥接网络,需要先停止容器的运行,然后重新创建该容器,并指定需要连接的其他网络
  • 如果更改了默认桥接网络的网络配置,需要重新启动docker,并且由于默认桥接网络只有一个,因此所有容器的网络配置都是一样的,而用户自定义网络可以在创建时指定网络配置(例如默认网关、MTU等),不需要重启docker,灵活性更高
  • 在默认桥接网络中,可以通过–link参数连接两个容器来共享环境变量,用户自定义网络中无法使用这种方式,但是docker提供了更好的方式:
    • 多个容器可以使用docker volume(这是docker存储数据的一种方式,以后会介绍)挂载到同一个文件,在文件中指明环境变量,从而实现所容器的环境变量共享
    • 多个容器可以使用同一个docker-compose(与docker service有关)文件启动 ,可以在该 件中定义共享环境变量
    • 可以使用swarm services,并且通过 secrets 和 configs (这两个还没看)实现环境变量共享

创建自定义网络

  • 创建一个自定义网络
    docker network create docker_network
  • 查看自定义网络
    docker network ls
  • 移除自定义网络
    docker network rm docker_network
  • docker容器运行时指定自定义网络和ip
    docker run –network=docker_network –ip=172.25.2.1 –name eureka-server -p 1001:1001 -d eureka-server
  • 断开容器连接
    docker network disconnect docker_network containername

shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# jenkins容器id
JENKINS_ID=$(docker ps | grep "jenkins" | awk '{print $1}')
# 复制微服务jar到对应文件夹
docker cp $JENKINS_ID:/robotservice2.0 /
# 删除运行容器
echo "删除容器 ..."
docker ps -a | grep "eureka" | awk '{print "docker stop " $12}' | sh
docker ps -a | grep "config-server-native" | awk '{print "docker stop " $12}' | sh

docker ps -a | grep "eureka" | awk '{print "docker rm " $12}' | sh
docker ps -a | grep "config-server-native" | awk '{print "docker rm " $12}' | sh
echo "删除容器成功"
# 删除镜像
echo "存在镜像,IID=$IID,删除镜像"
docker images | grep "eureka" | awk '{print "docker rmi " $3}'
docker images | grep "config-server-native" | awk '{print "docker rmi " $3}'
echo "删除镜像成功"
# 生成对应微服务镜像
docker build -t eureka-server --target eureka-server .
docker build -t eureka-client-zipkin --target eureka-client-zipkin .
docker build -t config-server-native --target config-server-native .
docker build -t eureka-gateway --target eureka-gateway .
docker build -t eureka-client-web-jsp --target eureka-client-web-jsp .
docker build -t eureka-client-user --target eureka-client-user .
docker build -t eureka-client-device --target eureka-client-device .
docker build -t eureka-client-patrol --target eureka-client-patrol .
docker build -t eureka-client-qpid --target eureka-client-qpid .
docker build -t eureka-client-report --target eureka-client-report .
docker build -t eureka-client-interactive --target eureka-client-interactive .
docker build -t eureka-client-oauth --target eureka-client-oauth .
docker build -t eureka-client-schedule --target eureka-client-schedule .
docker build -t eureka-client-file --target eureka-client-file .
# 运行镜像
docker run --network=docker_network --ip=172.25.2.1 --name eureka-server -p 1001:1001 -d eureka-server
docker run --network=docker_network --ip=172.25.2.3 --name config-server-native -p 1003:1003 -d config-server-native
sleep 3m
docker run --network=docker_network --ip=172.25.2.2 --name eureka-client-zipkin -p 1002:1002 -d eureka-client-zipkin
docker run --network=docker_network --ip=172.25.2.4 --name eureka-gateway -p 2001:2001 -d eureka-gateway
docker run --network=docker_network --ip=172.25.2.5 --name eureka-client-web-jsp -p 2002:2002 -d eureka-client-web-jsp
docker run --network=docker_network --ip=172.25.2.6 --name eureka-client-user -p 2003:2003 -d eureka-client-user
docker run --network=docker_network --ip=172.25.2.7 --name eureka-client-device -p 2004:2004 -d eureka-client-device
docker run --network=docker_network --ip=172.25.2.8 --name eureka-client-patrol -p 2005:2005 -d eureka-client-patrol
docker run --network=docker_network --ip=172.25.2.9 --name eureka-client-qpid -p 2006:2006 -d eureka-client-qpid
docker run --network=docker_network --ip=172.25.2.10 --name eureka-client-report -p 2007:2007 -d eureka-client-report
docker run --network=docker_network --ip=172.25.2.11 --name eureka-client-interactive -p 2008:2008 -d eureka-client-interactive
docker run --network=docker_network --ip=172.25.2.12 --name eureka-client-oauth -p 2009:2009 -d eureka-client-oauth
docker run --network=docker_network --ip=172.25.2.13 --name eureka-client-schedule -p 2010:2010 -d eureka-client-schedule
docker run --network=docker_network --ip=172.25.2.14 --name eureka-client-file -p 2011:2011 -d eureka-client-file

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
FROM openjdk:8u181-jdk-alpine AS eureka-server
VOLUME /tmp
ADD lib /lib
ADD eureka-server-0.0.1-SNAPSHOT.jar eureka-server-0.0.1-SNAPSHOT.jar
EXPOSE 1001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-server-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-zipkin
VOLUME /tmp
ADD lib /lib
ADD eureka-client-zipkin-0.0.1-SNAPSHOT.jar eureka-client-zipkin-0.0.1-SNAPSHOT.jar
EXPOSE 1002
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-zipkin-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS config-server-native
VOLUME /tmp
ADD lib /lib
ADD config-server-native-0.0.1-SNAPSHOT.jar config-server-native-0.0.1-SNAPSHOT.jar
EXPOSE 1003
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/config-server-native-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-gateway
VOLUME /tmp
ADD lib /lib
ADD eureka-gateway-0.0.1-SNAPSHOT.jar eureka-gateway-0.0.1-SNAPSHOT.jar
EXPOSE 2001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-gateway-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-web-jsp
VOLUME /tmp
ADD lib /lib
ADD eureka-client-web-jsp-0.0.1-SNAPSHOT.jar eureka-client-web-jsp-0.0.1-SNAPSHOT.jar
EXPOSE 2002
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-web-jsp-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-user
VOLUME /tmp
ADD lib /lib
ADD eureka-client-user-0.0.1-SNAPSHOT.jar eureka-client-user-0.0.1-SNAPSHOT.jar
EXPOSE 2003
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-user-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-device
VOLUME /tmp
ADD lib /lib
ADD eureka-client-device-0.0.1-SNAPSHOT.jar eureka-client-device-0.0.1-SNAPSHOT.jar
EXPOSE 2004
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-device-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-patrol
VOLUME /tmp
ADD lib /lib
ADD eureka-client-patrol-0.0.1-SNAPSHOT.jar eureka-client-patrol-0.0.1-SNAPSHOT.jar
EXPOSE 2005
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-patrol-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-qpid
VOLUME /tmp
ADD lib /lib
ADD eureka-client-qpid-0.0.1-SNAPSHOT.jar eureka-client-qpid-0.0.1-SNAPSHOT.jar
EXPOSE 2006
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-qpid-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-report
VOLUME /tmp
ADD lib /lib
ADD eureka-client-report-0.0.1-SNAPSHOT.jar eureka-client-report-0.0.1-SNAPSHOT.jar
EXPOSE 2007
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-report-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-interactive
VOLUME /tmp
ADD lib /lib
ADD eureka-client-interactive-0.0.1-SNAPSHOT.jar eureka-client-interactive-0.0.1-SNAPSHOT.jar
EXPOSE 2008
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-interactive-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-oauth
VOLUME /tmp
ADD lib /lib
ADD eureka-client-oauth-0.0.1-SNAPSHOT.jar eureka-client-oauth-0.0.1-SNAPSHOT.jar
EXPOSE 2009
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-oauth-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-schedule
VOLUME /tmp
ADD lib /lib
ADD eureka-client-schedule-0.0.1-SNAPSHOT.jar eureka-client-schedule-0.0.1-SNAPSHOT.jar
EXPOSE 2010
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-schedule-0.0.1-SNAPSHOT.jar"]

FROM openjdk:8u181-jdk-alpine AS eureka-client-file
VOLUME /tmp
ADD lib /lib
ADD eureka-client-file-0.0.1-SNAPSHOT.jar eureka-client-file-0.0.1-SNAPSHOT.jar
EXPOSE 2011
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dloader.path='lib/'","-jar","/eureka-client-file-0.0.1-SNAPSHOT.jar"]

人生两苦:想要却不得,拥有却失去。 –褚禄山
珍惜当下,与君共勉~


本文标题:Docker:Linux定时脚本(从jenkins容器取jar包部署)

文章作者:fufua

发布时间:2019年04月15日 - 10:54:13

最后更新:2019年05月10日 - 16:09:16

原始链接:https://qq1028951741.github.io/2019/04/15/Docker:Linux定时脚本(从jenkins容器取jar包部署)/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

this is end, thank you for reading