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

参考

使用Makefile构建Docker的更多相关文章

  1. 用Dockerfile构建docker image

    dockerfile是为快速构建docker image而设计的,当你使用docker build 命令的时候,docker 会读取当前目录下的命名为Dockerfile(首字母大写)的纯文本文件并执 ...

  2. 使用Jenkins来构建Docker容器

    使用Jenkins来构建Docker容器(Ubuntu 14.04) 当开发更新了代码,提交到Gitlab上,然后由测试人员触发Jenkins,于是一个应用的新版本就被构建了.听起来貌似很简单,dua ...

  3. 多阶段构建Docker镜像

    在Docker 17.05及更高的版本中支持支持一种全新的构建镜像模式:多阶段构建: 多阶段构建Docker镜像的最大好处是使构建出来的镜像变得更小: 目前常见的两个构建镜像的方式为: 1.直接使用某 ...

  4. 使用Jenkins pipeline流水线构建docker镜像和发布

    新建一个pipeline job 选择Pipeline任务,然后进入配置页面. 对于Pipeline, Definition选择 "Pipeline script from SCM" ...

  5. 构建Docker Compose服务堆栈

    1.安装了docker-compose,现在我们要使用docker-compose来运行容器栈.这个地方会有两个容器,一个容器中使用Flask搭建的简单应用,另一个容器是Redis,Flash会向re ...

  6. Docker:使用Jenkins构建Docker镜像

    Docker  彭东稳  1年前 (2016-12-27)  10709次浏览  已收录  0个评论 一.介绍Jenkins Jenkins是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从 ...

  7. 打包应用和构建Docker镜像(docker在windows上)

    在构建Docker时编译应用 一般有两种方法在构建镜像时进行打包应用.第一种方法就是使用基本的镜像,该镜像包括应用平台和构建工具,因此在Dockerfile中,复制源代码到镜像中并在构建镜像时编译ap ...

  8. uboot makefile构建分析-续

    前言 这篇博文是 uboot makefile构建分析的续篇,继续分析uboot构建u-boot.bin的过程 构建u-boot.bin过程分析 makefile一开始,就是确定链接脚本.在构建ubo ...

  9. 构建Docker镜像两种方式的比较-Dockerfile方式和S2I方式

    前言 写Dockerfile是构建Docker镜像最通常的方式,接触过Docker的童鞋多少了解一些.前段时间研究OpenShift(paas的一种),发现了另外一种构建Docker镜像的方式:S2I ...

随机推荐

  1. Python登录豆瓣并爬取影评

    上一篇我们讲过Cookie相关的知识,了解到Cookie是为了交互式web而诞生的,它主要用于以下三个方面: 会话状态管理(如用户登录状态.购物车.游戏分数或其它需要记录的信息) 个性化设置(如用户自 ...

  2. 一个简单的JS倒计时

    看到很多商城都是抢购倒计时的功能,今天闲来无事做了个倒计时.全当学习JS. 主要思路:主要用到Date对象,声明一个变量获取当前时间,在声明一个变量获取结束时间,结束时间-当前时间=剩余时间(倒计时) ...

  3. 使用Kubeadm创建k8s集群之部署规划(三十)

    前言 上一篇我们讲述了使用Kubectl管理k8s集群,那么接下来,我们将使用kubeadm来启动k8s集群. 部署k8s集群存在一定的挑战,尤其是部署高可用的k8s集群更是颇为复杂(后续会讲).因此 ...

  4. 《C# 语言学习笔记》——目录

    C# 简介 变量和表达式 流程控制 3.1 布尔逻辑 3.2 goto语句 3.3 分支 3.4 循环 变量的更多内容 4.1 类型转换 4.2 复杂的变量类型 4.3 字符串的处理 函数 5.1 定 ...

  5. linux初学者-延迟及定时任务篇

    linux初学者-延迟及定时任务篇 在linux系统的学习工作中,南面会遇到需要延迟进行的任务和需要定时去完成的任务,就像手机的闹钟一样,这时候就需要用到linux系统当中的系统延迟和定时任务的设置了 ...

  6. java练习---2

    //程序员:罗元昊 2017.9.6public class My{       public static void main(String[] args){              int a= ...

  7. Linux设备驱动程序学习----2.内核模块与应用程序的对比

    内核模块与应用程序的对比 更多内容请参考Linux设备驱动程序学习----目录 1. 内核模块与应用程序的对比 内核模块和应用程序之间的不同之处: 大多数中小规模的应用程序是从头到尾执行单个任务,而模 ...

  8. Django websocket之web端实时查看日志实践案例

    这是Django Channels系列文章的第二篇,以web端实现tailf的案例讲解Channels的具体使用以及跟Celery的结合 通过上一篇<Django使用Channels实现WebS ...

  9. spring autowrited注解

    @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法.在使用@Autowired之前,我们对一个b ...

  10. cmd与monkey测试

    monkey测试的相关命令 monkey是模拟用户触摸操作,不支持条件判断.monkey命令格式:  启动安卓模拟器/真机 点击运行->输入cmd->进入命令行界面 查看设备连接情况,ad ...