Loading... # 【Docker系列】不用宝塔面板,小白一样可以玩转VPS服务器! 转载自[咕咕鸽](https://blog.laoda.de/archives/hello-docker#5%E5%AE%89%E8%A3%85dockerdocker-compose) 12/05 **温馨提示:** 本文最后更新于 2023-01-10,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。 ## 前言 众所周知,[宝塔面板](https://loll.cc/btpanel)的出现,给了很多非计算机专业的小伙伴们一个可以相对轻松搭建自己网站的机会。 不过,由于宝塔最近加入了强制手机登陆验证(虽然可以用方法取消:[宝塔去除登陆](https://blog.laoda.de/archives/useful-script#%E5%AE%9D%E5%A1%94%E7%9B%B8%E5%85%B3),宝塔海外版本aapanel也不需要手机验证),还是让一些小伙伴觉得很不爽,装了宝塔之后,总是感觉有一双眼睛盯着自己,自己服务器上的数据随时在被宝塔监控着(宝塔是开源的,不过如果你做了一些奇怪的事情,宝塔不一定不会上报你的数据信息) 我们自建服务器图啥?图的不就是自由和隐私么?绝对不允许这样的情况出现! 不过,不用面板工具,我们难道要手动自己去下载配置LNMP环境么?好像有点复杂…… 确实,我自己尝试手动,有点繁琐,市面上也有辅助配置的,比如[**LNMP一键安装包**](https://lnmp.org/),不过后续的维护升级似乎也有些麻烦,对于计算机专业的同学来讲,可以试一试,对于我们这些非计算机专业的小伙伴,其实就不是非常必要了,我们可以尝试换一个选择——用Docker。 就我目前的体验来看,服务器上安装好Docker,利用Docker镜像已经可以部署我的90%小项目了。由于我的服务器比较多,所以我选择一台服务器安装aapanel(宝塔国际版)备用(针对个别docker无法实现的项目),另一台服务器纯原生环境+Docker来折腾。 所以接下来的一段时间,就让我们一起用Docker来玩一玩服务器吧,折腾起来! <iframe src="https://player.bilibili.com/player.html?aid=209697094&bvid=BV1kh411x7RL&cid=455307406&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe> ## 操作环境 * 服务器:~腾讯香港轻量应用服务器 24 元 / 月 VPS 一台~ 咕咕自己搭建用的是[腾讯轻量应用服务器](https://loll.cc/tx) ,(最好是选 **非大陆(香港)的服务器** )如果是小白刚开始玩的话,还可以购买 [Racknerd](https://blog.laoda.de/archives/cheap-vps-racknerd) 的服务器(地区选美西),最低一年不到 100 块(Racknerd 服务器介绍:[点击查看](https://blog.laoda.de/archives/cheap-vps-racknerd)) * 系统:Debian10或以上(Ubuntu 20.04以上) > 不建议用CentOS,具体原因请见:[解释使用Debian而不是CentOS的原因](https://blog.laoda.de/archives/getting-started-with-linux#0-%E5%89%8D%E8%A8%80%E5%90%90%E6%A7%BDcentos) ## DD一个纯净的系统(非必需) 腾讯云轻量服务器本身帮小白用户做了一些基础的防护工作(如默认禁止密码登陆、默认禁止root登陆等),其实没必要DD。 当然,也有不少有“洁癖”的小伙伴们,嫌弃系统上装有的监控软件,那么可以做以下的操作,给自己一个纯净的原生操作系统~ <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> -i <span class="token comment">#切换到root用户</span> <span class="token function">sudo</span> <span class="token function">apt</span> update -y <span class="token comment">#更新一下包</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ### 脚本一 **腾讯云轻量非大陆机器建议用这个:** <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">wget</span> --no-check-certificate -O AutoReinstall.sh https://git.io/AutoReinstall.sh <span class="token operator">&&</span> <span class="token function">bash</span> AutoReinstall.sh <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> * CentOS默认密码Pwd@CentOS 其它系统Pwd@Linux * OpenVZ / LXC 架构系统不适用 来源:[https://blog.hicasper.com/post/135.html](https://blog.hicasper.com/post/135.html) 参考:[https://www.ydyno.com/archives/1245.html](https://www.ydyno.com/archives/1245.html) 感谢大佬的付出! ### 脚本二 **这个有时候会失效:** 以下命令默认是Debian10,`自定义root密码`和`自定义ssh端口`自己修改成你自己想要的。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">bash</span> <span class="token operator"><</span><span class="token punctuation">(</span><span class="token function">wget</span> --no-check-certificate -qO- <span class="token string">'https://raw.githubusercontent.com/MoeClub/Note/master/InstallNET.sh'</span><span class="token punctuation">)</span> -d <span class="token number">10</span> -v <span class="token number">64</span> -p <span class="token string">"自定义root密码"</span> -port <span class="token string">"自定义ssh端口"</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 等待`15`到`30`分钟左右,连接即可,也可以自己ping一下域名,看看通不通,然后尝试连接。 **其他参数** > -d 10 -v 64 > -d 9 -v 64 > -u 20.04 -v 64 > -u 18.04 -v 64 ### **具体参考** [# 整理一些常用的脚本(持续更新中)](https://blog.laoda.de/archives/useful-script#dd%E7%9B%B8%E5%85%B3) ### 重要提醒!!! * 请仔细阅读服务器商家的ToS条款,事先确认你的服务器提供商是否支持你DD系统(重装自己的系统) * 由于授权问题,很多服务器提供商是禁止你把服务器DD成Windows系统的(比如Contabo),发现会暂停服务甚至删鸡! * OpenVZ / LXC 架构系统不适用此脚本 * **注意重装有风险,请妥善备份好自己的数据** ,(阿里腾讯搬瓦工等有快照的商家,你可以先在后台存一个快照)可能导致无法开机(部分商家可以用VNC救回来,但本文不涉及), **谨慎操作** ! 详情可见:[# 服务器自带的系统不喜欢?那就自己DD一个船新的系统吧!](https://blog.laoda.de/archives/dd) ## 1、BBR 新的 TCP 拥塞控制算法 `BBR (Bottleneck Bandwidth and RTT)` 可以让服务器的带宽尽量跑满,并且尽量不要有排队的情况,让网络服务更佳稳定和高效。 Linux Kernel 内核升级到 `4.9` 及以上版本可以实现 BBR 加速(Ubuntu 18.04 默认的内核是 `4.15`版本的内核,Ubuntu 20.04 默认的内核是 `5.4` 版本的内核,并已经默认编译了 TCP BBR 模块,可以直接通过参数开启)。 目前 Debian 10 自带的内核版本一般是`4.19`,我们可以查看一下内核版本(如果是 `5.6` 及以上内核则集成了 WireGuard ) <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">hostnamectl <span class="token comment">#或者 uname -r</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205141002084](https://pic.loll.cc/images/2021/12/05/202112051410227.png)我们选择更新到新的稳定版本的`5.1`内核(目前为止),再开启`BBR`。 1. 给 Debian 10 添加官方 `backports` 源,获取更新的软件库 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">nano</span> /etc/apt/sources.list <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> **说明** > 本文以 Debian 10 为例,所以使用 `/etc/apt/sources.list` 仍无问题,但如果你并不是根据本文从头开始,或者使用了其他 Linux 发行版,那么建议你建立 `/etc/apt/sources.list.d/` 文件夹,并在这个文件夹内建立自己的配置文件,形如 `/etc/apt/sources.list.d/vpsadmin.list` ,以此保证兼容性,也可避免默认文件在不可预见的情况下被覆盖而导致配置丢失。 2. 然后把下面这一条加在最后,并保存退出。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">deb http://deb.debian.org/debian buster-backports main <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![](https://pic.loll.cc/images/2021/12/05/202112051411364.png)3. 刷新软件库并查询 Debian 官方的最新版内核并安装。请务必安装你的 VPS 对应的版本(本文以比较常见的【amd64】为例)。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt</span> update <span class="token operator">&&</span> <span class="token function">sudo</span> <span class="token function">apt</span> -t buster-backports <span class="token function">install</span> linux-image-amd64 <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> **注意** > 如果你的 VPS 支持,可以尝试【云服务器专用内核】`linux-image-cloud-amd64`,优点就是精简、资源占用低,缺点嘛是有同学反馈不支持的系统强行安装会导致无法开机(Kernel 无法识别)。 > > 为了避免无法识别的悲剧,请确保: > > * 尝试前做一个系统快照,或者 > * 你有 `vnc` 可以救场(并且你知道怎么用) 4. 修改 `kernel` 参数配置文件 `sysctl.conf` 并指定开启 `BBR` <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">nano</span> /etc/sysctl.conf <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> **说明** > 本文以 Debian 10 为例,所以使用 `/etc/sysctl.conf` 仍无问题,但如果你并不是跟着本文从头开始,或者使用了其他 Linux 发行版,那么建议你建立 `/etc/sysctl.d/` 文件夹,并在这个文件夹内建立自己的配置文件,形如 `/etc/sysctl.d/vpsadmin.conf`,以此保证兼容性,因为部分发行版在 `systemd` 207 版本之后便不再从 `/etc/sysctl.conf` 读取参数。使用自定义配置文件也可避免默认文件在不可预见的情况下被覆盖而导致配置丢失。 5. 把下面的内容添加进去 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-text line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-text">net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 6. 重启 VPS、使内核更新和`BBR`设置都生效 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">reboot</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> **啰嗦君** > 因为我做展示的 VPS 支持云服务器专用内核,所以动图中我用了 `linux-image-cloud-amd64` 。如果你不确定你的 VPS 是否支持,那请务必按照第 3 步的命令,使用常规内核 `linux-image-amd64`。 ![http自动跳转https](https://pic.loll.cc/images/2021/12/05/202112051428670.gif)8. 确认`BBR`开启 如果你想确认 `BBR` 是否正确开启,可以使用下面的命令: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">lsmod <span class="token operator">|</span> <span class="token function">grep</span> bbr <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 此时应该返回这样的结果: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-text line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-text">tcp_bbr <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 如果你想确认 `fq` 算法是否正确开启,可以使用下面的命令: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">lsmod <span class="token operator">|</span> <span class="token function">grep</span> fq <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 此时应该返回这样的结果: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-text line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-text">sch_fq <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">uname</span> -r <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205143330029](https://pic.loll.cc/images/2021/12/05/202112051433195.png)内核版本已经变成了`5.10` ## 2、修改时间 首先,我们需要修改服务器的时间与我们本地的时间一致。 香港的服务器的话时间是一样的,就无所谓了,如果是欧美的服务器,可以通过下面的方式修改~ <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> -i <span class="token comment">#切换到root用户</span> <span class="token function">apt</span> update -y <span class="token operator">&&</span> <span class="token function">apt</span> upgrade -y <span class="token comment">#更新一下包</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 先查看时间: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">timedatectl <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205135246536](https://pic.loll.cc/images/2021/12/05/202112051352730.png)DD完之后,可以看到我们的时间变成了纽约时间,这边需要改一下。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">timedatectl list-timezones <span class="token comment">#列出所有时区</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205135432441](https://pic.loll.cc/images/2021/12/05/202112051354572.png) <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> timedatectl set-timezone Asia/Shanghai <span class="token comment">#改成上海</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205135502989](https://pic.loll.cc/images/2021/12/05/202112051355078.png)发现没有sudo命令,这是因为我们的系统现在是纯净的,没有装其他的东西, ![image-20211205135539839](https://pic.loll.cc/images/2021/12/05/202112051355914.png)这边我们手动安装几个常用的命令~ <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">apt</span> <span class="token function">install</span> <span class="token function">sudo</span> <span class="token function">curl</span> <span class="token function">wget</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 然后重新运行 ![image-20211205135742888](https://pic.loll.cc/images/2021/12/05/202112051357011.png)## 3、添加SWAP swap是Linux中的虚拟内存,用于扩充物理内存不足而用来存储临时数据存在的。它类似于Windows中的虚拟内存。在Windows中,只可以使用文件来当作虚拟内存。而linux可以文件或者分区来当作虚拟内存。 这个虚拟内存对于内存小的VPS非常有必要,可以提高我们的运行效率。 这里我们用脚本来添加。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">wget</span> -O box.sh https://raw.githubusercontent.com/BlueSkyXN/SKY-BOX/main/box.sh <span class="token operator">&&</span> <span class="token function">chmod</span> +x box.sh <span class="token operator">&&</span> <span class="token function">clear</span> <span class="token operator">&&</span> ./box.sh <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 更多常用脚本,可以看这篇:[整理一些常用的脚本(持续更新中)](https://blog.laoda.de/archives/useful-script) ![image-20211205140547106](https://pic.loll.cc/images/2021/12/05/202112051405244.png)这边建议是`2`倍,我添加了1倍,也就是1024MB ![image-20211205140623294](https://pic.loll.cc/images/2021/12/05/202112051406440.png)## 4、其他服务器安全相关的工作 其他安全防护工作,可以看这篇:[保护好你的小鸡!保姆级服务器安全教程!](https://blog.laoda.de/archives/how-to-secure-a-linux-server) 安全无小事,建议大家保护好自己的小鸡! ## 5、安装Docker、Docker-compose ### 更新、安装必备软件 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">apt-get</span> update <span class="token operator">&&</span> <span class="token function">apt-get</span> <span class="token function">install</span> -y <span class="token function">wget</span> <span class="token function">vim</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ### 非大陆Docker安装 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">wget</span> -qO- get.docker.com <span class="token operator">|</span> <span class="token function">bash</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 注意:出现`sudo: unable to resolve host [hostname]`,可以编辑`nano /etc/hosts`然后在 ![image-20211205144134019](https://pic.loll.cc/images/2021/12/05/202112051441164.png)localhost后面空一格,再打上`[hostname]`你对应的hostname内容。 ### 查看Docker版本 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">docker</span> -v <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ### 开机自动启动 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">systemctl <span class="token builtin class-name">enable</span> <span class="token function">docker</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ### 卸载Docker <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> purge docker-ce docker-ce-cli containerd.io <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">rm</span> -rf /var/lib/docker <span class="token function">sudo</span> <span class="token function">rm</span> -rf /var/lib/containerd <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ### 非大陆Docker-compose安装 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">curl</span> -L <span class="token string">"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-<span class="token variable"><span class="token variable">$(</span><span class="token function">uname</span> -s<span class="token variable">)</span></span>-<span class="token variable"><span class="token variable">$(</span><span class="token function">uname</span> -m<span class="token variable">)</span></span>"</span> -o /usr/local/bin/docker-compose <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">sudo</span> <span class="token function">chmod</span> +x /usr/local/bin/docker-compose <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">docker-compose</span> --version <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205144336324](https://pic.loll.cc/images/2021/12/05/202112051443430.png)大陆内地的服务器安装请见这边:[国内机安装docker](https://blog.laoda.de/archives/useful-script#docker%E7%9B%B8%E5%85%B3) ### 修改Docker配置(来自[烧饼博客](https://u.sb/debian-install-docker/)) 以下配置会增加一段自定义内网 IPv6 地址,开启容器的 IPv6 功能,以及限制日志文件大小,防止 Docker 日志塞满硬盘(泪的教训): <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">cat</span> <span class="token operator">></span> /etc/docker/daemon.json <span class="token operator"><<</span><span class="token string">EOF { "log-driver": "json-file", "log-opts": { "max-size": "20m", "max-file": "3" }, "ipv6": true, "fixed-cidr-v6": "fd00:dead:beef:c0::/80", "experimental":true, "ip6tables":true } EOF</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 然后重启 Docker 服务: <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash">systemctl restart <span class="token function">docker</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ## 6、文件管理 强烈建议大家专门给Docker的数据、配置文件新建一个文件夹, <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">mkdir</span> -p data/docker_data <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 这样如果换服务器或者想要备份,都会很方便。 ![image-20211205144543041](https://pic.loll.cc/images/2021/12/05/202112051445178.png)![image-20211205144822664](https://pic.loll.cc/images/2021/12/05/202112051448803.png)![image-20211205145020493](https://pic.loll.cc/images/2021/12/05/202112051450650.png)这边我们以之前搭建过的minimalist为例子[【好玩的Docker项目】搭建一个简洁的记事本——minimalist-web-notepad](https://blog.laoda.de/archives/docker-install-minimalist) 来简单讲讲。 首先进入我们创建好的文件夹下, ![image-20211205145334845](https://pic.loll.cc/images/2021/12/05/202112051453987.png)运行, <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">wget</span> https://github.com/pereorga/minimalist-web-notepad/archive/refs/heads/docker.zip <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205145659238](https://pic.loll.cc/images/2021/12/05/202112051456373.png)然后 ![image-20211205145715475](https://pic.loll.cc/images/2021/12/05/202112051457527.png)没有unzip,我们就安装一个 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">apt</span> <span class="token function">install</span> <span class="token function">unzip</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 再运行 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-none line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language- language-none">unzip docker.zip <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> ![image-20211205145834093](https://pic.loll.cc/images/2021/12/05/202112051458203.png) <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token builtin class-name">cd</span> minimalist-web-notepad-docker/ <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 建立镜像, <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">docker</span> build -t minimalist-web-notepad <span class="token builtin class-name">.</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 安装minimalist, <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">docker</span> run -d -it --restart<span class="token operator">=</span>always --name minimalist-web-notepad -v /root/data/docker_data/minimalist/minimalist-data:/var/www/html/_tmp -p <span class="token number">8006</span>:80 minimalist-web-notepad <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 注意这里的映射路径,我们改成了刚前面创建的路径`/root/data/docker_data/minimalist` 这样你的东西就都在这边啦。 ![image-20211205150144574](https://pic.loll.cc/images/2021/12/05/202112051501709.png) 一般来说,看到这一长串的东西,就说明创建成功啦。 由于Debian是本身开放所有端口的,所以直接访问即可,但是因为我们是腾讯云轻量服务器,后台默认还有一个防火墙,我们需要登陆把后台的防火墙里的`8006`端口打开(对应docker命令的`8006:80`),这样就能访问了。 ![image-20211205150839451](https://pic.loll.cc/images/2021/12/05/202112051508602.png)![image-20211205150902745](https://pic.loll.cc/images/2021/12/05/202112051509814.png)![image-20211205150938302](https://pic.loll.cc/images/2021/12/05/202112051509410.png)当然你要是觉得麻烦,也可以直接填`ALL`,这样就开放所有端口了,以后就不用每次来设置了,但是不推荐。 ![image-20211205151012846](https://pic.loll.cc/images/2021/12/05/202112051510917.png)然后我还非常推荐你再建立一个`txt`文件,记录这条docker命令。 <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">nano</span> config.txt <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> 然后输入前面的命令, <pre class="c_title c_hr c_macdot c_hover_tools c_expander c_copy language-bash line-numbers" tabindex="0"><i class="joe-font joe-icon-arrow-downb code-expander" title="折叠/展开"></i><code class="language-bash"><span class="token function">docker</span> run -d -it --restart<span class="token operator">=</span>always --name minimalist-web-notepad -v /root/data/docker_data/minimalist/minimalist-data:/var/www/html/_tmp -p <span class="token number">8006</span>:80 minimalist-web-notepad <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code><span class="copy-button"><i class="joe-font joe-icon-copy" title="复制代码"></i></span></pre> `Ctrl+x`退出,按`y`确认,回车。 ![image-20211205150658729](https://pic.loll.cc/images/2021/12/05/202112051506899.png)用IP+端口就可以访问了! 问题来了,之前我们都是通过宝塔面板上面建立一个空壳站点,然后修改配置文件来进行反向代理的,这下没有了宝塔面板,要怎么搞? 难道要手动去修改nginx的配置文件吗? 我还得安装一个nginx?感觉好麻烦啊! ![image-20211205151425693](https://pic.loll.cc/images/2021/12/05/202112051514859.png) 不!下期将介绍一个非常牛逼的工具,可以让你只要点击几下鼠标就能非常轻松完成反向代理,当然它自身也是基于Docker搭建的 ^ ^ 所以暂时先憋着,丑就丑一点,我们下期视频来处理这个事情。 ## 7、备份数据 参考这篇:[如何备份Docker数据?(以Bitwarden为例)](https://blog.laoda.de/archives/backup-docker) ### 附 如果还是放不下宝塔面板(或者担心以后会用到宝塔)的话,可以在第一部分BBR开启完毕之后,到宝塔官网下载宝塔(宝塔安装需要服务器是干净的没有安装过别的软件),安装宝塔完成之后(取消登陆框可以看这个:[宝塔去除登陆](https://blog.laoda.de/archives/useful-script#%E5%AE%9D%E5%A1%94%E7%9B%B8%E5%85%B3)),修改时间和添加SWAP可以在软件商店搜索Linux工具箱,利用工具箱来修改。之后安装Docker可以选择用宝塔来安装,也可以选择按上文的步骤来安装。 最后修改:2023 年 02 月 04 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏