Contentsgit
docker如何編譯,在 官網 進行了介紹。github
其實很簡單,就是在docker源碼中有一個makefile文件,執行make,就能夠進行編譯了。web
咱們從源碼來看一下make的過程。docker
首先看一下Makefile。shell
...
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中。從而在物理機上也可見。bash
對於docker的二進制代碼編譯採起了一種極爲dockerful的方式。由於搭建docker的編譯環境可能會較爲繁瑣,docker直接提供給了一個 docker-dev 的鏡像給用戶。得到這個鏡像有兩種方式,一種是直接下載。用戶只須要下載這個鏡像,就能夠在這個鏡像中進行docker的二進制代碼的編譯了。另外一種,就是使用Dockerfile進行進行build,來獲取鏡像。Dockerfile文件就在docker源碼的根目錄下。這種方式比較耗時,不推薦。fetch
有了docker-dev鏡像,其實沒必要每次都要docker build鏡像,而只須要利用原有鏡像就能夠了。那麼我修改了Makefile文件,從而能夠支持可以使用docker-dev鏡像進行編譯。更新後的Makefile在末尾附帶。ui
在個人裏面支持了多種方式來靈活使用本地環境:url
經過build方式
make DOCKER_COMMAND=build
經過pull方式(默認,pull與本版本相同的docker-dev鏡像)
make
經過pull方式,指定本地鏡像(再也不pull鏡像)
make DOCKER_IMAGE=docker-dev:v1.2
另一種方法,就是把你的代碼上傳到github上。spa
而後在 https://registry.hub.docker.com 上面,註冊,選擇本身的github項目,進行自動編譯。
這種方式真心慢,沒個半小時弄不出來(實測是20min),雖然是官方推薦。可是我感受這更像是給他們本身作的一種硬廣告。。。
.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工程中便可。