jenkins+docker安装

1.docker安装 jenkins

docker pull jenkins/jenkins
docker run --name=jenkins --user root -d -p 8080:8080 -v /usr/local/java:/usr/local/java -v /usr/local/maven:/usr/local/maven -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins

docker-compose.yml

version: '3'
services:
  jenkins:
    image: jenkins/jenkins
    container_name: jenkins
    restart: always
    user: root
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 9000:8080
    volumes:
      - /root/docker-app/jenkins/jenkins_home:/var/jenkins_home
      - /usr/local/java:/usr/local/java
      - /usr/local/maven:/usr/local/maven
      - /usr/local/go:/usr/local/go
      - /usr/bin/docker:/usr/bin/docker
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin:/usr/hbin
      - /usr/local/node:/usr/local/node

解释一下:
-d 后台运行容器
-p 端口映射
--user root 指定运行容器角色 否则可能会导致没有权限从而构建失败
-v是将你本地的文件夹挂载到容器指定的文件夹
这些环境的文件夹各位注意需要自己电脑上的文件夹

2.进入系统

登录系统,输入passwordd

docker ps
docker exec 容器id  cat /var/jenkins_home/secrets/initialAdminPassword

3.安装推荐的插件

4.对jenkis进行全局配置

配置java和maven

5.部署

java+docker项目

选“自由风格项目”
dockerfile

FROM java:8

RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone

WORKDIR /opt
COPY target/*.jar /opt/main.jar

EXPOSE 8080
CMD java -jar main.jar

git repositories配置

build 选 “执行shell”

#!/bin/bash
#maven打包
/usr/local/maven/apache-maven-3.8.5/bin/mvn clean package -DskipTests -Ptest
echo 'package ok!'
echo 'build start!'
cd ./
service_name="sw-airlines-server"
#查看容器id
CID=$(docker ps -a| grep "$service_name" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
    echo "exist $service_name container,CID=$CID"
    #停止
    docker stop $service_name
    #删除容器
    docker rm $service_name
else
    echo "no exist $service_name container"
fi
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
    echo "exist $service_name image,IID=$IID"
    #删除镜像
    docker rmi -f $service_name
    echo "delete $service_name image"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
else
    echo "no exist $service_name image,build docker"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
fi

#启动
docker run --name $service_name -d -p 8085:8080 $service_name
#查看启动日志
#docker logs -f  $service_name
go+docker项目

选“自由风格项目”
dockerfile

FROM alpine:3.13.4

RUN echo "http://mirrors.aliyun.com/alpine/v3.4/main/" > /etc/apk/repositories \
&& apk --no-cache add tzdata zeromq \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone

WORKDIR  /app

ADD main /app/main
ADD config.yaml /app/config.yaml

EXPOSE 9100

ENTRYPOINT ["./main"]

git repositories配置

build 选 “执行shell”

#!/bin/bash
#打包
cd $WORKSPACE
export GOPROXY=https://goproxy.io
go mod tidy
# 打印依赖,部署成功后查看版本依赖是否如预期
cat ./go.mod

# cd ./app/test/http
# linux环境编译
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main

echo 'package ok!'
echo 'build start!'

service_name="agent-server"
#查看容器id
CID=$(docker ps -a| grep "$service_name" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
    echo "exist $service_name container,CID=$CID"
    #停止
    docker stop $service_name
    #删除容器
    docker rm $service_name
else
    echo "no exist $service_name container"
fi
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
    echo "exist $service_name image,IID=$IID"
    #删除镜像
    docker rmi -f $service_name
    echo "delete $service_name image"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
else
    echo "no exist $service_name image,build docker"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
fi

#启动
docker run --name $service_name -d -p 9100:9100 $service_name
#查看启动日志
#docker logs -f  $service_name

vue+docker项目

选“自由风格项目”
dockerfile

FROM nginx

MAINTAINER zlb
COPY dist/  /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/conf.d/default.conf

# pnpm run dev      # dev配置运行
# pnpm run build    # dist打包

nginx.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    error_log  /var/log/nginx/error.log  error;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

git repositories配置

build 选 “执行shell”

#!/bin/bash
#pnpm打包
npm install 
npm run build:test
echo 'package ok!'
echo 'build start!'
cd ./
service_name="sw-airlines-web"
#查看容器id
CID=$(docker ps -a| grep "$service_name" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
    echo "exist $service_name container,CID=$CID"
    #停止
    docker stop $service_name
    #删除容器
    docker rm $service_name
else
    echo "no exist $service_name container"
fi
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
    echo "exist $service_name image,IID=$IID"
    #删除镜像
    docker rmi -f $service_name
    echo "delete $service_name image"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
else
    echo "no exist $service_name image,build docker"
    #构建
    docker build -t $service_name .
    echo "build $service_name image"
fi

#启动
docker run --name $service_name -d -p 8086:80 $service_name
#查看启动日志
#docker logs -f  $service_name
posted @ 2022-05-17 11:35  不止吧  阅读(152)  评论(0编辑  收藏  举报