目录

为什么会有.PHONY

Makefile

Dockerfile

执行

生成后的镜像长这样子

镜像详情 


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

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

https://www.cnblogs.com/woshimrf/p/make-docker.html

为什么会有.PHONY

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

 

Makefile

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

.PHONY: build compile start push

# 版本号
VERSION_TAG = 1.7.0
MILESTONE_TAG = Sep.2019
REGISTRY = registry.local
NAME = registry.local/xxx.cn/demo

# 源码最后一次提交的commit id,使用commit id可以将docker镜像与提交的代码绑定在一起
COMMIT_ID := $(shell git rev-parse HEAD)

# 镜像构建的时间戳
BUILD_TS := $(shell date +'%Y%m%d%H%M%S')

# 源码分支
BRANCH_TAG := $(shell git rev-parse --abbrev-ref HEAD)

# 镜像版本信息
VERSION := $(VERSION_TAG)-build-$(BRANCH_TAG)-$(BUILD_TS)

# 镜像构建信息
BUILD := $(VERSION_TAG)-$(MILESTONE_TAG)-build-$(BUILD_TS)-$(BRANCH_TAG)-$(COMMIT_ID)

export GO111MODULE = on

# 编译源码
compile:
	go build -p 8 -o demo

build: build-version

# 默认读取当前目录下的Dockerfile。 --build-arg:给Dockerfile添加参数,BUILD=$(BUILD)中间不能有空格
build-version:
	docker build --build-arg BUILD=$(BUILD) -t $(NAME):$(VERSION) . >/dev/null

# docker image 打tag
tag-latest:
	docker tag $(NAME):$(VERSION) $(NAME):latest >/dev/null

# push 到镜像仓库
push: build-version tag-latest
	docker push $(NAME):$(VERSION); docker push $(NAME):latest

# 运行镜像
start:
	docker run -it --rm $(NAME):$(VERSION) /bin/bash

Dockerfile

FROM alpine:3.8
MAINTAINER katy.xie@xxx.cn
RUN mkdir /usr/bin/demo
COPY ops /usr/bin/demo
ARG BUILD
LABEL VERSION $BUILD
EXPOSE 8088
ENTRYPOINT ["/usr/bin/demo", "--logtostderr"]
CMD []

执行

// 先编译源码,得到二进制文件
make compile 

// 构建镜像:docker build
make build

// 提交到远程镜像仓库
make push

// 运行镜像
make start

生成后的镜像长这样子

// 查看刚刚生成的demo镜像(为啥这个样子,公司统一的规范)
[root@localhost ~]# docker images|grep demo
registry.local/xxx.cn/demo             1.7.0-build-master-20190928134143-6840f2a31b7a58af9d064c4c0fe1bfbfbb69e91b   5b6a867af064        About an hour ago   11.8 MB

镜像详情 

[root@localhost ~]# docker inspect 5b6a867af064

[
    {
        "Id": "sha256:5b6a867af064fb5f44af87c7d1c539ffeb44e829dd3ec22209763e34edc5470a",
        "RepoTags": [
            "registry.local/xxx.cn/demo:1.7.0-build-master-20190928134143",
            "registry.local/xxx.cn/demo:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:15ab25270560e32243e0beba9f39cafe18e06772eb4ded9732cc3ebe588c8668",
        "Comment": "",
        "Created": "2019-09-28T05:41:45.122961442Z",
        "Container": "eeabdd61211ff10481f8c35d3d093a7568bf5206e885561c312017e274c27a1c",
        "ContainerConfig": {
            "Hostname": "915b7c04bc82",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD []"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:15ab25270560e32243e0beba9f39cafe18e06772eb4ded9732cc3ebe588c8668",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/usr/bin/demo",
                "--logtostderr"
            ],
            "OnBuild": [],
            "Labels": {
                "VERSION": "1.7.0-Sep.2019-build-20190928134143-master-6840f2a31b7a58af9d064c4c0fe1bfbfbb69e91b"
            }
        },
        "DockerVersion": "1.13.1",
        "Author": "katy.xie@xxx.cn",
        "Config": {
            "Hostname": "915b7c04bc82",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [],
            "ArgsEscaped": true,
            "Image": "sha256:15ab25270560e32243e0beba9f39cafe18e06772eb4ded9732cc3ebe588c8668",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/usr/bin/demo",
                "--logtostderr"
            ],
            "OnBuild": [],
            "Labels": {
                "VERSION": "1.7.0-Sep.2019-build-20190928134143-master-6840f2a31b7a58af9d064c4c0fe1bfbfbb69e91b"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 11798030,
        "VirtualSize": 11798030,
        "GraphDriver": {
            "Name": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0bb2174a4962553402b706c294b532917860dbd1f776cfb6b75483ed3e9ffe6c/diff:/var/lib/docker/overlay2/59fb938006fb49eceecfbf947bca229557b784cbc213bf732105d17a4010eea5/diff",
                "MergedDir": "/var/lib/docker/overlay2/838f040a569da4731049fda275b2afeed47eb3e6b43f7b7b95ccbb905bc73d48/merged",
                "UpperDir": "/var/lib/docker/overlay2/838f040a569da4731049fda275b2afeed47eb3e6b43f7b7b95ccbb905bc73d48/diff",
                "WorkDir": "/var/lib/docker/overlay2/838f040a569da4731049fda275b2afeed47eb3e6b43f7b7b95ccbb905bc73d48/work"
            }
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d9ff549177a94a413c425ffe14ae1cc0aa254bc9c7df781add08e7d2fba25d27",
                "sha256:da397fa5ffafadb72892d78edd0ba6f2108847bdb68a212b68a970b5d0c75024",
                "sha256:14b9893868f039d2d6543988af482cb8f6b49d623d8f85cc0ddb8bf7f40b4d38"
            ]
        }
    }
]

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐