rancher 2.0 源码编译(二)-- 容器打包

一、  背景介绍

         上篇博客,我们了解了 如何通过源码编译 rancher 的二进制文件,

         本文介绍如何将编译好的二进制文件打包成 rancher 镜像,以便部署。

         编译环境:在上文基础上,安装 Docker,我使用的版本是 17.12.0-ce,安装教程见官网

二、  基本步骤

2.1    修改源码

        a.  修改 Dockerfile,在 FROM 语句之后增加代理配置,下载相关文件需要,国内网络你懂的

# vim $GOPATH/src/github.com/rancher/rancher/package/Dockerfile
FROM ubuntu:17.10

ENV http_proxy=http://192.168.4.62:9666
ENV https_proxy=http://192.168.4.62:9666

...
# vim $GOPATH/src/github.com/rancher/rancher/package/Dockerfile.agent
FROM rancher/k8s:v1.8.7-rancher1-1 as k8s
FROM rancher/rke-service-sidekick:v0.1.0 as sidekick

FROM ubuntu:17.10

ENV http_proxy=http://192.168.4.62:9666
ENV https_proxy=http://192.168.4.62:9666

...

        b.  修改版本生成脚本

             由于我们修改了相关文件,Git 状态将是 Dirty 的,我们修改脚本,屏蔽错误的版本生成

# vim $GOPATH/src/github.com/rancher/rancher/scripts/version
#!/bin/bash

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    DIRTY="-dirty"
fi

COMMIT=$(git rev-parse --short HEAD)
GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)}

if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
    VERSION=$GIT_TAG
else
    VERSION=$GIT_TAG
fi

if [ -z "$ARCH" ]; then
    ARCH=amd64
fi

        c.  修改打包脚本

             默认注释了对 agent 镜像的编译,我们将它打开

# vim $GOPATH/src/github.com/rancher/rancher/scripts/package
#!/bin/bash
set -e

source $(dirname $0)/version

ARCH=${ARCH:-"amd64"}
SUFFIX=""
[ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}"

cd $(dirname $0)/../package

TAG=${TAG:-${VERSION}${SUFFIX}}
REPO=${REPO:-rancher}

if echo $TAG | grep -q dirty; then
    TAG=dev
fi

cp ../bin/rancher ../bin/agent .

IMAGE=${REPO}/server:${TAG}
AGENT_IMAGE=${REPO}/agent:${TAG}
docker build --build-arg VERSION=${TAG} -t ${IMAGE} .
docker build --build-arg VERSION=${TAG} -t ${AGENT_IMAGE} -f Dockerfile.agent .
echo ${IMAGE} > ../dist/images
#echo ${AGENT_IMAGE} > ../dist/images
echo Built ${IMAGE} #${AGENT_IMAGE}

2.2    执行编译

# cd $GOPATH/src/github.com/rancher/rancher/
# ./scripts/package

2.3    检查结果

# docker images





你可能感兴趣的:(容器技术)