docker二进制代码编译

docker二进制代码编译流程

docker如何编译,在 官网 进行了介绍。

其实很简单,就是在docker源码中有一个makefile文件,执行make,就可以进行编译了。

我们从源码来看一下make的过程。

首先看一下Makefile。

...
DOCKER_MOUNT := $(if $(BINDDIR),-v "$(CURDIR)/$(BINDDIR):/go/src/github.com/docker/docker/$(BINDDIR)")
DOCKER_RUN_DOCKER := docker run --rm -it --privileged -e TIMEOUT -e BUILDFLAGS -e TESTFLAGS -e TESTDIRS -e DOCKER_GRAPHDRIVER -e DOCKER_EXECDRIVER $(DOCKER_MOUNT) "$(DOCKER_IMAGE)"
...
binary: build
   $(DOCKER_RUN_DOCKER) hack/make.sh binary
...
build: bundles
docker build -t "$(DOCKER_IMAGE)" .
...
bundles:
   mkdir bundles

执行make binary。会首先创建bundles文件夹,然后执行docker build,通过dockerfile创建镜像(这一步其实可以直接从官网pull镜像而不用自己build,下面详细讲)。然后将容器run起来,通过-v参数,将容器中的bundles同物理机的该路径下的bundles文件夹绑定起来,然后启动容器。容器执行了hack/dind命令,编译docker,将编译好的文件放在了bundles中。从而在物理机上也可见。

对于docker的二进制代码编译采取了一种极为dockerful的方式。因为搭建docker的编译环境可能会较为繁琐,docker直接提供给了一个 docker-dev 的镜像给用户。获得这个镜像有两种方式,一种是直接下载。用户只需要下载这个镜像,就可以在这个镜像中进行docker的二进制代码的编译了。另一种,就是使用Dockerfile进行进行build,来获取镜像。Dockerfile文件就在docker源码的根目录下。这种方式比较耗时,不推荐。

有了docker-dev镜像,其实不必每次都要docker build镜像,而只需要利用原有镜像就可以了。那么我修改了Makefile文件,从而可以支持能够使用docker-dev镜像进行编译。更新后的Makefile在末尾附带。

在我的里面支持了多种方式来灵活使用本地环境:

通过build方式
make DOCKER_COMMAND=build

通过pull方式(默认,pull与本版本相同的docker-dev镜像)
make

通过pull方式,指定本地镜像(不再pull镜像)
make DOCKER_IMAGE=docker-dev:v1.2

其他编译方法

另外一种方法,就是把你的代码上传到github上。

然后在 https://registry.hub.docker.com 上面,注册,选择自己的github项目,进行自动编译。

这种方式真心慢,没个半小时弄不出来(实测是20min),虽然是官方推荐。但是我感觉这更像是给他们自己做的一种硬广告。。。

Makefile

.PHONY: all binary build cross default docs docs-build docs-shell shell test test-unit test-integration test-integration-cli test-docker-py validate

# env vars passed through directly to Docker's build scripts
# to allow things like `make DOCKER_CLIENTONLY=1 binary` easily
# `docs/sources/contributing/devenvironment.md ` and `project/PACKAGERS.md` have some limited documentation of some of these
DOCKER_ENVS := \
   -e BUILDFLAGS \
   -e DOCKER_CLIENTONLY \
   -e DOCKER_EXECDRIVER \
   -e DOCKER_GRAPHDRIVER \
   -e TESTDIRS \
   -e TESTFLAGS \
   -e TIMEOUT
# note: we _cannot_ add "-e DOCKER_BUILDTAGS" here because even if it's unset in the shell, that would shadow the "ENV DOCKER_BUILDTAGS" set in our Dockerfile, which is very important for our official builds

# to allow pull docker-dev image from registry.hub.docker.com and avoid building every time
# allow `make DOCKER_COMMAND=build` to build image from Dockerfile
DOCKER_COMMAND := pull

DOCKER_VERSION := $(shell cat VERSION)

# to allow `make BIND_DIR=. shell` or `make BIND_DIR= test`
# (default to no bind mount if DOCKER_HOST is set)
# note: BINDDIR is supported for backwards-compatibility here
BIND_DIR := $(if $(BINDDIR),$(BINDDIR),$(if $(DOCKER_HOST),,bundles))

DOCKER_BUILD_MOUNT := $(if $(BIND_DIR),-v "$(CURDIR)/$(BIND_DIR):/go/src/github.com/docker/docker/$(BIND_DIR)")
DOCKER_PULL_MOUNT := -v "$(CURDIR):/go/src/github.com/docker/docker"

# to allow `make DOCSDIR=docs docs-shell` (to create a bind mount in docs)
DOCS_MOUNT := $(if $(DOCSDIR),-v $(CURDIR)/$(DOCSDIR):/$(DOCSDIR))

# to allow `make DOCSPORT=9000 docs`
DOCSPORT := 8000

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)

# to allow you miss .git directory
DOCKER_ENVS += $(if $(GIT_BRANCH),,-e DOCKER_GITCOMMIT=$(DOCKER_VERSION))

DOCKER_BUILD_IMAGE := docker$(if $(GIT_BRANCH),:$(GIT_BRANCH))
DOCKER_PULL_IMAGE := docker-dev:$(if $(DOCKER_VERSION),$(DOCKER_VERSION))

# to allow `make DOCKER_IMAGE=docker-dev:v1.2`(to use some other image if needed)
ifeq ($(DOCKER_COMMAND),pull)
   DOCKER_IMAGE := $(DOCKER_PULL_IMAGE)
   DOCKER_MOUNT := $(DOCKER_PULL_MOUNT)
else
   DOCKER_IMAGE := $(DOCKER_BUILD_IMAGE)
   DOCKER_MOUNT := $(DOCKER_BUILD_MOUNT)
endif

DOCKER_DOCS_IMAGE := docker-docs$(if $(GIT_BRANCH),:$(GIT_BRANCH))

DOCKER_RUN_DOCKER := docker run --rm -it --privileged $(DOCKER_ENVS) $(DOCKER_MOUNT) "$(DOCKER_IMAGE)"

DOCKER_RUN_DOCS := docker run --rm -it $(DOCS_MOUNT) -e AWS_S3_BUCKET -e NOCACHE

# for some docs workarounds (see below in "docs-build" target)
GITCOMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)

default: binary

all: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh

binary: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh binary

cross: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh binary cross

docs: docs-build
   $(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 "$(DOCKER_DOCS_IMAGE)" mkdocs serve

docs-shell: docs-build
   $(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 "$(DOCKER_DOCS_IMAGE)" bash

docs-release: docs-build
   $(DOCKER_RUN_DOCS) -e OPTIONS -e BUILD_ROOT -e DISTRIBUTION_ID \
      -v $(CURDIR)/docs/awsconfig:/docs/awsconfig \
      "$(DOCKER_DOCS_IMAGE)" ./release.sh

docs-test: docs-build
   $(DOCKER_RUN_DOCS) "$(DOCKER_DOCS_IMAGE)" ./test.sh

test: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh binary cross test-unit test-integration test-integration-cli test-docker-py

test-unit: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh test-unit

test-integration: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh test-integration

test-integration-cli: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh binary test-integration-cli

test-docker-py: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh binary test-docker-py

validate: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) hack/make.sh validate-gofmt validate-dco validate-toml

shell: $(DOCKER_COMMAND)
   $(DOCKER_RUN_DOCKER) bash

pull:
   docker pull "$(DOCKER_IMAGE)"

build: bundles
   docker build -t "$(DOCKER_IMAGE)" .

docs-build:
   git fetch https://github.com/docker/docker.git docs && git diff --name-status FETCH_HEAD...HEAD -- docs > docs/changed-files
   cp ./VERSION docs/VERSION
   echo "$(GIT_BRANCH)" > docs/GIT_BRANCH
#  echo "$(AWS_S3_BUCKET)" > docs/AWS_S3_BUCKET
   echo "$(GITCOMMIT)" > docs/GITCOMMIT
   docker build -t "$(DOCKER_DOCS_IMAGE)" docs

bundles:
   mkdir bundles

将Makefile拷贝到自己的docker工程中即可。



来自为知笔记(Wiz)



posted on 2015-03-03 16:25  晓论三国  阅读(1658)  评论(0编辑  收藏  举报