docker二进制代码编译

Contents

  • docker二进制代码编译流程
  • 其他编译方法
  • Makefile

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)

时间: 2024-07-29 10:46:27

docker二进制代码编译的相关文章

docker下编译mangoszero WOW60级服务端(一)

这几天看到暴雪准备开放怀旧服的新闻,突然想到几年前用大芒果window一键服务端自己搭建过服务,就想着在Linux环境下重新编译一套,毕竟Linux作为服务端,性能和稳定性都会高一些,于是在mac虚拟机中安了个centos7,按照官方文档搞了一套. 虚拟中搭建完成之后,想着不如在docker中做几个镜像,到时一键启动就可以搭建完成一套服务端,多么轻松. 经过几天的努力,完成了60级镜像和70级镜像的制作,由于镜像有些大,目前只把60级镜像推送到了阿里云,有兴趣的朋友可以pull下来玩玩,当然客户

docker下编译mangoszero WOW60级服务端(二)

开始搭建基于docker的mangoszero WOW服务端,我自己的操作系统是mac os,其他平台操作可以等价替换 1.准备工作 (1) 安装docker,参考docker官方文档,https://www.docker.com,有各种平台的安装教程,mac下可以直接 brew install caskroom/cask/docker (2) 拉取docker官方centos镜像,可使用阿里云hub,https://dev.aliyun.com/detail.html?spm=5176.197

【docker】编译MongoDB镜像

1.新建一个Dockerfile文件 Dockerfile 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # VERSION 0.0.1 FROM ubuntu:latest MAINTAINER lanhong Turnbull "[email protected]" # Add 10gen official apt source to the sources list RUN apt-key adv --keyserver hkp://keyserv

Docker环境编译时的两个错误记录

1)报错一docker-compose -f compose/app.yaml -f compose/backend.yaml -f compose/proxy.yaml build peatio barongERROR: Couldn't connect to Docker daemon at http://localhost:4243 - is it running? If it's at a non-standard location, specify the URL with the D

使用Docker容器来源码编译etcd

背景 etcd是CoreOS公司开发的分布式键值对存储库.在Kubernetes中,我们需要使用etcd作为所有REST API对象的持久化存储. 不幸的是,在github的release中,CoreOS将etcd的二进制可执行文件都放在了亚马逊的S3存储上,在国内访问非常慢.因此,我们只能通过源码编译etcd. 过程 1. 下载etcd源码. $ git clone https://github.com/coreos/etcd.git $ cd etcd 2. 根据实际情况,选择合适的版本.如

docker 1.8.2 源代码编译

编译docker的必要条件 这阵子在公司搞docker container这些技术,docker编译网上查了一下木有靠谱的.只好自己动手丰衣足食了. 声明:你编译docker不需要git pull它的源码,必备条件只有一个,就是你有一台能pull镜像的docker主机就行了. 本人环境是VMware下CentOS Linux release 7.1.1503 (Core) [x86_64] 以下为编译过程的具体记录 1.pull docker-dev:1.8.2的镜像 [[email prote

centos 6 编译 docker

docker centos 编译 下载最新源代码,至目录:GOPATH/src/github.com/docker cd GOPATH/src/github.com/docker/docker go get -v ... 下载golang.org/x/net/websocket失败 cd GOPATH/src/ mkdir -p golang.org/x/net/ cd golang.org/x/net git clone https://github.com/golang/net.git ./

将Spring-boot应用部署到Docker容器

1:Docker中设置阿里云加速 使用阿里云的加速器,因为在使用docker的时候,会需要从docker的网站下载镜像文件,下载速度可能会很慢.获得阿里云加速,需要登录阿里云开发者平台,然后点击右侧的管理中心: 阿里云开发者平台:https://dev.aliyun.com/search.html 注册开通服务后,会分配一个加速地址. 参考阿里云管理端进行Docker加速配置. 2:Spring-boot 应用程序打包部署 (1)gradle build –x test打包Spring-boot

ZKUI中文编码以及以docker方式运行的问题

*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0; } /* HEAD