概述
环境为:Linux—ubuntu
1. 安装完毕docker以及docker-compose
2. 设置阿里镜像:进入该/etc/docker/daemon.json
如果没有deamon.json文件就创建一个,文件内容:
{ "registry-mirrors": ["https://legww7jr.mirror.aliyuncs.com"], "insecure-registries": ["192.168.4.134:5000"] }
其中这个地址"insecure-registries": ["192.168.4.134:5000"]
是我自己的镜像私服,可以修改掉或者不用。
3.安装gitlab
创建gitlab文件并在其内创建文件叫: docker-compose.yml
version: '3'
services:
web:
image: 'store/gitlab/gitlab-ce:11.10.4-ce.0'
restart: always
hostname: '192.168.4.134'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.4.134'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
unicorn['port'] = 8888
nginx['listen_port'] = 80
ports:
- '80:80'
- '8443:443'
- '2222:22'
volumes:
- /usr/local/docker/gitlab/config:/etc/gitlab
- /usr/local/docker/gitlab/data:/var/opt/gitlab
- /usr/local/docker/gitlab/logs:/var/log/gitlab
4、安装自己的私服nexus
创建nexus文件并在其内创建文件叫: docker-compose.yml
version: '3.1'
services:
nexus:
restart: always
image: sonatype/nexus3
container_name: nexus
ports:
- 8081:8081
volumes:
- /usr/local/docker/nexus/data:/nexus-data
5、安装自己的镜像仓库服务端和客户端
创建registry文件并在其内创建文件叫: docker-compose.yml
version: '3.1'
services:
registry:
image: registry
restart: always
container_name: registry
ports:
- 5000:5000
volumes:
- /usr/local/docker/registry/data:/var/lib/registry
frontend:
image: konradkleine/docker-registry-frontend:v2
ports:
- 8080:80
volumes:
- ./certs/frontend.crt:/etc/apache2/server.crt:ro
- ./certs/frontend.key:/etc/apache2/server.key:ro
environment:
- ENV_DOCKER_REGISTRY_HOST=192.168.4.134
- ENV_DOCKER_REGISTRY_PORT=5000
6、安装gitlab-runner实现CI/CD自动化
创建runner文件并在其内创建文件叫: docker-compose.yml
version: '3.1'
services:
gitlab-runner:
build: environment
restart: always
container_name: gitlab-runner
privileged: true
volumes:
- /usr/local/docker/runner/config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
在runner文件夹内创建一个文件夹叫:environment
在environment内需要有三个东西:daemon.json 和 Dockerfile 和 jdk-8u212-linux-x64.tar.gz
daemon.json的内容为:
{"registry-mirrors": ["https://legww7jr.mirror.aliyuncs.com/"]}
Dockerfile 的内容为:
FROM gitlab/gitlab-runner
MAINTAINER Lusifer <robin_time@163.com>
# 修改软件源
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list &&
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list &&
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list &&
echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list &&
apt-get update -y &&
apt-get clean
# 安装 Docker
RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common &&
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - &&
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" &&
apt-get update -y &&
apt-get install -y docker-ce
COPY daemon.json /etc/docker/daemon.json
# 安装 Docker Compose
WORKDIR /usr/local/bin
RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose
RUN chmod +x docker-compose
# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk-8u212-linux-x64.tar.gz /usr/local/java
RUN tar -zxvf jdk-8u212-linux-x64.tar.gz &&
rm -fr jdk-8u212-linux-x64.tar.gz
# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz
# COPY apache-maven-3.5.3-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.5.3-bin.tar.gz &&
rm -fr apache-maven-3.5.3-bin.tar.gz
# COPY settings.xml /usr/local/maven/apache-maven-3.5.3/conf/settings.xml
# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_212
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.5.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
WORKDIR /
jdk-8u212-linux-x64.tar.gz是JDK8的压缩包上传进来即可;
7、将gitlab上的项目注册到gitlab-runner上:
docker exec -it gitlab-runner gitlab-runner register
8:在项目代码springboot项目根目录下创建文件:.gitlab-ci.yml
stages:
- build
- push
- run
- clean
build:
stage: build
script:
- /usr/local/maven/apache-maven-3.5.3/bin/mvn clean package
- cp target/itoken-config-1.0.0-SNAPSHOT.jar docker
- cd docker
- docker build -t 192.168.4.134:5000/itoken-config .
push:
stage: push
script:
- docker push 192.168.4.134:5000/itoken-config
run:
stage: run
script:
- cd docker
- docker-compose down
- docker-compose up -d
clean:
stage: clean
script:
- docker rmi $(docker images -q -f dangling=true)
9:在项目代码springboot项目根目录下创建文件夹叫 docker
docker文件夹内创建2个文件叫:docker-compose.yml
和 Dockerfile
docker-compose.yml内容:
version: '3.1'
services:
itoken-comfig:
restart: always
image: 192.168.4.134:5000/itoken-config
container_name: itoken-comfig
ports:
- 8888:8888
Dockerfile内容为:
FROM openjdk:8-jre
MAINTAINER robin <robin_time@vip.qq.com>
ENV APP_VERSION 1.0.0-SNAPSHOT
RUN mkdir /app
COPY itoken-config-$APP_VERSION.jar /app/app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"]
EXPOSE 8888
效果图:
知识源于:千峰教育-李卫民老师。
我只是做个笔记方便自己以后回忆。
最后
以上就是眼睛大小懒猪为你收集整理的Docker实现springboot项目CI/CD持续集成笔记的全部内容,希望文章能够帮你解决Docker实现springboot项目CI/CD持续集成笔记所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复