docker二进制代码编译

docker二进制代码编译

2023年8月2日发(作者:)

docker⼆进制代码编译Contentsdocker如何编译,在 进⾏了介绍。其实很简单,就是在docker源码中有⼀个makefile⽂件,执⾏make,就可以进⾏编译了。我们从源码来看⼀下make的过程。⾸先看⼀下Makefile。...DOCKER_MOUNT := $(if $(BINDDIR),-v "$(CURDIR)/$(BINDDIR):/go/src//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/ build: bundlesdocker 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的⼆进制代码的编译了。另⼀种,就是使⽤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上。这种⽅式真⼼慢,没个半⼩时弄不出来(实测是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/ ` and `project/` have some limited documentation of some of theseDOCKER_ENVS := -e BUILDFLAGS -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 and avoid building every time# allow `make DOCKER_COMMAND=build` to build image from DockerfileDOCKER_COMMAND := pullDOCKER_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 hereBIND_DIR := $(if $(BINDDIR),$(BINDDIR),$(if $(DOCKER_HOST),,bundles))DOCKER_BUILD_MOUNT := $(if $(BIND_DIR),-v "$(CURDIR)/$(BIND_DIR):/go/src//docker/docker/$(BIND_DIR)")DOCKER_PULL_MOUNT := -v "$(CURDIR):/go/src//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 := 8000GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)# to allow you miss .git directoryDOCKER_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)endifDOCKER_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: binaryall: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ry: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ binarycross: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ binary cross $(DOCKER_RUN_DOCKER) hack/ binary crossdocs: docs-build $(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 "$(DOCKER_DOCS_IMAGE)" mkdocs servedocs-shell: docs-build $(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 "$(DOCKER_DOCS_IMAGE)" bashdocs-release: docs-build $(DOCKER_RUN_DOCS) -e OPTIONS -e BUILD_ROOT -e DISTRIBUTION_ID -v $(CURDIR)/docs/awsconfig:/docs/awsconfig "$(DOCKER_DOCS_IMAGE)" ./-test: docs-build $(DOCKER_RUN_DOCS) "$(DOCKER_DOCS_IMAGE)" ./: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ binary cross test-unit test-integration test-integration-cli test-docker-pytest-unit: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ test-unittest-integration: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ test-integrationtest-integration-cli: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ binary test-integration-clitest-docker-py: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ binary test-docker-pyvalidate: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) hack/ validate-gofmt validate-dco validate-tomlshell: $(DOCKER_COMMAND) $(DOCKER_RUN_DOCKER) bashpull: docker pull "$(DOCKER_IMAGE)"build: bundles docker build -t "$(DOCKER_IMAGE)" .docs-build: git fetch /docker/ docs && git diff --name-status 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)" docsbundles: mkdir bundles将Makefile拷贝到⾃⼰的docker⼯程中即可。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690920745a463867.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信