使用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