Docker source code reading (1) - Compile and run Docker source code (Moby) (no agent required)

foreword

Before officially building the environment, let's take a look at what the moby source code we want to build is. Maybe you all know that Docker is an open source project, and the sudden appearance of Moby here is a bit confusing, so let's take a look at it with everyone. Due to various factors (mainly interest factors), the Docker open source project was renamed Moby around 2017. The architecture of Moby -> Docker CE -> Docker EE is formed as shown in the figure below. Docker CE includes components such as Docker Hub and Docker Compose on top of moby. And Docker EE adds some advanced functions on top of Docker CE, such as vulnerability detection and security monitoring. Therefore, Moby can be simply understood as the development version of Docker deamon
insert image description here

Construct

After understanding what Moby is, the next step is to build it. In fact, the biggest problem in the process of building is the network problem. Many people may time out in the process of connecting to Github. As for the solution, the easiest way is of course to hang the proxy. Of course, in view of the fact that this solution requires a certain threshold, I modified the DockerFile file required for the construction and added the domestic image, which can be installed directly without an agent. I will demonstrate the process step by step.

  1. First Clone moby code to local
git clone https://github.com/moby/moby.git
  1. According to the requirements of the official document, we ensure that no unnecessary docker images are installed on the host (in fact, it is not necessary), so you can use the following command to delete the image
docker system prune -a

or

docker rm $(docker ps -a -q)

In fact, I personally feel that there is no need to do this. You can try to build it first. If there is an error, come and run this step.

  1. Delete all dangling images
docker rmi -f $(docker images -q -a -f dangling=true)
  1. After some operations, we can finally enter our main topic. Here, we enter the source code folder, open the terminal, and execute the construction task
sudo make BIND_DIR=. shell

insert image description here
Students who are confident in their own network can try it out. Of course, if there is no accident, there will be an accident, and there is a high probability that a network error will be reported
insert image description here

So here we have to make some changes to the file

  1. First copy the following patch file, then create a new file proxy_cn.patch in the source code directory and copy the content into it
diff --git a/Dockerfile b/Dockerfile
index c5fa44064c..9e0453c2c4 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -170,7 +170,7 @@ FROM base AS tomll
 ARG GOTOML_VERSION=v1.8.1
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
-        GOBIN=/build/ GO111MODULE=on go install "github.com/pelletier/go-toml/cmd/tomll@${GOTOML_VERSION}" \
+        GOBIN=/build/ GO111MODULE=on GOPROXY=https://goproxy.cn  go install "github.com/pelletier/go-toml/cmd/tomll@${GOTOML_VERSION}" \
      && /build/tomll --help
 
 FROM base AS gowinres
@@ -178,7 +178,7 @@ FROM base AS gowinres
 ARG GOWINRES_VERSION=v0.3.0
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
-        GOBIN=/build/ GO111MODULE=on go install "github.com/tc-hib/go-winres@${GOWINRES_VERSION}" \
+        GOBIN=/build/ GO111MODULE=on GOPROXY=https://goproxy.cn go install "github.com/tc-hib/go-winres@${GOWINRES_VERSION}" \
      && /build/go-winres --help
 
 # containerd
@@ -226,21 +226,21 @@ FROM base AS golangci_lint
 ARG GOLANGCI_LINT_VERSION=v1.51.2
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
-        GOBIN=/build/ GO111MODULE=on go install "github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}" \
+        GOBIN=/build/ GO111MODULE=on GOPROXY=https://goproxy.cn go install "github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}" \
      && /build/golangci-lint --version
 
 FROM base AS gotestsum
 ARG GOTESTSUM_VERSION=v1.8.2
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
-        GOBIN=/build/ GO111MODULE=on go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}" \
+        GOBIN=/build/ GO111MODULE=on GOPROXY=https://goproxy.cn go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}" \
      && /build/gotestsum --version
 
 FROM base AS shfmt
 ARG SHFMT_VERSION=v3.6.0
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
-        GOBIN=/build/ GO111MODULE=on go install "mvdan.cc/sh/v3/cmd/shfmt@${SHFMT_VERSION}" \
+        GOBIN=/build/ GO111MODULE=on GOPROXY=https://goproxy.cn go install "mvdan.cc/sh/v3/cmd/shfmt@${SHFMT_VERSION}" \
      && /build/shfmt --version
 
 # dockercli
@@ -357,6 +357,7 @@ RUN --mount=type=cache,sharing=locked,id=moby-rootlesskit-aptlib,target=/var/lib
         apt-get update && xx-apt-get install -y --no-install-recommends \
             gcc libc6-dev
 ENV GO111MODULE=on
+ENV GOPROXY=https://goproxy.cn
 ARG DOCKER_STATIC
 RUN --mount=from=rootlesskit-src,src=/usr/src/rootlesskit,rw \
     --mount=type=cache,target=/go/pkg/mod \
@@ -392,7 +393,9 @@ RUN --mount=type=cache,sharing=locked,id=moby-crun-aptlib,target=/var/lib/apt \
             libyajl-dev \
             python3 \
             ;
+
 RUN --mount=type=tmpfs,target=/tmp/crun-build \
+    git config --global url."https://ghproxy.com/https://github.com/".insteadOf https://github.com && \
     git clone https://github.com/containers/crun.git /tmp/crun-build && \
     cd /tmp/crun-build && \
     git checkout -q "${CRUN_VERSION}" && \
     

insert image description here
Then apply the patch using the command

git apply proxy_cn.patch


If there is a problem here indicating that the patch file is damaged, it may be a problem with the encoding format. Delete line 66, and then press Enter on line 65 to generate line 66. 6. Build again After the patch is successfully applied, you can use the command in step 4 to
insert image description here
build
again
insert image description here
.

code debugging

After successfully building the source code, we need to start the code debugging test
First run the following command to build Dockerd

hack/make.sh binary install-binary

insert image description here
Then start dockerd with the following command

dockerd -D &

insert image description here
After the startup is complete, we docker versioncan see the docker information using
insert image description here

Pay attention, the version under the server here is dev, indicating that we have built successfully

Next, try to modify the source code. Here we modify the short in docker.go.
insert image description here
After saving the modification, we compile again using the following command

hack/make.sh binary install-binary

Run dockerd --help to see that the modification is successful
insert image description here
So far, you're done, and you can play happily with the moby source code

Guess you like

Origin blog.csdn.net/x646602196/article/details/131032138