源码编译docker

编译环境

  • go 1.8.3
  • make
  • gcc
  • Protoc 3.x
1
2
wget -c https://github.com/google/protobuf/releases/download/v3.1.0/protoc-3.1.0-linux-x86_64.zip
unzip protoc-3.1.0-linux-x86_64.zip -d /usr/local

版本

COMPONENT COMMIT ID VERSION
dockerd 89658bed64c2a8fe05a978e5b87dbec409d57a0f v17.05.0-ce
containerd 9048e5e50717ea4497b757314bad98ea3763c145 v0.2.8
runc 9c2d8d184e5da67c95d601382adf14862e4f2228
libnetwork 7b2b1feb1de4817d522cc372af149ff48d25028e

编译全家桶

1
2
3
git clone https://github.com/moby/moby.git $GOPATH/src/github.com/docker/docker
cd docker $GOPATH/src/github.com/docker/docker
git checkout -q 89658bed64c2a8fe05a978e5b87dbec409d57a0f

moby项目提供了全家桶编译,需要在docker环境下以容器方式编译。这里需要注意两点:

  • 代理。需要连到外网。如有需要请设置代理。

    • docker代理

    • Dockerfile中代理。修改项目中根目录下的Dockerfile文件,添加代理的环境变量。

      1
      ENV http_proxy="http://yourproxy" https_proxy="http://yourproxy"
  • golang下载地址。Dockerfile中使用的是https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz,属于墙外地址,如不能翻墙,可以替换为golangtc的地址

    1
    RUN curl -fsSL "https://www.golangtc.com/static/go/${GO_VERSION}/go${GO_VERSION}.linux-amd64.tar.gz"

编译全家桶:

1
make binary DOCKER_BUILD_APT_MIRROR=mirrors.aliyun.com

这里使用了aliyun镜像来加速编译。

生成在bundles/binary-daemon/

1
2
3
4
5
6
7
docker-containerd
docker-containerd-ctr
docker-containerd-shim
dockerd
docker-init
docker-proxy
docker-runc

RPM

同样需要在以下文件中设置代理

1
2
3
4
Dockerfile
man/Dockerfile
contrib/builder/rpm/amd64/centos-7/Dockerfile
hack/make/build-rpm

以及在hack/make/build-rpm中的cat > "$DEST/$version/Dockerfile.build" <<-EOF位置上添加代理设置。

然后执行:

1
make rpm DOCKER_BUILD_PKGS=centos-7

编译结果在:

1
bundles/17.05.0-ce/build-rpm/centos-7/RPMS/x86_64/

安装

  1. cp $GOPATH/src/github.com/docker/docker/contrib/udev/80-docker.rules /etc/udev/rules.d/80-docker.rules
  2. 复制二进制到/usr/local/bin
  3. groupadd docker
  4. 运行dockerd启动。

分组件编译

下面介绍各个组件的分别构建方式。

docker组件间的版本要求很苛刻。在项目中一般都有写明需要其他组件的commit id。

比如,在moby中的hack/dockerfile/binaries-commits和containerd中的RUNC.md

cli

1
2
3
4
5
git clone https://github.com/docker/cli.git $GOPATH/src/github.com/docker/cli
cd $GOPATH/src/github.com/docker/cli

make binary
mv build/docker-linux-amd64 /usr/local/bin/docker

containerd

1
2
3
4
5
6
git clone https://github.com/containerd/containerd.git $GOPATH/src/github.com/containerd/containerd
cd $GOPATH/src/github.com/containerd/containerd
git checkout -q 9048e5e50717ea4497b757314bad98ea3763c145

make BUILDTAGS=no_btrfs
make install

dockerd编译需要btrfs头文件和库。这里禁用了btr。

生成二进制:

  • containerd
  • containerd-shim
  • containerd-stress
  • ctr

runc

runc的版本必须跟containerd中要求的一致。见https://github.com/containerd/containerd/blob/master/RUNC.md,其中要求:

RUNC_COMMIT = 9c2d8d184e5da67c95d601382adf14862e4f2228

1
2
3
4
5
6
7
8
# 安装libseccomp
yum install -y libseccomp-devel

git clone https://github.com/opencontainers/runc $GOPATH/src/github.com/opencontainers/runc
cd $GOPATH/src/github.com/opencontainers/runc
git checkout -q 9c2d8d184e5da67c95d601382adf14862e4f2228

make && make install

如果不希望启动seccomp,则

1
2
> make BUILDTAGS=''
>
显示 Gitment 评论