镜像仓库概述
镜像仓库用于存放 Docker 镜像,Docker 镜像用于部署容器服务,每个镜像有特定的唯一标识(镜像的 Registry 地址+镜像名称+镜像 Tag)。
接下来将演示如何配置一个简单的CICD
代码源授权
这里配置授权同步github代码源
点击github,将跳转github界面,在github中授权绑定即可。
创建一个私有仓库,用于保存镜像
根据指引先推送第一版的镜像到镜像仓库中,关于镜像大家可以用dockerhub中现成的,或自行构建的。
本章将通过dockerfile构建第一个基础镜像,这里是引用nginx的dockerfile 在这基础上新增了一句
RUN echo "Hello world 2019.5.23 11:46:37" > /usr/share/nginx/html/index.html
执行docker build 构建镜像
$ docker build -t lxinma/nginx-test:v1 .
Sending build context to Docker daemon 22.53kB
Step 1/10 : FROM debian:stretch-slim
stretch-slim: Pulling from library/debian
743f2d6c1f65: Pull complete
Digest: sha256:105032f50e173e95a84b86a5864ddc0c26c22ce3324b3dab98741601af79aaa4
Status: Downloaded newer image for debian:stretch-slim
---> 92d2f0789514
Step 2/10 : LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
---> Running in 395fbf7856cf
Removing intermediate container 395fbf7856cf
---> 55ce3bec803f
Step 3/10 : ENV NGINX_VERSION 1.15.12-1~stretch
---> Running in a8e63627e443
Removing intermediate container a8e63627e443
---> 042a4a274b46
Step 4/10 : ENV NJS_VERSION 1.15.12.0.3.1-1~stretch
---> Running in 33d920caa03c
Removing intermediate container 33d920caa03c
---> 94a2a9bc5bd4
Step 5/10 : RUN set -x && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 apt-transport-https ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION} nginx-module-xslt=${NGINX_VERSION} nginx-module-geoip=${NGINX_VERSION}nginx-module-image-filter=${NGINX_VERSION} nginx-module-njs=${NJS_VERSION} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; *) echo "deb-src https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | x
Step 7/10 : RUN echo "Hello world 2019.5.23 11:46:37" > /usr/share/nginx/html/index.html
---> Running in 7e7568c0cc72
Removing intermediate container 7e7568c0cc72
---> 37f1219c00ad
Step 8/10 : EXPOSE 80
---> Running in 1adebbd71fde
Removing intermediate container 1adebbd71fde
---> 3c4d06865eda
Step 9/10 : STOPSIGNAL SIGTERM
---> Running in f77a90fb4aaa
Removing intermediate container f77a90fb4aaa
---> f577bdb2f1d5
Step 10/10 : CMD ["nginx", "-g", "daemon off;"]
---> Running in b910f6bea551
Removing intermediate container b910f6bea551
---> 9ff0a8fd9e6d
Successfully built 9ff0a8fd9e6d
Successfully tagged lxinma/nginx-test:v1
查看镜像,并将为镜像打tag
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lxinma/nginx-test v1 9ff0a8fd9e6d 5 minutes ago 109MB
$ sudo docker tag 9ff0a8fd9e6d ccr.ccs.tencentyun.com/lxinma/nginx-cicd:v1
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ccr.ccs.tencentyun.com/lxinma/nginx-cicd v1 9ff0a8fd9e6d 5 minutes ago 109MB
lxinma/nginx-test v1 9ff0a8fd9e6d 5 minutes ago 109MB
推送至仓库
$ sudo docker push ccr.ccs.tencentyun.com/lxinma/nginx-cicd:v1
The push refers to repository [ccr.ccs.tencentyun.com/lxinma/nginx-cicd]
ad129c5939c6: Pushed
9bd24805e39a: Pushed
83425603da83: Pushed
6270adb5794c: Pushed
v1: digest: sha256:c3146c87dec50032446a0b684fcac2ae03f230a0721b2f6586135b13423b9ce2 size: 1155
在仓库中就能看到推送上来的镜像
选择刚才推送的镜像
创建后访问service的vip,即可看到nginx的内容
这里需要在github中先新建一个repo
然后在镜像仓库中选择对应的镜像,配置镜像构建
勾选代码源,对应仓库,触发方式
配置镜像版本命名规则等。
基础的配置后反回主界面,点击立即构建测试构建功能。
构建完成后可以看到有个构建成功的状态,此时返回镜像版本查看所有的镜像
通过基础的构建,生成了test版本
通过触发器,可以在每次生成新的Tag(镜像版本)时,自行执行动作,如:自动更新使用该镜像仓库的服务
配置触发器名称,触发条件,触发动作不能修改,只能更新容器的镜像。
接着配置对应的工作负载即第四步创建的deployment + service
[root@VM_3_5_centos nginx-cicd-test]# ls
dockerfile README.md
[root@VM_3_5_centos nginx-cicd-test]# git add dockerfile
[root@VM_3_5_centos nginx-cicd-test]# git commit -m "update dockerfile"
[master c94a8ba] update dockerfile
1 file changed, 1 insertion(+), 1 deletion(-)
[root@VM_3_5_centos nginx-cicd-test]# git push -u origin master
...
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 323 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/malingxin/nginx-cicd-test.git
cf651a5..c94a8ba master -> master
Branch master set up to track remote branch master from origin.
代码提交后,触发镜像构建规则,自动进入构建环节
镜像构建完成后,触发器产生触发动作,最终更新了nginx deployment
访问service的vip可看到内容已经改变,大体的cicd思路已展示完毕。
可参考官方文档:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。