Because work needs to compile docker from source code, this article will introduce.

I. Environment construction

Docker compilation, docker software needs to be pre-installed in the host. Because when compiling docker source code, a Docker image will be built and run, and build operation will be carried out in this container. Since the container already includes the GO locale, there is no need for the host to install Golang. Ubuntu 16.04 64bit Docker version

Docker -v Docker version 17.10.0-CE, build F4FFd25Copy the code

Download the source code

Docker’s github website is github.com/docker/dock… . Docker is being developed at a pace of one release per month. The naming rules are as follows: year-month-ce, where CE indicates the community version. As of this writing, the latest version is V17.12.0-CE, but the next version, V18.01.00-CE-dev, is already under development (dev indicates development), and the compiled version of this article is V18.01.00-CE-dev. Download the release at github.com/docker/dock… . Note This document is performed in the /home/latele/docker-dev directory. Modify the directory as required. Download source code:

git clone https://github.com/docker/docker-ce
Copy the code

Enter docker-ce directory:

cd docker-ce
Copy the code

Switch to the latest tag:

Git checkout - b v18.01.0 - ceCopy the code

3. Compilation process

This section working directory to/home/latelee/docker/dev/docker – ce directory.

Modify Dockerfile

An error occurs when you execute the compile command in the following section, so you need to modify the Dockerfile file beforehand. When compiled, a docker image will be built to compile, and the actual command is executed (from the components/ Packaging /deb makefile, which is parsed) : docker build -t debbuild-ubuntu-xenial/x86_64 -f / home/latelee docker/dev/docker – ce/components/packaging/deb/ubuntu – xenial/Dockerfile x86_64). Analysis components/packaging/deb/ubuntu – xenial/Dockerfile x86_64 files, know the build process will download golang compiler, because can’t access golang.org, will build fails in the end. Therefore, the download source needs to be updated. Solution: Find a domestic downloadable website. Such as the dl. Gocn. IO /. Modify the components/packaging/deb/ubuntu – xenial/Dockerfile x86_64 file. RUN curl -fsl “https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz”; | tar xzC/usr/local is amended as: RUN curl – fSL “${GO_VERSION} {GO_VERSION} / go https://dl.gocn.io/golang/$. Linux – amd64. Tar. Gz”; | tar xzC/usr/local will eventually build image: debbuild – ubuntu – xenial/x86_64. Note that this image will continue to be used in subsequent compilations, so it does not need to be removed.

compile

Enter the directory:

cd components/packaging/deb
Copy the code

The Makefile in the deb directory specifies the deb package to compile. Execute the compile command:

Make VERSION = 18.01.0 - ce - dev ENGINE_DIR = / home/latelee docker/dev/docker - ce/components/engine CLI_DIR=/home/latelee/docker/dev/docker-ce/components/cli ubuntu-xenialCopy the code

Command explanation: This command specifies the VERSION number and directory of the Docker component (VERSION, ENGINE, CLI for VERSION number, Docker ENGINE, and docker command line respectively). It also specifies the system VERSION to compile (Ubuntu16.04 code-named Xenial) because, This article builds for one version of the system, not all.

Generate the file

After about half an hour, the compilation was successful. The resulting deb package is located at: Components \ Packaging \deb\debbuild\ Ubuntu-Xenial. Deb file is: the docker – ce_18. 01.0 ~ ce ~ dev ~ git20171228.105814.0.486 a48d – 0 ~ ubuntu_amd64. Deb

The installation

Save the deb package to your machine or other Ubuntu system and run the following command to install it:

# DPKG -i docker - ce_18. 01.0 ~ ce ~ dev ~ git20171228.105814.0.486 a48d - 0 ~ ubuntu_amd64. DebCopy the code

Verify its version number:

# docker -v
Docker version 18.01.0-ce-dev, build 486a48d
Copy the code

At this point, docker compilation is complete.

Docker source directory analysis

Docker-ce source directory is as follows:

.├── CHANGELOG.md├── components  # 组件目录│   ├── cli│   ├── engine│   └── packaging├── components.conf├── CONTRIBUTING.md├── Makefile # 编译所需├── README.md # 说明文件└── VERSION # 版本文件4 directories, 6 files
Copy the code

The component directory contains three subdirectories: CLI, Engine, and Packaging. The first two are the directories where the GO code resides, and Packaging is the directory where the final binary is built. As follows:

. ├ ─ ─ deb │ ├ ─ ─ the build - deb │ ├ ─ ─ common │ ├ ─ ─ debbuild │ ├ ─ ─ debian - arcade │ ├ ─ ─ debian - Jessie │ ├ ─ ─ debian - stretch │ ├ ─ ─ Debian-wheezy │ ├─ Makefile│ ├─ Raspbian-Jessie │ ├─ Md │ ├─ Systemd │ ├─ Ubuntu-Artful │ ├ ─ ─ ubuntu - trusty │ └ ─ ─ ubuntu - xenial ├ ─ ─ Jenkinsfile ├ ─ ─ a Makefile ├ ─ ─ the README. Md ├ ─ ─ the RPM │ ├ ─ ─ centos - 7 │ ├ ─ ─ fedora - 26 │ ├ ─ ─ Fedora - 27 │ ├ ─ ─ gen - RPM - ver │ ├ ─ ─ a Makefile │ ├ ─ ─ the README. Md │ └ ─ ─ systemd └ ─ ─ the static ├ ─ ─ hash_files └ ─ ─ a MakefileCopy the code

The deb directory is compiled to create deb files for different versions of the system (e.g., Ubuntu and Debian, and Ubuntu is code-named for different versions, trusty for 14.04, Xenial for 16.04, and so on). For example, the Ubuntu-Xenial directory includes dockerfiles for different platforms based on 16.04. Build-deb is a compiled script that runs on the containers mentioned earlier. Detailed analysis will be given in subsequent documents.

The appendix

If you download the release and compile it, you get an error like this:

# WARNING! I don't seem to be running in a Docker container.# The result of this command might be an incorrect build, and will not be# officially supported.## Try this instead: make all#error: .git directory missing and DOCKER_GITCOMMIT not specified Please either build with the .git directory accessible, or specify the exact (--short) commit hash you are building using DOCKER_GITCOMMIT for future accountability in diagnosing build issues. Thanks! make[1]: *** [override_dh_auto_builCopy the code

Solution: Use Git to download the repository, then switch to the release branch and compile again.

As the Docker version updates very quickly, this article is for reference only.