docker 发布方式尝试

docker 发布方式尝试

目前有个小项目, 尝试用docker的方式来发布, 项目只有一个节点, 使用 kubenate 有点小题大做, 所以采用docker-compose来发布.

发布过程

GIT --> build --> publish

发布中的注意点

GIT 版本号(tag/commit)

  1. 从git命令生成
# git tag
GIT_TAG=`git describe --tags`
IFS='-' read -r -a tags <<< $GIT_TAG
if [ "${#tags[@]}" = "1" ]; then
    GIT_COMMIT=$tags
else
    GIT_COMMIT=`git rev-parse HEAD | cut -c 1-8`
fi
  1. 通过 docker build 的 --build-arg 参数传入
docker build -f /path/to/Dockerfile -t app_name --build-arg VERSION=${GIT_COMMIT} .
  1. dockerfile 中可以得到此变量 VERSION
ARG VERSION=no-version  # 如果没有赋值, 默认值是 no-version
  1. build image 的时候, 可以将此变量注入到前端页面上

build 和 publish 的 image 区别

通过 docker 发布应用的过程, 一般需要2个image.
一个用来build应用, 一个用来发布应用

比如, 发布一个前端工程, build时需要 nodejs 的环境, 但是发布后只要 nginx 环境即可. 这时, Dockerfile 可以这样写:

FROM node:10-alpine as builder

WORKDIR /build
ADD ./myapp .
RUN yarn
RUN yarn build

FROM nginx:1

COPY --from=builder /build/build /app/static
RUN gzip -r /app/static
RUN touch /app/static/index.html

ADD /path/to/my-nginx.conf /etc/nginx/nginx.conf

这样, 可以让发布的镜像尽可能的小.

image build之后的清理

每次image发布之后, 都会残留一些过程中产生的image, 可以在build完成之后, 通过如下命令清理:

docker image prune -f

docker-compose 环境变量配置

对于简单的应用, image 发布之后, 可以通过 docker-compose 来启动看看发布的效果.

docker-compose.yml 同级目录下新建 .env 文件, 可以控制docker-compose启动中的环境变量.

.env 示例:

COMPOSE_PROJECT_NAME=app-name
SERVER_PORT=8080
STAGE=dev

docker-compose.yml 示例:

version: '3'
services:
  myapp:
    image: myapp:latest
    # restart: always
    ports:
      - '8080:8080'
    environment:
      SERVER_PORT: $SERVER_PORT
      STAGE: $STAGE

发布脚本示例

#!/bin/bash
#set -x
#******************************************************************************
# @file    : dev.deploy.sh
# @author  : wangyubin
# @date    : 2018-11-15  9:24:11
#
# @brief   : auto build deploy dev version
# history  : init
#******************************************************************************

function usage() {
    echo "dev.deploy.sh [-a/-v/-h]"
    echo "OPTIONS: "
    echo "         -a: app name"
    echo "         -v: docker image version (v2.1.1, v2.2-test)"
    echo "         -h: this help infomation"
    echo ""
    echo "SAMPLES:"
    echo "         dev.deploy.sh -h"
    echo "         dev.deploy.sh -a myapp -v v2.1.2"
}

declare APP
declare IMAGE_VERSION
declare GIT_COMMIT

while getopts "a:v:h" arg
do
    case $arg in
        a)
            APP=$OPTARG
            ;;
        v)
            IMAGE_VERSION=$OPTARG
            ;;
        h)
            usage
            exit 0
            ;;
    esac
done

# image version
if [ "$IMAGE_VERSION" = "" ]; then
    IMAGE_VERSION=latest
fi

# git tag
GIT_TAG=`git describe --tags`
IFS='-' read -r -a tags <<< $GIT_TAG
if [ "${#tags[@]}" = "1" ]; then
    GIT_COMMIT=$tags
else
    GIT_COMMIT=`git rev-parse HEAD | cut -c 1-8`
fi

# kill docker compose
docker-compose down

# start build images
echo "--------------------------------"
echo "APP:          $APP"
echo "VERSION:      $IMAGE_VERSION"
echo "CODE_VERSION: $GIT_COMMIT"
echo "--------------------------------"

docker build -f /path/to/Dockerfile -t ${APP}:${IMAGE_VERSION} --build-arg VERSION=${GIT_COMMIT} .
docker image prune -f
sleep 2

# recreate docker compose
docker-compose up -d
posted @ 2019-02-19 13:17  wang_yb  阅读(849)  评论(0编辑  收藏  举报