赞
踩
采用Centos7.9安装Docker,官方文档 https://docs.docker.com/engine/install/centos/
卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装
# 安装依赖包 yum install -y yum-utils # 配置镜像源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 列出存储库中的可用的Docker版本(本次选在 23.0.6 ) yum list docker-ce --showduplicates | sort -r # 安装Docker Engine 安装目录是:/var/lib/docker/ yum install docker-ce-23.0.6 docker-ce-cli-23.0.6 systemctl start docker #启动 systemctl restart docker #重启 systemctl stop docker #停止 systemctl status docker #查看状态 systemctl enable docker #自启动 systemctl disable docker #禁止自启动 systemctl restart docker.service # 容器运行时操作防火墙出现以下,执行该命令 Error response from daemon: Failed to Setup IP tables: Unable to enable SKIP DNAT rule docker version # 显示docker的版本信息 docker info # 显示docker的系统信息,包括镜像和容器的数量
配置阿里云镜像加速 https://www.aliyun.com/ 支付宝登录,搜索 容器镜像服务
进入控制台
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://fxx0h34d.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
镜像官网查询地址 https://hub-stage.docker.com/
安装docker-compose https://github.com/docker/compose/releases
mkdir /apps && cd /apps
# 下载
wget https://github.com/docker/compose/releases/download/v2.18.0/docker-compose-linux-x86_64
# 添加可执行权限
chmod a+x docker-compose-linux-x86_64
mv docker-compose-linux-x86_64 /usr/bin/docker-compose
# 查看版本
docker-compose version
mkdir -p /data/gitlab
mkdir -p /docker/docker-compose/gitlab && cd /docker/docker-compose/gitlab
vi docker-compose.yml
docker-compose up -d
# 需要等待一段时间,访问:http://10.10.60.216:8929 ,用户名root,密码在 /etc/gitlab/initial_root_password
docker-compose logs -f
cat /data/gitlab/config/initial_root_password
Password: Cf4DW/zqQQA4LCgI0PQ5kC6BlHiBIi8Ui+HUz02++GQ=
docker-compose.yml
version: '3.1' services: gitlab: image: 'gitlab/gitlab-ce:15.0.3-ce.0' container_name: gitlab restart: always environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://10.10.60.216:8929' #自己安装git的服务器IP gitlab_rails['gitlab_shell_ssh_port'] = 2224 ports: - '8929:8929' - '2224:2224' volumes: - '/data/gitlab/config:/etc/gitlab' - '/data/gitlab/logs:/var/log/gitlab' - '/data/gitlab/data:/var/opt/gitlab'
重置密码:admin123456
jdk安装,https://www.oracle.com/cn/java/technologies/javase/javase8u211-later-archive-downloads.html
# 将安装包拷贝到 /apps 目录下
cd /apps
tar -zxvf jdk-8u351-linux-x64.tar.gz -C /usr/local
# 配置环境变量,在 /etc/profile 末尾加上
vi /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_351
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
# 使环境变量配置生效并查看java版本
source /etc/profile
java -version
maven安装,https://archive.apache.org/dist/maven/maven-3/
cd /apps
wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local
# 重命名
cd /usr/local && mv apache-maven-3.6.3 maven-3.6.3
# 配置环境变量,在 /etc/profile 末尾加上
vi /etc/profile
export MAVEN_HOME=/usr/local/maven-3.6.3
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
# 使环境变量配置生效并查看maven版本
source /etc/profile
mvn -v
修改maven配置文件 /usr/local/maven-3.6.3/conf/settings.xml
<!--本地仓库目录 --> <localRepository>/usr/local/mavenRep</localRepository> <!--添加阿里云镜像仓库 --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <!--添加jdk配置 --> <profile> <id>jdk1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> <activeProfiles> <activeProfile>jdk1.8</activeProfile> </activeProfiles>
mkdir -p /data/jenkins mkdir -p /docker/docker-compose/jenkins && cd /docker/docker-compose/jenkins vi docker-compose.yml docker-compose up -d # 查看日志,发现挂载的数据卷(/data/jenkins)没有权限 [root@bogon jenkins]# docker-compose logs -f jenkins | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied jenkins | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions? # 设置权限并重启 chmod -R 777 /data/jenkins docker-compose restart # 再次查看日志,可以发现一个登录密码 [root@bogon jenkins]# docker-compose logs -f jenkins | Jenkins initial setup is required. An admin user has been created and a password generated. jenkins | Please use the following password to proceed to installation: jenkins | jenkins | 01de227339b345bf934e8ce3be3afc3f
docker-compose.yml
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.363
container_name: jenkins
ports:
- 8080:8080
- 50000:50000
volumes:
- /data/jenkins/:/var/jenkins_home/
访问与配置 http://10.10.60.215:8080
Manage Jenkins–Manage Plugins-可选插件
Git Parameter
Publish Over SSH
# 1.找到jdk、maven的安装目录
[root@bogon local]# cd /usr/local && ls
bin etc games include jdk1.8.0_351 lib lib64 libexec maven-3.6.3 sbin share src
# 2.移动jdk、maven 到 Jenkins 的数据目录
[root@bogon local]# mv jdk1.8.0_351/ /data/jenkins/
[root@bogon local]# mv maven-3.6.3/ /data/jenkins/
# 修改maven本地仓库地址
<!--本地仓库目录 -->
<localRepository>/var/jenkins_home/mavenRep</localRepository>
配置:Manage Jenkins-Global Tool Configuration(全局配置)
配置:Manage Jenkins-Configure System
# 在远程服务器上创建 /jars 目录,用户后期的工作目录
mkdir -p /jars
在gitee上创建一个项目 https://gitee.com/mswang/spring-boot-jenkins.git
重要说明:项目构建后源码位置
/var/jenkins_home/workspace
对应宿主机位置/data/jenkins/workspace
pom.xml
<build> <finalName>spring-boot-jenkins</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Dockerfile
# 拉取一个jdk为1.8的docker镜像
FROM java:8
# 将当前目录下的 spring-boot-jenkins.jar 拷贝到镜像的/usr/local/目录下
COPY spring-boot-jenkins.jar /usr/local/
# 指定工作目录
WORKDIR /usr/local/
# 执行命令
CMD java -jar spring-boot-jenkins.jar
docker-compose.yml
version: "3.1"
services:
# 服务名称
spring-boot-jenkins:
# 生成镜像(当前目录下的Dockerfile文件)
build:
context: ./
dockerfile: Dockerfile
# 镜像名称
image: spring-boot-jenkins:v1.0
# 容器名称
container_name: spring-boot-jenkins
# 端口映射(容器的8888端口映射到宿主机的8868端口)
ports:
- 8868:8888
构建配置
cd /jars/boot-demo/docker
mv ../target/*.jar ./
docker-compose down
docker-compose up -d --build
docker image prune -f
结果:
[root@bogon boot-demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
spring-boot-jenkins v1.0 8db27a6d030d 54 seconds ago 685MB
[root@bogon boot-demo]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
387c907f72f5 spring-boot-jenkins:v1.0 "/bin/sh -c 'java -j…" About a minute ago Up About a minute 0.0.0.0:8868->8888/tcp, :::8868->8888/tcp
**现状:**经过前面配置,完成了(Jenkins打jar拷贝到部署服务器 => 部署服务器制作镜像 => 部署服务器执行docker-compose启动服务)
**升级:**Jenkins制作镜像推送镜像仓库 => Jenkins通知部署服务器拉取镜像 => 部署服务器执行本地脚本拉取镜像启动服务
cd /apps
wget https://github.com/goharbor/harbor/releases/download/v2.5.3/harbor-offline-installer-v2.5.3.tgz
tar xvf harbor-offline-installer-v2.5.3.tgz
cd harbor
# 拷贝一个配置文件
cp harbor.yml.tmpl harbor.yml
vi harbor.yml
# 安装运行
./install.sh
修改 harbor.yml
# 1.设置harbor所在主机IP hostname: 10.10.60.215 # 2.修改http访问的默认端口 # http related config http: # port for http, default is 80. If https enabled, this port will redirect to https port port: 8899 # 3.注释掉https访问的配置 # https related config # https: # https port for harbor, default is 443 # port: 443 # The path of cert and key files for nginx # certificate: /your/certificate/path # private_key: /your/private/key/path # 4.修改默认密码 harbor_admin_password: wms12345 # 5.修改镜像存储目录 data_volume: /data/harbor
登录验证: http://10.10.60.215:8899 admin/wms12345 新建一个项目,名称为:zcpro 访问级别为:公开。
# 在 /etc/docker/daemon.json 中配置私有镜像仓库地址
[root@bogon docker]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://fxx0h34d.mirror.aliyuncs.com"],
"insecure-registries": ["10.10.60.215:8899"]
}
[root@bogon docker]# systemctl daemon-reload
[root@bogon docker]# systemctl restart docker
将本地镜像spring-boot-jenkins:v1.0 推送到harbor
[root@bogon docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE spring-boot-jenkins v1.0 2faecdd2e79b 3 days ago 685MB # 1) 给本地镜像打tag,harbor地址/harbor项目名称/镜像名称:镜像版本 [root@bogon docker]# docker tag 2faecdd2e79b 10.10.60.215:8899/zcpro/spring-boot-jenkins:v1.0 [root@bogon docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.10.60.215:8899/zcpro/spring-boot-jenkins v1.0 2faecdd2e79b 3 days ago 685MB spring-boot-jenkins v1.0 2faecdd2e79b 3 days ago 685MB # 2) 推送镜像到harbor [root@bogon docker]# docker push 10.10.60.215:8899/zcpro/spring-boot-jenkins:v1.0 The push refers to repository [10.10.60.215:8899/zcpro/spring-boot-jenkins] 5f70bf18a086: Preparing 4a1003cf5063: Preparing 35c20f26d188: Preparing c3fe59dd9556: Preparing 6ed1a81ba5b6: Preparing a3483ce177ce: Waiting ce6c8756685b: Waiting 30339f20ced0: Waiting 0eb22bfb707d: Waiting a2ae92ffcd29: Waiting unauthorized: unauthorized to access repository: zcpro/spring-boot-jenkins, action: push: unauthorized to access repository: zcpro/spring-boot-jenkins, action: push # 3) 登录镜像仓库 [root@bogon harbor]# docker login -u admin -p wms12345 10.10.60.215:8899 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded # 4) 再次推送镜像到harbor [root@bogon harbor]# docker push 10.10.60.215:8899/zcpro/spring-boot-jenkins:v1.0 The push refers to repository [10.10.60.215:8899/zcpro/spring-boot-jenkins] 5f70bf18a086: Pushed 4a1003cf5063: Pushed 35c20f26d188: Pushed c3fe59dd9556: Pushed 6ed1a81ba5b6: Pushed a3483ce177ce: Pushed ce6c8756685b: Pushed 30339f20ced0: Pushed 0eb22bfb707d: Pushed a2ae92ffcd29: Pushed v1.0: digest: sha256:d43bca7f552c827620516e08d9f7a35e5902e4fa477751bf13b4f98428af74ab size: 2418 # 5) 获取镜像可以使用如下命令 [root@bogon harbor]# docker pull 10.10.60.215:8899/zcpro/spring-boot-jenkins
在Jenkins内部调用宿主机的docker,打包镜像并推送到harbor
[root@bogon run]# cd /var/run/
# 1) 修改docker.sock文件的所属组
[root@bogon run]# chown root:root docker.sock
# 2) 修改docker.sock读写权限
[root@bogon run]# chmod o+rw docker.sock
修改Jenkins的部署配置,添加宿主机docker的数据卷映射
cd /docker/docker-compose/jenkins vi docker-compose.yml docker-compose up -d # 进入docker内部,验证是否可以使用docker命令 [root@bogon jenkins]# docker exec -it jenkins bash jenkins@c557cfd5be91:/$ docker version Client: Docker Engine - Community Version: 23.0.6 API version: 1.42 Go version: go1.19.9 Git commit: ef23cbc Built: Fri May 5 21:21:29 2023 OS/Arch: linux/amd64 Context: default Server: Docker Engine - Community Engine: Version: 23.0.6 API version: 1.42 (minimum version 1.12) Go version: go1.19.9 Git commit: 9dbdbd4 Built: Fri May 5 21:20:38 2023 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.21 GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8 runc: Version: 1.1.7 GitCommit: v1.1.7-0-g860f061 docker-init: Version: 0.19.0 GitCommit: de40ad0
docker-compose.yml
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.363
container_name: jenkins
restart: always
ports:
- 8080:8080
- 50000:50000
volumes:
- /data/jenkins/:/var/jenkins_home/
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /etc/docker/daemon.json:/etc/docker/daemon.json
mv target/*.jar docker/
docker build -t boot-jenkins-harbor:$tag docker/
docker login -u admin -p wms12345 10.10.60.215:8899
docker tag boot-jenkins-harbor:$tag 10.10.60.215:8899/zcpro/boot-jenkins-harbor:$tag
docker push 10.10.60.215:8899/zcpro/boot-jenkins-harbor:$tag
docker image prune -f
# 查看当前部署服务器环境变量
[root@bogon jenkins]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 在/usr/bin目录下创建deploy.sh脚本,让其他用户可执行
[root@bogon jenkins]# cd /usr/bin
[root@bogon jenkins]# vi deploy.sh
[root@bogon jenkins]# chmod a+x deploy.sh
deploy.sh
harbor_addr=$1 harbor_pro=$2 project=$3 version=$4 port=$5 echo "容器运行时端口" container_port=$5 echo "宿主机映射端口" host_prot=$6 echo "镜像名称" imageName=$harbor_addr/$harbor_pro/$project:$version echo $imageName echo "正在运行的容器id" containerId=`docker ps -a | grep ${project} | awk '{print $1}'` echo $containerId echo "停止并删除容器进程" if [ "$containerId" != "" ] ; then docker stop $containerId docker rm $containerId fi echo "打印工程tag版本" tag=`docker images | grep ${project} | awk '{print $2}'` echo $tag echo "versin中包含tag版本,删除镜像" if [[ "$tag" =~ "$version" ]] ; then docker rmi $imageName fi echo "登录harbor仓库" docker login -u admin -p wms12345 $harbor_addr echo "拉取镜像" docker pull $imageName echo "删除none多余镜像" docker image prune -f echo "启动容器" docker run -d -p $host_prot:$container_port -name $project $imageName echo "success"
添加构建参数
构建后执行部署服务器脚本
deploy.sh 10.10.60.215:8899 zcpro ${JOB_NAME} $tag $container_port $host_prot
构建执行任务
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。