網站首頁 編程語言 正文
準備環境
準備一臺Linux主機,并在上面安裝好docker-ce,安裝好make,git就可以開始編譯工作了。對,就是如此簡單,可能你會對此感到異或為啥要裝docker,我不是準備編譯這個玩意么,為啥不裝go,docker不是用go開發的么? 這些疑問會在后面的步驟中解答。
# docker-ce 安裝參考
# FROM: https://developer.aliyun.com/article/110806
# Ubuntu 14.04 16.04 (使用apt-get進行安裝)
# step 1: 安裝必要的一些系統工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安裝GPG證書
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 寫入軟件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安裝 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
注意:其他注意事項在下面的注釋中
# 安裝指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
# docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# Step 2: 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]
# 通過經典網絡、VPC網絡內網安裝時,用以下命令替換Step 2、Step 3中的命令
# 經典網絡:
# curl -fsSL http://mirrors.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# VPC網絡:
# curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# sudo add-apt-repository "deb [arch=amd64] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# CentOS 7 (使用yum進行安裝)
# step 1: 安裝必要的一些系統工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加軟件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安裝 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 開啟Docker服務
sudo service docker start
注意:其他注意事項在下面的注釋中
# 官方軟件源默認啟用了最新的軟件,您可以通過編輯軟件源的方式獲取各個版本的軟件包。例如官方并沒有將測試版本的軟件源置為可用,你可以通過以下方式開啟。同理可以開啟各種測試版本等。
# vim /etc/yum.repos.d/docker-ce.repo
# 將 [docker-ce-test] 下方的 enabled=0 修改為 enabled=1
#
# 安裝指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
# Loading mirror speeds from cached hostfile
# Loaded plugins: branch, fastestmirror, langpacks
# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# Available Packages
# Step2 : 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]
# 注意:在某些版本之后,docker-ce安裝出現了其他依賴包,如果安裝失敗的話請關注錯誤信息。例如 docker-ce 17.03 之后,需要先安裝 docker-ce-selinux。
# yum list docker-ce-selinux- --showduplicates | sort -r
# sudo yum -y install docker-ce-selinux-[VERSION]
# 通過經典網絡、VPC網絡內網安裝時,用以下命令替換Step 2中的命令
# 經典網絡:
# sudo yum-config-manager --add-repo http://mirrors.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
# VPC網絡:
# sudo yum-config-manager --add-repo http://mirrors.could.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
# 安裝其他工具, 以centos7為例子
yum install -y make git
獲取源碼
git clone https://github.com/moby/moby.git
其實如果你有對docker二次開發的需求了,我這邊也不用再多費口舌在moby這個奇怪的項目名稱上
其實就個人而言對docker改名這件事也是頗有微詞的,docker公司愿意開源docker-ce固然是好事,但是直接換個名稱明擺著是想把docker早期創造出來的名聲都讓公司的付費產品“docker”
上,而不是所謂的moby(docker-ce)。
編譯前的準備工作
如果你工作網絡環境足夠優秀(可以穩定的訪問到dockerhub和各種操作系統的源)可以跳過這一小節,肉身在國外的一般沒有這種煩惱。而對于大部分國內的開發者則需要通過代理完成這個開發環境的構建,一下的步驟僅供參考。
添加dockerd的代理
docker的鏡像拖取工作是由dockerd完成的(而不是你平常使用的docker命令行工具),而dockerd一般掛在systemd的進程下(如果有老哥頭鐵從命令行運行dockerd那么當我沒說,這樣也能讀到代理的環境變量)。如果要為dockerd添加代理的話可以參考如下操作
# 修改為你自己的http代理地址
cat > /etc/systemd/system/docker.service.d/http-proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://192.168.144.1:10811"
Environment="HTTPS_PROXY=http://192.168.144.1:10811"
EOF
systemctl daemon-realod
systemctl restart docker
到這一步只是為鏡像的拖取操作添加了代理,但是在開發環境中資源的拖取問題還是沒有辦法解決,這點我們稍微修改Makefile可以處理,參考如下操作
1. 在Makefile文件頭部增加"include .env"
2. Makefile同目錄創建.env文件并寫入你的環境變量,參考如下
BUILD_PROXY := --build-arg "http_proxy=http://192.168.144.1:10811" --build-arg "https_proxy=http://192.168.144.1:10811"
3. 追加參數到指定的構建參數變量,修改Makefile, 在BUILD_OPTS := *后面的一行增加
BUILD_OPTS += $(BUILD_PROXY)
開始編譯
ntpdate ntp1.aliyun.com # 同步時間,如果時間誤差過大會導致部分源依賴獲取失效,非必須
make BIND_DIR=. shell
構建過程由于或需要拖取鏡像和資源耗時一般比較長(即使在網絡條件較好的情況下),一般要10分鐘左右。
如果構建成功你回進入如下的一個容器內終端
root@3cede98051a6:/go/src/github.com/docker/docker#
這個便是makefile通過dockerfile為你構建的docker-ce的編譯環境,這個環境中你開發和調試的工具應有盡有,如你還有特殊的需求可以通過修改dockerfile重新構建(別擔心,由于之前構建的緩存文件,你第二次構建只會構建新增部分,不會花太多的時間),項目的根目錄掛載到主機目錄,所以你在此路徑下構建出的二進制文件也會寫入到宿主機。
接下來我們開始編譯
hack/make.sh binary
編譯完成
root@3cede98051a6:/go/src/github.com/docker/docker# hack/make.sh binary
Removing bundles/
---> Making bundle: binary (in bundles/binary)
Building: bundles/binary-daemon/dockerd
GOOS="" GOARCH="" GOARM=""
Created binary: bundles/binary-daemon/dockerd
Copying nested executables into bundles/binary-daemon
Building: bundles/binary-daemon/docker-proxy
GOOS="" GOARCH="" GOARM=""
Created binary: bundles/binary-daemon/docker-proxy
root@3cede98051a6:/go/src/github.com/docker/docker#
安裝測試
# 還是在容器內
make install
啟動測試
/usr/local/bin/dockerd -D
你也可以在容器內下載鏡像,啟動容器
# 在宿主機另開一個終端,并進入容器
[root@localhost moby]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cede98051a6 docker-dev "hack/dind bash" 8 minutes ago Up 8 minutes condescending_swanson
dae301c0f728 yandex/clickhouse-server "/entrypoint.sh" 33 hours ago Up 33 hours 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp, 9009/tcp ch-server
[root@localhost moby]# docker exec -it 3ced /bin/bash
root@3cede98051a6:/go/src/github.com/docker/docker#
# 下載鏡像,啟動容器
root@3cede98051a6:/go/src/github.com/docker/docker# docker pull centos:7
7: Pulling from library/centos
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Image is up to date for centos:7
root@3cede98051a6:/go/src/github.com/docker/docker# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 15 months ago 204MB
root@3cede98051a6:/go/src/github.com/docker/docker# docker run --rm -it eeb /bin/bash
[root@26c12cba6431 /]# exit
exit
使用Delve調試
都從源碼構建了,總不可能單純奔著純安裝去的吧,多少是想對docker做點簡單的定制化,這里就可以使用Delve來調試我們編譯出來的dockerd二進制程序了。
dlv /usr/local/bin/dockerd -- -D
哎,咋報錯了
2022-12-14T09:10:43Z error layer=debugger error loading binary "/usr/local/bin/dockerd": could not parse .eh_frame section: pointer encoding not supported 0x9b at 0x3150
could not launch process: could not parse .eh_frame section: pointer encoding not supported 0x9b at 0x3150
這個報錯是由于默認的hack/make.sh中的這一段(我所編譯的版本在105)
IAMSTATIC='true'
if [ -z "$DOCKER_DEBUG" ]; then
LDFLAGS='-w'
fi
在編譯時加了-w參數刪除了調試信息,所以在編譯時添加參數或者寫到環境變量里就行啦
# 在容器創建時添加debug參數
DOCKER_DEBUG=1 make BIND_DIR=. shell
# 在編譯時添加參數
DOCKER_DEBUG=1 hack/make.sh binary
以上兩種方法都可以,之后就可以使用dlv來調試docekrd了
root@f42403397fa5:/go/src/github.com/docker/docker# dlv exec --check-go-version=false /usr/local/bin/dockerd -- -D
WARNING: undefined behavior - version of Delve is too old for Go version 1.19.4 (maximum supported version 1.18)
Type 'help' for list of commands.
(dlv)
至于為什么加上 --check-go-version=false 這個參數,如果你是使用開發中的分支,docker的開發者可能忽略了更新dlv的版本,導致dlv的版本有那么一絲落后于go的版本。如果你要解決這個問題可以通過修改Dockerfile中的 Delve鏡像拖取的版本,像是如下這樣,把AELVE_VERSION修改為1.9.1即可。
ARG DELVE_VERSION=v1.8.1
# Delve on Linux is currently only supported on amd64 and arm64;
# https://github.com/go-delve/delve/blob/v1.8.1/pkg/proc/native/support_sentinel.go#L1-L6
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
case $(dpkg --print-architecture) in \
amd64|arm64) \
GOBIN=/build/ GO111MODULE=on go install "github.com/go-delve/delve/cmd/dlv@${DELVE_VERSION}" \
&& /build/dlv --help \
;; \
*) \
mkdir -p /build/ \
;; \
esac
至于delve調試工具如何使用就不在本文里細述,可通過help的方式知道如何使用,類似于gdb,上手十分的簡單。
參考
https://github.com/moby/moby/tree/master/docs/contributing
https://github.com/moby/moby/blob/master/docs/contributing/debug.md
https://github.com/moby/moby/blob/master/docs/contributing/set-up-dev-env.md
原文鏈接:https://www.cnblogs.com/oscar2960/p/16992078.html
相關推薦
- 2022-07-31 C語言編程基礎char類型轉換示例_C 語言
- 2023-05-06 pandas中groupby操作實現_python
- 2022-05-25 C++?自定義單向鏈表?ListNode詳情_C 語言
- 2023-03-28 python程序中調用其他程序的實現_python
- 2022-12-29 react如何將字符串轉義成html語句_React
- 2022-06-12 Python中常用的內置函數_python
- 2022-03-15 When allowCredentials is true, allowedOrigins cann
- 2022-08-22 ?C++?new?和?delete?關鍵字詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支