示例:通过makefile生成go二进制文件,将二进制文件拷贝到根目录下,打包成docker镜像
生产阶段Dockerfile:? Dockerfile-develop
发布节点Dockerfile:? Dockerfile-product
Makefile:编译出go二进制文件
基础镜像:alpine:3.12.0(或其他基础镜像)
最终镜像包:镜像名-tag.tar/镜像名-tag.tar.gz
makeimage.sh
set –xe
image_name=test
image_tag=v1.0
cleanup_make() # 清理
rm –rf ./*.tar ./*.tar.gz
make # 编译
docker build –no-cache=true –t ${image_name}-develop:${image_tag} –f Dockerfile-develop .
docker run –d –name ${image_name} ${image_name}:${image_tag}
mkdir release && cd release
docker cp ${image_name}:/ ./
rm –rf `find ./ -name ‘apk’`
tar cvf files.tar *
cd ../
docker build –t ${image_name}:${image_tag} –f Dockerfile-product .
docker save –o ${image_name}-${image_tag}.tar ${image_name}:${image_tag}
lz4 -9 ./${image_name}-${image_tag}.tar #压缩文件
# cleanup_make() # 清理
cleanup_make() {
docker ps –a | grep ${image_name} | awk ‘print $1’ | xargs –I {} docker rm {}
docker images | grep ${image_name} |awk ‘{print $1”:”$2}’ | xargs –I {} docker rmi {}
rm –rf release
}
生产阶段Dockerfile:? Dockerfile-develop
From alpine:3.12.0
ADD ./_output /
RUN rm -rf /var/lib/apk/* && \
rm -rf `find / -name 'apk'` \
chmod +x /test
发布节点Dockerfile:? Dockerfile-product
From scratch
ADD ./release/files.tar /
WORKDIR /
Makefile
all : _test
_test :
go build -ldflags '-s -w -extldflags "-static"' -o ./_output/test ./main.go
.PHONY : clean
clean :
rm -rf ./_output
|