Docker Smart Use

这里主要记录一下docker container 一些有意思的用法。

Version Release

可以将源码封装到 docker(tag) 或者 helm chart 中,通过check-in pre/post-CI/CD pipeline 发布,然后在 K8s or Jenkins pipeline 中使用,这样整个流程就非常规范,易于管理。

Run in Background

之前一直通过更改--entrypoint/bin/sh 加上 -c "tail -f /dev/null"的方式,使容器在后台running,其实可以默认使用 -d(detach) 的方式,但必须了解entrypoint/cmd的设置,比如:

1
2
3
4
5
6
7
# the busybox build with CMD ["sh"], here it run as default sh
# and with -it, this container hang in background
docker run -itd busybox

# if image has entrypoint different than sh or bash, to make it idle
# we have to reset the entrypoint
docker run -itd --entrypoint=/bin/sh <image>:<tag>

Wrap Dependencies

把一些需要依赖的tool build到docker image中,然后运行docker container去工作,把需要处理的资源mount到container中: 比如 helm linter cv:

1
2
3
docker run --rm -it \
-v pathto/chart:/chart \
cv:latest cv lint helm /chart

Logs may also be captured by passing an additional volume: -v ~/path/to/logs:/tmp/cv

再比如jsonnet,搭建本地运行环境比较困难:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# check jsonnet help
docker run --rm -it bitnami/jsonnet:latest --help

# convert Chart.yaml.jsonnet to Chart.yaml
# -V: pass parameters to chart.yaml.jsonnet
# -S: plain text display
# sed -e 's/"//g': remove double quotes, but need to quote the version number

# the entrypoint is jsonnet cli
docker run \
--rm \
--user root \
-v pathto/Chart.yaml.jsonnet:/Chart.yaml.jsonnet \
bitnami/jsonnet:latest /Chart.yaml.jsonnet -V version="1.0.0" -S | sed -e 's/"//g'
0%