ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Makefile + Dockerfile

2021-11-10 16:33:35  阅读:252  来源: 互联网

标签:target make Makefile build Linux docker Dockerfile


makefile 和 dockerfile 的关系:
makefile 是一个系统变量或者命令的集合,它引用dockerfile,或者读取默认的dockerfile,来完成自动按照流程 build的目的

从来没写过Makefile,见过的倒是不少,没有自己写过,还是不会。

有好多关于makefile的,但是我感觉都写的太过理论,昨天折腾了一天,还是不知怎么下手。早上看到这个文章,觉得可操作性很强。

使用Makefile构建Docker - Ryan.Miao - 博客园


为什么会有.PHONY

当我们设置的target和当前目录下的文件名一样的话,target会被忽略,所以,通常,我们把target都用做phony target。


Makefile

makefile的空格键是Tab,否则会报错


  1. .PHONY: build compile start push

  2. # 版本号

  3. VERSION_TAG = 1.7.0

  4. MILESTONE_TAG = Sep.2019

  5. REGISTRY = registry.local

  6. NAME = registry.local/xxx.cn/demo

  7. # 源码最后一次提交的commit id,使用commit id可以将docker镜像与提交的代码绑定在一起

  8. COMMIT_ID := $(shell git rev-parse HEAD)

  9. # 镜像构建的时间戳

  10. BUILD_TS := $(shell date +'%Y%m%d%H%M%S')

  11. # 源码分支

  12. BRANCH_TAG := $(shell git rev-parse --abbrev-ref HEAD)

  13. # 镜像版本信息

  14. VERSION := $(VERSION_TAG)-build-$(BRANCH_TAG)-$(BUILD_TS)

  15. # 镜像构建信息

  16. BUILD := $(VERSION_TAG)-$(MILESTONE_TAG)-build-$(BUILD_TS)-$(BRANCH_TAG)-$(COMMIT_ID)

  17. export GO111MODULE = on

  18. # 编译源码

  19. compile:

  20. go build -p 8 -o demo

  21. build: build-version

  22. # 默认读取当前目录下的Dockerfile。 --build-arg:给Dockerfile添加参数,BUILD=$(BUILD)中间不能有空格

  23. build-version:

  24. docker build --build-arg BUILD=$(BUILD) -t $(NAME):$(VERSION) . >/dev/null

  25. # docker image 打tag

  26. tag-latest:

  27. docker tag $(NAME):$(VERSION) $(NAME):latest >/dev/null

  28. # push 到镜像仓库

  29. push: build-version tag-latest

  30. docker push $(NAME):$(VERSION); docker push $(NAME):latest

  31. # 运行镜像

  32. start:

  33. docker run -it --rm $(NAME):$(VERSION) /bin/bash


Dockerfile


  1. FROM alpine:3.8

  2. MAINTAINER katy.xie@xxx.cn

  3. RUN mkdir /usr/bin/demo

  4. COPY ops /usr/bin/demo

  5. ARG BUILD

  6. LABEL VERSION $BUILD

  7. EXPOSE 8088

  8. ENTRYPOINT ["/usr/bin/demo", "--logtostderr"]

  9. CMD []


执行


  1. // 先编译源码,得到二进制文件

  2. make compile

  3. // 构建镜像:docker build

  4. make build

  5. // 提交到远程镜像仓库

  6. make push

  7. // 运行镜像

  8. make start


生成后的镜像长这样子


  1. // 查看刚刚生成的demo镜像(为啥这个样子,公司统一的规范)

  2. [root@localhost ~]# docker images|grep demo

  3. registry.local/xxx.cn/demo 1.7.0-build-master-20190928134143-6840f2a31b7a58af9d064c4c0fe1bf

使用Makefile构建Docker

刚开始学习docker命令的时候,很喜欢一个字一个字敲,因为这样会记住命令。后来熟悉了之后,每次想要做一些操作的时候就不得不
重复的输入以前的命令。当切换一个项目之后,又重复输入类似但又不完全相同的命令,仅仅通过history命令加速也有限。

于是想,把要用的命令写到shell里,然后调用shell脚本去做。刚开始确实是这样做的。比如https://github.com/Ryan-Miao/docker-yapi。
直到有一天,发现有人使用Makefile来存储操作,瞬间感觉很棒。

这里简单记录Makefile的简单用法。

Makefile是什么

Makefile是make命令的规则配置文件。make命令是什么?

先来看看make在哪里


~ > whereis make make: /usr/bin/make /usr/share/man/man1/make.1.gz

可以看到make是bin下的以可执行文件。 看看用户手册


MAKE(1) User Commands MAKE(1) NAME make - GNU make utility to maintain groups of programs SYNOPSIS make [OPTION]... [TARGET]... DESCRIPTION The make utility will determine automatically which pieces of a large program need to be recompiled, and issue the commands to recom‐ pile them. The manual describes the GNU implementation of make, which was written by Richard Stallman and Roland McGrath, and is cur‐ rently maintained by Paul Smith. Our examples show C programs, since they are very common, but you can use make with any programming language whose compiler can be run with a shell command. In fact, make is not limited to programs. You can use it to describe any task where some files must be updated automatically from others whenever the others change. To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program, and the states the commands for updating each file. In a program, typically the executable file is updated from object files, which are in turn made by compiling source files. Once a suitable makefile exists, each time you change some source files, this simple shell command: make suffices to perform all necessary recompilations. The make program uses the makefile description and the last-modification times of the files to decide which of the files need to be updated. For each of those files, it issues the commands recorded in the makefile. make executes commands in the makefile to update one or more target names, where name is typically a program. If no -f option is present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that order. Normally you should call your makefile either makefile or Makefile. (We recommend Makefile because it appears prominently near the beginning of a directory listing, right near other important files such as README.) The first name checked, GNUmakefile, is not recom‐ mended for most makefiles. You should use this name if you have a makefile that is specific to GNU make, and will not be understood by other versions of make. If makefile is '-', the standard input is read. make updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the target does not exist.

大致是说make是GNU中维护和组织程序的。比如我们的C语言编译, 再比如源码安装某些软件,比如nginx的时候。那么GNU是什么鬼?

GNU(GNU's Not Unix)是一个类Unix系统, 目标是创建一套完全自由的操作系统。在Linux出现之前,GNU已经完成了除了内核之外大部分的软件。Linux出现之后,与GNU结合变成GNU/Linux
严格的说,Linux只代表Linux内核,其他Linux软件称为Linux发行版。但由于商业发行商坚持称呼Linux, 虽然已经更名为GNU/Linux, 但大家依然叫Linux.


## 比如我的本机Ubuntu ~ ❯ uname Linux ~ ❯ uname -a Linux ryan-computer 4.18.0-20-generic #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux ## 大部分基于Debian的docker镜像 airflow@88e36c088b81:~$ cat /etc/issue Debian GNU/Linux 9 \n \l ## RedHat [root@data-docker001 docker-airflow]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@data-docker001 docker-airflow]# uname -a Linux data-docker001 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

make的基本用法就是


make target

Makefile基本语法

详细参见附录参考,这里为了减少认知成本,只罗列用到的知识点。

在当前目录创建一个叫做Makefile的文件。

声明变量

简单的变量赋值,比如声明name


name=ryan

声明规则Rule

Makefile文件由一系列规则(rules)构成。每条规则的形式如下。


<target> : <prerequisites> [tab] <commands>

  • target 目标
  • prerequisites 前置条件
  • tab command必须由tab隔开
  • commands 只能有一行的shell

防止target和文件名一样

当我们设置的target和当前目录下的文件名一样的话,target会被忽略,所以,通常,我们把target都用做phony target。


.PHONY: build start push

表示, build start push 这3个target,不检查当前目录下的文件,直接执行命令。

Docker构建用的指令

我常用的Makefile如下


NAME = ryan/airflow VERSION = 1.10.4 .PHONY: build start push build: build-version build-version: docker build -t ${NAME}:${VERSION} . tag-latest: docker tag ${NAME}:${VERSION} ${NAME}:latest start: docker run -it --rm ${NAME}:${VERSION} /bin/bash push: build-version tag-latest docker push ${NAME}:${VERSION}; docker push ${NAME}:latest

构建一个版本的镜像


make build

构建完毕,运行一下镜像,看看内容是否正确


make start

最后推送到docker仓库


make push

参考

标签:target,make,Makefile,build,Linux,docker,Dockerfile
来源: https://blog.csdn.net/yujia_666/article/details/121251081

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有