下面是从源代码编译最新(2018-03-27)的 docker/cli 和 moby 的详细过程
第1步 安装 ubuntu 16.04
下载最新的 ubuntu-16.04.4-desktop-amd64.iso
安装后
# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
# uname -a
Linux vbcomputer 4.13.0-36-generic #40~16.04.1-Ubuntu SMP Fri Feb 16 23:25:58 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
NOTE: 本文档所有命令都使用 root 账户操作
第2步 更改 apt 安装源
# cat /etc/apt/sources.list
#deb包
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
第3步 安装 go 语言
https://studygolang.com/dl
下载
https://studygolang.com/dl/golang/go1.9.4.linux-amd64.tar.gz
NOTE: 必须是 go 1.9.4 , 不能用 go 1.10
tar xzvf go1.9.4.linux-amd64.tar.gz -C /usr/local/go
设置环境变量
/etc/bash.bashrc 最后增加
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
/root/.bashrc 最后增加
export GOPATH=/gospace
export PATH=${GOPATH}/bin:${PATH}
第4步 安装 docker
https://mirrors.aliyun.com/docker-ce/
下载
https://mirrors.aliyun.com/docker-ce/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_17.12.1~ce-0~ubuntu_amd64.deb
# dpkg -i docker-ce_17.12.1~ce-0~ubuntu_amd64.deb
# docker version
Client:
Version:
17.12.1-ce
API version:
1.35
Go version:
go1.9.4
Git commit:
7390fc6
Built:
Tue Feb 27 22:17:40 2018
OS/Arch:
linux/amd64
Server:
Engine:
Version:
17.12.1-ce
API version:
1.35 (minimum version 1.12)
Go version:
go1.9.4
Git commit:
7390fc6
Built:
Tue Feb 27 22:16:13 2018
OS/Arch:
linux/amd64
Experimental:
false
第5步 更新 docker hub 源
mv /etc/docker/daemon.json /etc/docker/daemon.json_orig
将新的源写入 /etc/docker/daemon.json
效果如下
# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
第6步 编译 docker/cli
下载 https://github.com/docker/cli.git
# git clone https://github.com/docker/cli.git /gospace/src/github.com/docker/cli
NOTE: docker/cli 的代码, 必须 git clone 到 ${GOPATH}/src/github.com/docker/cli/ 路径下面, 其它路径都不行.
# cd /gospace/src/github.com/docker/cli
# make binary --> 执行编译
编译成功后, 生成 build/docker
在路径 ${GOPATH}/src/github.com/docker/cli/build/docker
第7步 下载 https://github.com/moby/moby.git
# git clone https://github.com/moby/moby.git /gospace/src/github.com/docker/docker
NOTE: moby 的源代码, 必须 git clone 到 ${GOPATH}/src/github.com/docker/docker 目录, 并且目录名叫做 docker, 而不是 moby
第8步 第一次修改 /gospace/src/github.com/docker/docker/Dockerfile
# cd /gospace/src/github.com/docker/docker
修改 Dockerfile
将 debian 源 改为 163的源
在
ARG APT_MIRROR=deb.debian.org
RUN sed -ri "s/(httpredir|deb).debian.org/$APT_MIRROR/g" /etc/apt/sources.list
行后增加如下这行
RUN rm /etc/apt/sources.list && echo 'deb http://mirrors.163.com/debian/ stretch main' >> /etc/apt/sources.list && echo 'deb http://mirrors.163.com/debian/ stretch-updates main' >> /etc/apt/sources.list && echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main' >> /etc/apt/sources.list
第9步 第二次修改 /gospace/src/github.com/docker/docker/Dockerfile
将 FROM base AS frozen-images 下面的 RUN 命令删除
也就是删除
RUN /download-frozen-image-v2.sh /docker-frozen-images \
buildpack-deps:jessie@sha256:dd86dced7c9cd2a724e779730f0a53f93b7ef42228d4344b25ce9a42a1486251 \
busybox:latest@sha256:bbc3a03235220b170ba48a157dd097dd1379299370e1ed99ce976df0355d24f0 \
busybox:glibc@sha256:0b55a30394294ab23b9afd58fab94e61a923f5834fba7ddbae7f8e0c11ba85e6 \
debian:jessie@sha256:287a20c5f73087ab406e6b364833e3fb7b3ae63ca0eb3486555dc27ed32c6e60 \
hello-world:latest@sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
并删除
COPY --from=frozen-images /docker-frozen-images /docker-frozen-images
这句
/docker-frozen-images 做测试用, 如果只是编译binary 可以暂时不管
第10步 第三次修改 /gospace/src/github.com/docker/docker/Dockerfile
将 && pip install -r test-requirements.txt 这句
改为 && pip install -r test-requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
这样改以后, 一般来说, make build 会成功
如果这样改了以后, make build 在这句不成功
则删除
RUN cd /docker-py \
&& pip install docker-pycreds==0.2.1 \
&& pip install -r test-requirements.txt
这一整句
/docker-py 做测试用, 如果只是编译binary 可以暂时不管
第11步 修改 /gospace/src/github.com/docker/docker/hack/dockerfile/install/dockercli.installer
将
url=https://download.docker.com/linux/static
改为
url=https://mirrors.aliyun.com/docker-ce/linux/static
第12步 先编译 docker container
# cd /gospace/src/github.com/docker/docker
# make build
如果成功的话
可以看到
# docker images
docker-dev master a74b5338e172 About an hour ago 1.67GB
第13步 再在容器中编译 moby
# cd /gospace/src/github.com/docker/docker
# make binary
编译成功的话, 可以看到
root:/gospace/src/github.com/docker/docker/bundles
# tree .
.
├── binary-daemon
│ ├── docker-containerd
│ ├── docker-containerd-ctr
│ ├── docker-containerd-ctr.md5
│ ├── docker-containerd-ctr.sha256
│ ├── docker-containerd.md5
│ ├── docker-containerd.sha256
│ ├── docker-containerd-shim
│ ├── docker-containerd-shim.md5
│ ├── docker-containerd-shim.sha256
│ ├── dockerd -> dockerd-dev
│ ├── dockerd-dev
│ ├── dockerd-dev.md5
│ ├── dockerd-dev.sha256
│ ├── docker-init
│ ├── docker-init.md5
│ ├── docker-init.sha256
│ ├── docker-proxy
│ ├── docker-proxy.md5
│ ├── docker-proxy.sha256
│ ├── docker-runc
│ ├── docker-runc.md5
│ └── docker-runc.sha256
└── latest -> .
2 directories, 22 files