首页 编程笔记

windows wsl 子系统 ubuntu 安装 golang 服务器

pyweeX 发布于 09-25
编程笔记
pyweeX

在安装 wsl 之前,如果不做一些确认,最终会导致进入子系统后 docker 一直安装不上!还有其他诸多问题。所以 WSL 一定要安装第2版本的。

wsl 相关命令:

  1. # 查看当前子系统版本,一定要显示2的,如果显示1,那就会有很多缺陷
  2. wsl -l -v
  3. NAME STATE VERSION
  4. * Ubuntu Stopped 2
  5. Ubuntu-18.04 Stopped 2
  6. # 查看已经安装的分发版系统
  7. wslconfig /l
  8. # 更改默认的 Linux 发行版,请运行以下命令,其中 Name 是 Linux 发行版的名称
  9. wslconfig /setdefault Name
  10. # 要将默认环境由 Ubuntu 切换为 openSUSE Leap 42 由可以执行如下命令
  11. wslconfig /setdefault openSUSE-42

手动安装 hyper-v,将以下代码保存为 .bat 文件运行,完成后重启系统。我运行之后有一些报错,虽然重启了系统发现菜单中出现了 hyper-v 但怎么也开启不了。实际上其实开启了子系统功能也是可以使用 wsl 的。

  1. pushd "%~dp0"
  2. dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hv.txt
  3. for /f %%i in ('findstr /i . hv.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
  4. del hv.txt
  5. Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
  6. Pause

先升级WSL到WSL2 重中之重

  1. wsl --set-default-version 2
  2. wsl --update

完成后,在确保 windows 已经开启了子系统服务的前提下,直接在 cmd 运行 wsl —install -d Ubuntu,安装过程一般要几分钟,完成后会自动进入系统,然后先退出,在 windows 下 cmd 运行这句,以 root 登录:

  1. ubuntu config --default-user root

然后再执行 ubuntu 重新进入系统,这时候你的登录者就是 root 了。

进入后会提示

  1. This message is shown once a day. To disable it please create the
  2. /root/.hushlogin file.

所以手动创建一下:

  1. sudo touch /root/.hushlogin

安装 Docker

这时候先安装最容易出错的 docker 吧。

先更新源,换了几个阿里的,总是报错,索引直接用默认的了,并不算很慢。

  1. # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
  2. # newer versions of the distribution.
  3. deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
  4. # deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted
  5. ## Major bug fix updates produced after the final release of the
  6. ## distribution.
  7. deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
  8. # deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
  9. ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
  10. ## team. Also, please note that software in universe WILL NOT receive any
  11. ## review or updates from the Ubuntu security team.
  12. deb http://archive.ubuntu.com/ubuntu/ jammy universe
  13. # deb-src http://archive.ubuntu.com/ubuntu/ jammy universe
  14. deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
  15. # deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates universe
  16. ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
  17. ## team, and may not be under a free licence. Please satisfy yourself as to
  18. ## your rights to use the software. Also, please note that software in
  19. ## multiverse WILL NOT receive any review or updates from the Ubuntu
  20. ## security team.
  21. deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
  22. # deb-src http://archive.ubuntu.com/ubuntu/ jammy multiverse
  23. deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
  24. # deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
  25. ## N.B. software from this repository may not have been tested as
  26. ## extensively as that contained in the main release, although it includes
  27. ## newer versions of some applications which may provide useful features.
  28. ## Also, please note that software in backports WILL NOT receive any review
  29. ## or updates from the Ubuntu security team.
  30. deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
  31. # deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
  32. deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted
  33. # deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted
  34. deb http://security.ubuntu.com/ubuntu/ jammy-security universe
  35. # deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe
  36. deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse
  37. # deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse

上面是默认的,所以我这里直接更新好了。

  1. apt update
  2. apt upgrade

更新完成后开始安装 docker,wsl 子系统下的 docker 安装非常讲究,你如果只是用传统的安装方式进行安装,大概率会报错,而且是大量报错,完全解决不了。

后来我在 docker 官方文档中发现了专门针对 WSL 下的 docker 安装方式,具体就是两句代码,中间那句是我加的:

  1. curl -fsSL https://get.docker.com -o get-docker.sh
  2. chmod 777 get-docker.sh
  3. sudo sh get-docker.sh

正常情况下,执行完上面的句子,你的 docker 就能安装完成了。我在这里备份一下这个脚本文件:

  1. #!/bin/sh
  2. set -e
  3. # Docker Engine for Linux installation script.
  4. #
  5. # This script is intended as a convenient way to configure docker's package
  6. # repositories and to install Docker Engine, This script is not recommended
  7. # for production environments. Before running this script, make yourself familiar
  8. # with potential risks and limitations, and refer to the installation manual
  9. # at https://docs.docker.com/engine/install/ for alternative installation methods.
  10. #
  11. # The script:
  12. #
  13. # - Requires `root` or `sudo` privileges to run.
  14. # - Attempts to detect your Linux distribution and version and configure your
  15. # package management system for you.
  16. # - Doesn't allow you to customize most installation parameters.
  17. # - Installs dependencies and recommendations without asking for confirmation.
  18. # - Installs the latest stable release (by default) of Docker CLI, Docker Engine,
  19. # Docker Buildx, Docker Compose, containerd, and runc. When using this script
  20. # to provision a machine, this may result in unexpected major version upgrades
  21. # of these packages. Always test upgrades in a test environment before
  22. # deploying to your production systems.
  23. # - Isn't designed to upgrade an existing Docker installation. When using the
  24. # script to update an existing installation, dependencies may not be updated
  25. # to the expected version, resulting in outdated versions.
  26. #
  27. # Source code is available at https://github.com/docker/docker-install/
  28. #
  29. # Usage
  30. # ==============================================================================
  31. #
  32. # To install the latest stable versions of Docker CLI, Docker Engine, and their
  33. # dependencies:
  34. #
  35. # 1. download the script
  36. #
  37. # $ curl -fsSL https://get.docker.com -o install-docker.sh
  38. #
  39. # 2. verify the script's content
  40. #
  41. # $ cat install-docker.sh
  42. #
  43. # 3. run the script with --dry-run to verify the steps it executes
  44. #
  45. # $ sh install-docker.sh --dry-run
  46. #
  47. # 4. run the script either as root, or using sudo to perform the installation.
  48. #
  49. # $ sudo sh install-docker.sh
  50. #
  51. # Command-line options
  52. # ==============================================================================
  53. #
  54. # --version <VERSION>
  55. # Use the --version option to install a specific version, for example:
  56. #
  57. # $ sudo sh install-docker.sh --version 23.0
  58. #
  59. # --channel <stable|test>
  60. #
  61. # Use the --channel option to install from an alternative installation channel.
  62. # The following example installs the latest versions from the "test" channel,
  63. # which includes pre-releases (alpha, beta, rc):
  64. #
  65. # $ sudo sh install-docker.sh --channel test
  66. #
  67. # Alternatively, use the script at https://test.docker.com, which uses the test
  68. # channel as default.
  69. #
  70. # --mirror <Aliyun|AzureChinaCloud>
  71. #
  72. # Use the --mirror option to install from a mirror supported by this script.
  73. # Available mirrors are "Aliyun" (https://mirrors.aliyun.com/docker-ce), and
  74. # "AzureChinaCloud" (https://mirror.azure.cn/docker-ce), for example:
  75. #
  76. # $ sudo sh install-docker.sh --mirror AzureChinaCloud
  77. #
  78. # ==============================================================================
  79. # Git commit from https://github.com/docker/docker-install when
  80. # the script was uploaded (Should only be modified by upload job):
  81. SCRIPT_COMMIT_SHA="e5543d473431b782227f8908005543bb4389b8de"
  82. # strip "v" prefix if present
  83. VERSION="${VERSION#v}"
  84. # The channel to install from:
  85. # * stable
  86. # * test
  87. # * edge (deprecated)
  88. # * nightly (deprecated)
  89. DEFAULT_CHANNEL_VALUE="stable"
  90. if [ -z "$CHANNEL" ]; then
  91. CHANNEL=$DEFAULT_CHANNEL_VALUE
  92. fi
  93. DEFAULT_DOWNLOAD_URL="https://download.docker.com"
  94. if [ -z "$DOWNLOAD_URL" ]; then
  95. DOWNLOAD_URL=$DEFAULT_DOWNLOAD_URL
  96. fi
  97. DEFAULT_REPO_FILE="docker-ce.repo"
  98. if [ -z "$REPO_FILE" ]; then
  99. REPO_FILE="$DEFAULT_REPO_FILE"
  100. fi
  101. mirror=''
  102. DRY_RUN=${DRY_RUN:-}
  103. while [ $# -gt 0 ]; do
  104. case "$1" in
  105. --channel)
  106. CHANNEL="$2"
  107. shift
  108. ;;
  109. --dry-run)
  110. DRY_RUN=1
  111. ;;
  112. --mirror)
  113. mirror="$2"
  114. shift
  115. ;;
  116. --version)
  117. VERSION="${2#v}"
  118. shift
  119. ;;
  120. --*)
  121. echo "Illegal option $1"
  122. ;;
  123. esac
  124. shift $(( $# > 0 ? 1 : 0 ))
  125. done
  126. case "$mirror" in
  127. Aliyun)
  128. DOWNLOAD_URL="https://mirrors.aliyun.com/docker-ce"
  129. ;;
  130. AzureChinaCloud)
  131. DOWNLOAD_URL="https://mirror.azure.cn/docker-ce"
  132. ;;
  133. "")
  134. ;;
  135. *)
  136. >&2 echo "unknown mirror '$mirror': use either 'Aliyun', or 'AzureChinaCloud'."
  137. exit 1
  138. ;;
  139. esac
  140. case "$CHANNEL" in
  141. stable|test)
  142. ;;
  143. edge|nightly)
  144. >&2 echo "DEPRECATED: the $CHANNEL channel has been deprecated and is no longer supported by this script."
  145. exit 1
  146. ;;
  147. *)
  148. >&2 echo "unknown CHANNEL '$CHANNEL': use either stable or test."
  149. exit 1
  150. ;;
  151. esac
  152. command_exists() {
  153. command -v "$@" > /dev/null 2>&1
  154. }
  155. # version_gte checks if the version specified in $VERSION is at least the given
  156. # SemVer (Maj.Minor[.Patch]), or CalVer (YY.MM) version.It returns 0 (success)
  157. # if $VERSION is either unset (=latest) or newer or equal than the specified
  158. # version, or returns 1 (fail) otherwise.
  159. #
  160. # examples:
  161. #
  162. # VERSION=23.0
  163. # version_gte 23.0 // 0 (success)
  164. # version_gte 20.10 // 0 (success)
  165. # version_gte 19.03 // 0 (success)
  166. # version_gte 21.10 // 1 (fail)
  167. version_gte() {
  168. if [ -z "$VERSION" ]; then
  169. return 0
  170. fi
  171. eval version_compare "$VERSION" "$1"
  172. }
  173. # version_compare compares two version strings (either SemVer (Major.Minor.Path),
  174. # or CalVer (YY.MM) version strings. It returns 0 (success) if version A is newer
  175. # or equal than version B, or 1 (fail) otherwise. Patch releases and pre-release
  176. # (-alpha/-beta) are not taken into account
  177. #
  178. # examples:
  179. #
  180. # version_compare 23.0.0 20.10 // 0 (success)
  181. # version_compare 23.0 20.10 // 0 (success)
  182. # version_compare 20.10 19.03 // 0 (success)
  183. # version_compare 20.10 20.10 // 0 (success)
  184. # version_compare 19.03 20.10 // 1 (fail)
  185. version_compare() (
  186. set +x
  187. yy_a="$(echo "$1" | cut -d'.' -f1)"
  188. yy_b="$(echo "$2" | cut -d'.' -f1)"
  189. if [ "$yy_a" -lt "$yy_b" ]; then
  190. return 1
  191. fi
  192. if [ "$yy_a" -gt "$yy_b" ]; then
  193. return 0
  194. fi
  195. mm_a="$(echo "$1" | cut -d'.' -f2)"
  196. mm_b="$(echo "$2" | cut -d'.' -f2)"
  197. # trim leading zeros to accommodate CalVer
  198. mm_a="${mm_a#0}"
  199. mm_b="${mm_b#0}"
  200. if [ "${mm_a:-0}" -lt "${mm_b:-0}" ]; then
  201. return 1
  202. fi
  203. return 0
  204. )
  205. is_dry_run() {
  206. if [ -z "$DRY_RUN" ]; then
  207. return 1
  208. else
  209. return 0
  210. fi
  211. }
  212. is_wsl() {
  213. case "$(uname -r)" in
  214. *microsoft* ) true ;; # WSL 2
  215. *Microsoft* ) true ;; # WSL 1
  216. * ) false;;
  217. esac
  218. }
  219. is_darwin() {
  220. case "$(uname -s)" in
  221. *darwin* ) true ;;
  222. *Darwin* ) true ;;
  223. * ) false;;
  224. esac
  225. }
  226. deprecation_notice() {
  227. distro=$1
  228. distro_version=$2
  229. echo
  230. printf "\033[91;1mDEPRECATION WARNING\033[0m\n"
  231. printf " This Linux distribution (\033[1m%s %s\033[0m) reached end-of-life and is no longer supported by this script.\n" "$distro" "$distro_version"
  232. echo " No updates or security fixes will be released for this distribution, and users are recommended"
  233. echo " to upgrade to a currently maintained version of $distro."
  234. echo
  235. printf "Press \033[1mCtrl+C\033[0m now to abort this script, or wait for the installation to continue."
  236. echo
  237. sleep 10
  238. }
  239. get_distribution() {
  240. lsb_dist=""
  241. # Every system that we officially support has /etc/os-release
  242. if [ -r /etc/os-release ]; then
  243. lsb_dist="$(. /etc/os-release && echo "$ID")"
  244. fi
  245. # Returning an empty string here should be alright since the
  246. # case statements don't act unless you provide an actual value
  247. echo "$lsb_dist"
  248. }
  249. echo_docker_as_nonroot() {
  250. if is_dry_run; then
  251. return
  252. fi
  253. if command_exists docker && [ -e /var/run/docker.sock ]; then
  254. (
  255. set -x
  256. $sh_c 'docker version'
  257. ) || true
  258. fi
  259. # intentionally mixed spaces and tabs here -- tabs are stripped by "<<-EOF", spaces are kept in the output
  260. echo
  261. echo "================================================================================"
  262. echo
  263. if version_gte "20.10"; then
  264. echo "To run Docker as a non-privileged user, consider setting up the"
  265. echo "Docker daemon in rootless mode for your user:"
  266. echo
  267. echo " dockerd-rootless-setuptool.sh install"
  268. echo
  269. echo "Visit https://docs.docker.com/go/rootless/ to learn about rootless mode."
  270. echo
  271. fi
  272. echo
  273. echo "To run the Docker daemon as a fully privileged service, but granting non-root"
  274. echo "users access, refer to https://docs.docker.com/go/daemon-access/"
  275. echo
  276. echo "WARNING: Access to the remote API on a privileged Docker daemon is equivalent"
  277. echo " to root access on the host. Refer to the 'Docker daemon attack surface'"
  278. echo " documentation for details: https://docs.docker.com/go/attack-surface/"
  279. echo
  280. echo "================================================================================"
  281. echo
  282. }
  283. # Check if this is a forked Linux distro
  284. check_forked() {
  285. # Check for lsb_release command existence, it usually exists in forked distros
  286. if command_exists lsb_release; then
  287. # Check if the `-u` option is supported
  288. set +e
  289. lsb_release -a -u > /dev/null 2>&1
  290. lsb_release_exit_code=$?
  291. set -e
  292. # Check if the command has exited successfully, it means we're in a forked distro
  293. if [ "$lsb_release_exit_code" = "0" ]; then
  294. # Print info about current distro
  295. cat <<-EOF
  296. You're using '$lsb_dist' version '$dist_version'.
  297. EOF
  298. # Get the upstream release info
  299. lsb_dist=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'id' | cut -d ':' -f 2 | tr -d '[:space:]')
  300. dist_version=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'codename' | cut -d ':' -f 2 | tr -d '[:space:]')
  301. # Print info about upstream distro
  302. cat <<-EOF
  303. Upstream release is '$lsb_dist' version '$dist_version'.
  304. EOF
  305. else
  306. if [ -r /etc/debian_version ] && [ "$lsb_dist" != "ubuntu" ] && [ "$lsb_dist" != "raspbian" ]; then
  307. if [ "$lsb_dist" = "osmc" ]; then
  308. # OSMC runs Raspbian
  309. lsb_dist=raspbian
  310. else
  311. # We're Debian and don't even know it!
  312. lsb_dist=debian
  313. fi
  314. dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
  315. case "$dist_version" in
  316. 12)
  317. dist_version="bookworm"
  318. ;;
  319. 11)
  320. dist_version="bullseye"
  321. ;;
  322. 10)
  323. dist_version="buster"
  324. ;;
  325. 9)
  326. dist_version="stretch"
  327. ;;
  328. 8)
  329. dist_version="jessie"
  330. ;;
  331. esac
  332. fi
  333. fi
  334. fi
  335. }
  336. do_install() {
  337. echo "# Executing docker install script, commit: $SCRIPT_COMMIT_SHA"
  338. if command_exists docker; then
  339. cat >&2 <<-'EOF'
  340. Warning: the "docker" command appears to already exist on this system.
  341. If you already have Docker installed, this script can cause trouble, which is
  342. why we're displaying this warning and provide the opportunity to cancel the
  343. installation.
  344. If you installed the current Docker package using this script and are using it
  345. again to update Docker, you can safely ignore this message.
  346. You may press Ctrl+C now to abort this script.
  347. EOF
  348. ( set -x; sleep 20 )
  349. fi
  350. user="$(id -un 2>/dev/null || true)"
  351. sh_c='sh -c'
  352. if [ "$user" != 'root' ]; then
  353. if command_exists sudo; then
  354. sh_c='sudo -E sh -c'
  355. elif command_exists su; then
  356. sh_c='su -c'
  357. else
  358. cat >&2 <<-'EOF'
  359. Error: this installer needs the ability to run commands as root.
  360. We are unable to find either "sudo" or "su" available to make this happen.
  361. EOF
  362. exit 1
  363. fi
  364. fi
  365. if is_dry_run; then
  366. sh_c="echo"
  367. fi
  368. # perform some very rudimentary platform detection
  369. lsb_dist=$( get_distribution )
  370. lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"
  371. if is_wsl; then
  372. echo
  373. echo "WSL DETECTED: We recommend using Docker Desktop for Windows."
  374. echo "Please get Docker Desktop from https://www.docker.com/products/docker-desktop/"
  375. echo
  376. cat >&2 <<-'EOF'
  377. You may press Ctrl+C now to abort this script.
  378. EOF
  379. ( set -x; sleep 20 )
  380. fi
  381. case "$lsb_dist" in
  382. ubuntu)
  383. if command_exists lsb_release; then
  384. dist_version="$(lsb_release --codename | cut -f2)"
  385. fi
  386. if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
  387. dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
  388. fi
  389. ;;
  390. debian|raspbian)
  391. dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
  392. case "$dist_version" in
  393. 12)
  394. dist_version="bookworm"
  395. ;;
  396. 11)
  397. dist_version="bullseye"
  398. ;;
  399. 10)
  400. dist_version="buster"
  401. ;;
  402. 9)
  403. dist_version="stretch"
  404. ;;
  405. 8)
  406. dist_version="jessie"
  407. ;;
  408. esac
  409. ;;
  410. centos|rhel|sles)
  411. if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
  412. dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
  413. fi
  414. ;;
  415. *)
  416. if command_exists lsb_release; then
  417. dist_version="$(lsb_release --release | cut -f2)"
  418. fi
  419. if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
  420. dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
  421. fi
  422. ;;
  423. esac
  424. # Check if this is a forked Linux distro
  425. check_forked
  426. # Print deprecation warnings for distro versions that recently reached EOL,
  427. # but may still be commonly used (especially LTS versions).
  428. case "$lsb_dist.$dist_version" in
  429. debian.stretch|debian.jessie)
  430. deprecation_notice "$lsb_dist" "$dist_version"
  431. ;;
  432. raspbian.stretch|raspbian.jessie)
  433. deprecation_notice "$lsb_dist" "$dist_version"
  434. ;;
  435. ubuntu.xenial|ubuntu.trusty)
  436. deprecation_notice "$lsb_dist" "$dist_version"
  437. ;;
  438. ubuntu.impish|ubuntu.hirsute|ubuntu.groovy|ubuntu.eoan|ubuntu.disco|ubuntu.cosmic)
  439. deprecation_notice "$lsb_dist" "$dist_version"
  440. ;;
  441. fedora.*)
  442. if [ "$dist_version" -lt 36 ]; then
  443. deprecation_notice "$lsb_dist" "$dist_version"
  444. fi
  445. ;;
  446. esac
  447. # Run setup for each distro accordingly
  448. case "$lsb_dist" in
  449. ubuntu|debian|raspbian)
  450. pre_reqs="apt-transport-https ca-certificates curl"
  451. if ! command -v gpg > /dev/null; then
  452. pre_reqs="$pre_reqs gnupg"
  453. fi
  454. apt_repo="deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] $DOWNLOAD_URL/linux/$lsb_dist $dist_version $CHANNEL"
  455. (
  456. if ! is_dry_run; then
  457. set -x
  458. fi
  459. $sh_c 'apt-get update -qq >/dev/null'
  460. $sh_c "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $pre_reqs >/dev/null"
  461. $sh_c 'install -m 0755 -d /etc/apt/keyrings'
  462. $sh_c "curl -fsSL \"$DOWNLOAD_URL/linux/$lsb_dist/gpg\" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg"
  463. $sh_c "chmod a+r /etc/apt/keyrings/docker.gpg"
  464. $sh_c "echo \"$apt_repo\" > /etc/apt/sources.list.d/docker.list"
  465. $sh_c 'apt-get update -qq >/dev/null'
  466. )
  467. pkg_version=""
  468. if [ -n "$VERSION" ]; then
  469. if is_dry_run; then
  470. echo "# WARNING: VERSION pinning is not supported in DRY_RUN"
  471. else
  472. # Will work for incomplete versions IE (17.12), but may not actually grab the "latest" if in the test channel
  473. pkg_pattern="$(echo "$VERSION" | sed 's/-ce-/~ce~.*/g' | sed 's/-/.*/g')"
  474. search_command="apt-cache madison docker-ce | grep '$pkg_pattern' | head -1 | awk '{\$1=\$1};1' | cut -d' ' -f 3"
  475. pkg_version="$($sh_c "$search_command")"
  476. echo "INFO: Searching repository for VERSION '$VERSION'"
  477. echo "INFO: $search_command"
  478. if [ -z "$pkg_version" ]; then
  479. echo
  480. echo "ERROR: '$VERSION' not found amongst apt-cache madison results"
  481. echo
  482. exit 1
  483. fi
  484. if version_gte "18.09"; then
  485. search_command="apt-cache madison docker-ce-cli | grep '$pkg_pattern' | head -1 | awk '{\$1=\$1};1' | cut -d' ' -f 3"
  486. echo "INFO: $search_command"
  487. cli_pkg_version="=$($sh_c "$search_command")"
  488. fi
  489. pkg_version="=$pkg_version"
  490. fi
  491. fi
  492. (
  493. pkgs="docker-ce${pkg_version%=}"
  494. if version_gte "18.09"; then
  495. # older versions didn't ship the cli and containerd as separate packages
  496. pkgs="$pkgs docker-ce-cli${cli_pkg_version%=} containerd.io"
  497. fi
  498. if version_gte "20.10"; then
  499. pkgs="$pkgs docker-compose-plugin docker-ce-rootless-extras$pkg_version"
  500. fi
  501. if version_gte "23.0"; then
  502. pkgs="$pkgs docker-buildx-plugin"
  503. fi
  504. if ! is_dry_run; then
  505. set -x
  506. fi
  507. $sh_c "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $pkgs >/dev/null"
  508. )
  509. echo_docker_as_nonroot
  510. exit 0
  511. ;;
  512. centos|fedora|rhel)
  513. if [ "$(uname -m)" != "s390x" ] && [ "$lsb_dist" = "rhel" ]; then
  514. echo "Packages for RHEL are currently only available for s390x."
  515. exit 1
  516. fi
  517. if [ "$lsb_dist" = "fedora" ]; then
  518. pkg_manager="dnf"
  519. config_manager="dnf config-manager"
  520. enable_channel_flag="--set-enabled"
  521. disable_channel_flag="--set-disabled"
  522. pre_reqs="dnf-plugins-core"
  523. pkg_suffix="fc$dist_version"
  524. else
  525. pkg_manager="yum"
  526. config_manager="yum-config-manager"
  527. enable_channel_flag="--enable"
  528. disable_channel_flag="--disable"
  529. pre_reqs="yum-utils"
  530. pkg_suffix="el"
  531. fi
  532. repo_file_url="$DOWNLOAD_URL/linux/$lsb_dist/$REPO_FILE"
  533. (
  534. if ! is_dry_run; then
  535. set -x
  536. fi
  537. $sh_c "$pkg_manager install -y -q $pre_reqs"
  538. $sh_c "$config_manager --add-repo $repo_file_url"
  539. if [ "$CHANNEL" != "stable" ]; then
  540. $sh_c "$config_manager $disable_channel_flag 'docker-ce-*'"
  541. $sh_c "$config_manager $enable_channel_flag 'docker-ce-$CHANNEL'"
  542. fi
  543. $sh_c "$pkg_manager makecache"
  544. )
  545. pkg_version=""
  546. if [ -n "$VERSION" ]; then
  547. if is_dry_run; then
  548. echo "# WARNING: VERSION pinning is not supported in DRY_RUN"
  549. else
  550. pkg_pattern="$(echo "$VERSION" | sed 's/-ce-/\\\\.ce.*/g' | sed 's/-/.*/g').*$pkg_suffix"
  551. search_command="$pkg_manager list --showduplicates docker-ce | grep '$pkg_pattern' | tail -1 | awk '{print \$2}'"
  552. pkg_version="$($sh_c "$search_command")"
  553. echo "INFO: Searching repository for VERSION '$VERSION'"
  554. echo "INFO: $search_command"
  555. if [ -z "$pkg_version" ]; then
  556. echo
  557. echo "ERROR: '$VERSION' not found amongst $pkg_manager list results"
  558. echo
  559. exit 1
  560. fi
  561. if version_gte "18.09"; then
  562. # older versions don't support a cli package
  563. search_command="$pkg_manager list --showduplicates docker-ce-cli | grep '$pkg_pattern' | tail -1 | awk '{print \$2}'"
  564. cli_pkg_version="$($sh_c "$search_command" | cut -d':' -f 2)"
  565. fi
  566. # Cut out the epoch and prefix with a '-'
  567. pkg_version="-$(echo "$pkg_version" | cut -d':' -f 2)"
  568. fi
  569. fi
  570. (
  571. pkgs="docker-ce$pkg_version"
  572. if version_gte "18.09"; then
  573. # older versions didn't ship the cli and containerd as separate packages
  574. if [ -n "$cli_pkg_version" ]; then
  575. pkgs="$pkgs docker-ce-cli-$cli_pkg_version containerd.io"
  576. else
  577. pkgs="$pkgs docker-ce-cli containerd.io"
  578. fi
  579. fi
  580. if version_gte "20.10"; then
  581. pkgs="$pkgs docker-compose-plugin docker-ce-rootless-extras$pkg_version"
  582. fi
  583. if version_gte "23.0"; then
  584. pkgs="$pkgs docker-buildx-plugin"
  585. fi
  586. if ! is_dry_run; then
  587. set -x
  588. fi
  589. $sh_c "$pkg_manager install -y -q $pkgs"
  590. )
  591. echo_docker_as_nonroot
  592. exit 0
  593. ;;
  594. sles)
  595. if [ "$(uname -m)" != "s390x" ]; then
  596. echo "Packages for SLES are currently only available for s390x"
  597. exit 1
  598. fi
  599. if [ "$dist_version" = "15.3" ]; then
  600. sles_version="SLE_15_SP3"
  601. else
  602. sles_minor_version="${dist_version##*.}"
  603. sles_version="15.$sles_minor_version"
  604. fi
  605. repo_file_url="$DOWNLOAD_URL/linux/$lsb_dist/$REPO_FILE"
  606. pre_reqs="ca-certificates curl libseccomp2 awk"
  607. (
  608. if ! is_dry_run; then
  609. set -x
  610. fi
  611. $sh_c "zypper install -y $pre_reqs"
  612. $sh_c "zypper addrepo $repo_file_url"
  613. if ! is_dry_run; then
  614. cat >&2 <<-'EOF'
  615. WARNING!!
  616. openSUSE repository (https://download.opensuse.org/repositories/security:SELinux) will be enabled now.
  617. Do you wish to continue?
  618. You may press Ctrl+C now to abort this script.
  619. EOF
  620. ( set -x; sleep 30 )
  621. fi
  622. opensuse_repo="https://download.opensuse.org/repositories/security:SELinux/$sles_version/security:SELinux.repo"
  623. $sh_c "zypper addrepo $opensuse_repo"
  624. $sh_c "zypper --gpg-auto-import-keys refresh"
  625. $sh_c "zypper lr -d"
  626. )
  627. pkg_version=""
  628. if [ -n "$VERSION" ]; then
  629. if is_dry_run; then
  630. echo "# WARNING: VERSION pinning is not supported in DRY_RUN"
  631. else
  632. pkg_pattern="$(echo "$VERSION" | sed 's/-ce-/\\\\.ce.*/g' | sed 's/-/.*/g')"
  633. search_command="zypper search -s --match-exact 'docker-ce' | grep '$pkg_pattern' | tail -1 | awk '{print \$6}'"
  634. pkg_version="$($sh_c "$search_command")"
  635. echo "INFO: Searching repository for VERSION '$VERSION'"
  636. echo "INFO: $search_command"
  637. if [ -z "$pkg_version" ]; then
  638. echo
  639. echo "ERROR: '$VERSION' not found amongst zypper list results"
  640. echo
  641. exit 1
  642. fi
  643. search_command="zypper search -s --match-exact 'docker-ce-cli' | grep '$pkg_pattern' | tail -1 | awk '{print \$6}'"
  644. # It's okay for cli_pkg_version to be blank, since older versions don't support a cli package
  645. cli_pkg_version="$($sh_c "$search_command")"
  646. pkg_version="-$pkg_version"
  647. fi
  648. fi
  649. (
  650. pkgs="docker-ce$pkg_version"
  651. if version_gte "18.09"; then
  652. if [ -n "$cli_pkg_version" ]; then
  653. # older versions didn't ship the cli and containerd as separate packages
  654. pkgs="$pkgs docker-ce-cli-$cli_pkg_version containerd.io"
  655. else
  656. pkgs="$pkgs docker-ce-cli containerd.io"
  657. fi
  658. fi
  659. if version_gte "20.10"; then
  660. pkgs="$pkgs docker-compose-plugin docker-ce-rootless-extras$pkg_version"
  661. fi
  662. if version_gte "23.0"; then
  663. pkgs="$pkgs docker-buildx-plugin"
  664. fi
  665. if ! is_dry_run; then
  666. set -x
  667. fi
  668. $sh_c "zypper -q install -y $pkgs"
  669. )
  670. echo_docker_as_nonroot
  671. exit 0
  672. ;;
  673. *)
  674. if [ -z "$lsb_dist" ]; then
  675. if is_darwin; then
  676. echo
  677. echo "ERROR: Unsupported operating system 'macOS'"
  678. echo "Please get Docker Desktop from https://www.docker.com/products/docker-desktop"
  679. echo
  680. exit 1
  681. fi
  682. fi
  683. echo
  684. echo "ERROR: Unsupported distribution '$lsb_dist'"
  685. echo
  686. exit 1
  687. ;;
  688. esac
  689. exit 1
  690. }
  691. # wrapped up in a function so that we have some protection against only getting
  692. # half the file during "curl | sh"
  693. do_install

具体的地址是这个:https://docs.docker.com/engine/install/ubuntu/

至此,docker 安装完成。

执行 dockerd 启动时出现报错

  1. falling back to default port range 49153-65535 error="open /proc/sys/net/ipv4/ip_local_port_range: no such file or directory"
  2. INFO[2023-09-25T01:21:29.615164400+08:00] unable to detect if iptables supports xlock: 'iptables --wait -L -n': `iptables/1.8.7 Failed to initialize nft: Protocol not supported` error="exit status 1"
  3. INFO[2023-09-25T01:21:29.706463300+08:00] stopping event stream following graceful shutdown error="<nil>" module=libcontainerd namespace=moby
  4. INFO[2023-09-25T01:21:29.707695400+08:00] stopping healthcheck following graceful shutdown module=libcontainerd
  5. INFO[2023-09-25T01:21:29.707753400+08:00] stopping event stream following graceful shutdown error="context canceled" module=libcontainerd namespace=plugins.moby
  6. failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables/1.8.7 Failed to initialize nft: Protocol not supported
  7. (exit status 1)

这是因为没有定义 docker 的 daemon.json,这里手动创建一下,这是我的内容:

  1. vim /etc/docker/daemon.json
  2. {
  3. "log-driver": "json-file",
  4. "log-opts": {
  5. "max-size": "800m",
  6. "max-file": "50"
  7. },
  8. "registry-mirrors": [
  9. "https://hub-mirror.c.163.com",
  10. "https://docker.mirrors.ustc.edu.cn",
  11. "https://registry.docker-cn.com"
  12. ]
  13. }

启动还是报错:

  1. failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables/1.8.7 Failed to initialize nft: Protocol not supported

在配置中加入 iptables: false

  1. {
  2. "iptables": false,
  3. "log-driver": "json-file",
  4. "log-opts": {
  5. "max-size": "800m",
  6. "max-file": "50"
  7. },
  8. "registry-mirrors": [
  9. "https://hub-mirror.c.163.com",
  10. "https://docker.mirrors.ustc.edu.cn",
  11. "https://registry.docker-cn.com"
  12. ]
  13. }

经过以上几步,docker 顺利完成安装。通过以下命令启动:

  1. dockerd # 在前台运行
  2. service docker start # 在后台运行

新版本的 docker 已经自带了 docker compose,所以这里不用再单独装了。


安装 make,用于执行 Makefile,个人需要,如果你不用,则不需要装。

  1. apt install make

安装GO语言

  1. cd /opt
  2. wget https://go.dev/dl/go1.21.1.linux-amd64.tar.gz
  3. rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz
  4. # 打开 /etc/profile 粘贴环境变量进去
  5. vim /etc/profile
  6. export PATH=$PATH:/usr/local/go/bin
  7. source /etc/profile
  8. go env -w GOPROXY=https://goproxy.cn,direct
  9. go env -w GO111MODULE=on
  10. # 完成 查看其版本
  11. go version

直接使用 docker compose 安装 mysql 和 redis

配置文件,先提前创建一下网卡

  1. docker network create bb

新建 my.cnf 文件保存下面内容

  1. [mysqld]
  2. user=mysql
  3. default-storage-engine=INNODB
  4. character-set-server=utf8
  5. secure-file-priv=/var/lib/mysql
  6. [client]
  7. default-character-set=utf8
  8. [mysql]
  9. default-character-set=utf8

新建 compose.yaml 文件保存下面内容

  1. version: '3'
  2. services:
  3. redis:
  4. user: root
  5. image: redis
  6. restart: always
  7. hostname: redis
  8. container_name: redis
  9. privileged: true
  10. ports:
  11. - 6379:6379
  12. environment:
  13. TZ: Asia/Shanghai
  14. volumes:
  15. - /data/redis/data:/data
  16. - /data/redis/conf/sysctl.conf:/etc/sysctl.conf
  17. - /data/redis/conf/redis.conf:/etc/redis/redis.conf
  18. - /data/redis/logs:/logs
  19. command: redis-server /etc/redis/redis.conf --requirepass linuxRedis
  20. networks:
  21. - bb
  22. mysql:
  23. user: root
  24. image: mysql
  25. restart: always
  26. privileged: true
  27. container_name: mysql
  28. environment:
  29. MYSQL_ROOT_PASSWORD: admin
  30. TZ: Asia/Shanghai
  31. ports:
  32. - 3306:3306
  33. volumes:
  34. - /data/mysql/data:/var/lib/mysql
  35. - /data/mysql/config/my.cnf:/etc/mysql/my.cnf
  36. command:
  37. --max_connections=30
  38. --character-set-server=utf8mb4
  39. --collation-server=utf8mb4_general_ci
  40. --default-authentication-plugin=mysql_native_password
  41. networks:
  42. - bb
  43. networks:
  44. bb:
  45. external: true

新建 Makefile 文件保存下面内容

  1. install:
  2. mkdir -p /data/redis/conf
  3. mkdir -p /data/redis/logs
  4. mkdir -p /data/redis/data:/data
  5. mkdir -p /data/mysql/data
  6. mkdir -p /data/mysql/config
  7. cp my.cnf /data/mysql/config/my.cnf

然后,运行它,然后就是静静地等待完成…

  1. make

声明: 因编程语言版本更新较快,当前文章所涉及的语法或某些特性相关的信息并不一定完全适用于您当前所使用的版本,请仔细甄别。文章内容仅作为学习和参考,若有错误,欢迎指正。

讨论 支持 Markdown 语法 点击演示
回复
评论预览框
#1 pyweeX 09-25 golang-pyweeX
  1. {
  2. "exec-opts": ["native.cgroupdriveer=systemd"],
  3. "registry-mirrors": ["https://ud6340vz.mirror.aliyuncs.com", "https://harbor.bluettipower.com"],
  4. "insecure-registries": ["192.168.23.70:8011"]
  5. }

回复 赞(0)

开发者

开发者·注册登录
  • 获取验证码
  • 取消