一步两步是魔鬼的步伐

Duan1v's Blog

使用Hugo自搭博客系列四:部署

手动部署

参考github page

  • 注意,git仓库需要是public的;下面自动的不用

自动部署

Travis CI官网

Netlify官网

Vercel

AWS Amplify

Netlify

参考Netlify部署Hugo个人博客Hugo Algolia搜索及Netlify自动化处理 ,这位博主写的很详细了,我就不写了。

AWS Amplify

  • 我目前换成了这个来部署,之前使用的Netlify,总是间歇性卡顿,很卡的那种

  • 关于亚马逊云的使用还是有点麻烦的,亚马逊中国只支持企业,个人体验需要用注册海外账户

https://static.duan1v.top/images/2b7b0f90a0ee37d6cc115e541fdc18ba.png
博客自动部署
  • 相比于Netlify,直接使用的不是extended,这会导致样式加载不生效,可以使用下面贴的 amplify.yml

  • 其他的环境变量和上面的Netlify差不多,将 HUGO_VERSION 改为 VERSION_HUGO

Vercel

  • AWS Amplify的工作人员打电话给我,说服务都是免费一年的;我心想,我可是想着白嫖永久的;好吧,那就清理资源,销号

  • 转头Vercel,折腾过上面两个之后,也很简单;

  • 目前我的图床、评论、博客都是放在github上的,由Vercel部署的

贴下几个文件

  • 根目录下的.gitignore
1
2
3
4
.env
node_modules
.idea
.vscode
  • 根目录下的netlify.toml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[build]
publish = "public"
command = "hugo --gc --minify --theme=even && npm install atomic-algolia --save && npm run algolia"

[context.production.environment]
HUGO_VERSION = "0.101.0"
HUGO_ENV = "production"
HUGO_ENABLEGITINFO = "true"

[context.split1]
command = "hugo --gc --minify --enableGitInfo"

[context.split1.environment]
HUGO_VERSION = "0.101.0"
HUGO_ENV = "production"

[context.deploy-preview]
command = "hugo --gc --minify --buildFuture -b $DEPLOY_PRIME_URL"

[context.deploy-preview.environment]
HUGO_VERSION = "0.101.0"

[context.branch-deploy]
command = "hugo --gc --minify -b $DEPLOY_PRIME_URL"

[context.branch-deploy.environment]
HUGO_VERSION = "0.101.0"

[context.next.environment]
HUGO_ENABLEGITINFO = "true"

[[redirects]]
from = "/npmjs/*"
to = "/npmjs/"
status = 200
  • public目录下的.gitignore
1
2
*
!.gitignore
  • amplify.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: 1
frontend:
  phases:
    preBuild:
      commands:
        - wget https://github.com/gohugoio/hugo/releases/download/v${VERSION_HUGO}/hugo_extended_${VERSION_HUGO}_Linux-64bit.tar.gz
        - tar --overwrite -xf hugo_extended_${VERSION_HUGO}_Linux-64bit.tar.gz hugo
        - mv hugo /usr/bin/hugo
        - rm -rf hugo_extended_${VERSION_HUGO}_Linux-64bit.tar.gz
        - hugo version
        - hugo --gc --minify --buildFuture 
        - npm install atomic-algolia --save 
    build:
      commands:
        - hugo --gc --minify --theme=loveit --enableGitInfo=false 
        - npm run algolia
  artifacts:
    baseDirectory: public
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

使用Hugo自搭博客系列三:algolia搜索

algolia

预备工作

https://static.duan1v.top/images/2knoef1Q3RBPt7g.png

本地项目创建/修改文件

添加前端UI

  • layouts/partials/head.html添加
1
2
3
{{- if .Site.Params.algolia.appId -}}
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
{{- end -}}
  • layouts/partials/scripts.html添加
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{{- if .Site.Params.algolia.appId -}}
  <script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
  <script>
  docsearch({
      apiKey: {{ .Site.Params.algolia.searchOnlyKey }},
      indexName: {{ .Site.Params.algolia.indexName }},
      appId: {{ .Site.Params.algolia.appId }},
      inputSelector: '.docsearch-input',
      debug: false,
  });
  docsearch({
      apiKey: {{ .Site.Params.algolia.searchOnlyKey }},
      indexName: {{ .Site.Params.algolia.indexName }},
      appId: {{ .Site.Params.algolia.appId }},
      inputSelector: '.mob-docsearch-input',
      debug: false,
  });
  </script>
{{- end -}}
  • layouts/partials/header.html , menu那里添加
1
2
3
4
5
    {{- if .Site.Params.algolia.appId -}}
      <li style="display:inline-block;margin-right:10px;">
        <input type="search" class="docsearch-input" placeholder="Search" />
      </li>
    {{- end -}}
  • layouts/partials/slideout.html, menu那里添加
1
2
3
4
5
    {{- if .Site.Params.algolia.appId -}}
      <li style="display:inline-block;margin-right:10px;">
          <input type="search" class="mob-docsearch-input" placeholder="Search" />
      </li>
    {{- end -}} 
  • assets/sass/_partial/_slideout.scss 修改
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.slideout-menu {
  position: fixed;
  top: 0;
  left: 0px;
  bottom: 0;
  width: 180px;
  min-height: 100vh;
  overflow: visible;
  -webkit-overflow-scrolling: touch;
  z-index: 2;
  display: none;

  .language-selector {
    padding-left: 30px;
  }
}

// 这里新增
@media screen and (max-width: 500px) {
    .algolia-autocomplete .ds-dropdown-menu {
        min-width: 99vw;
    }
}

layouts_default\list.algolia.json

1
2
3
4
5
6
7
8
9
{{/* 生成Algolia搜索索引文件 */}}
{{- $.Scratch.Add "index" slice -}}
{{/* content/posts或content/post目录下的博文才生成索引 */}}
{{- range where (where .Site.Pages "Type" "in" (slice "posts" "post")) "IsPage" true -}}
  {{- if and (not .Draft) (not .Params.private) -}}
    {{- $.Scratch.Add "index" (dict "objectID" .File.UniqueID "url" .Permalink "content" (.Summary | plainify) "tags" .Params.Tags "lvl0" .Title "lvl1" .Params.Categories "lvl2" "摘要") -}}
  {{- end -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

修改 config.toml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[outputs]
    home = ["HTML", "RSS", "Algolia"]

[outputFormats.Algolia]
  baseName = "algolia"
  isPlainText = true
  mediaType = "application/json"
  notAlternative = true
  
[params.algolia]
  appId = "填下" # 是id
  indexName = "填下"
  searchOnlyKey = "填下"

根目录执行 hugo 命令之后,在 public 目录下就会生成 algolia.json 文件

上传索引文件

手动

  • 将生成的索引文件algolia.json,上传到Algolia的服务器。

https://static.duan1v.top/images/vcj7CB4FMYPrODu.png

自动

  • 安装atomic-algolia
1
2
npm init
npm install atomic-algolia --save
  • 修改目录下的 package.json
1
2
3
4
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "algolia": "atomic-algolia"
  },
  • 根目录下新建 .env 文件
1
2
3
ALGOLIA_APP_ID=你的Application ID # 注意是ID
ALGOLIA_INDEX_NAME=你的索引名字 ALGOLIA_INDEX_FILE=public/algolia.json 
ALGOLIA_ADMIN_KEY=你的Admin API Key
  • 执行上传命令
1
npm run algolia

本文参考:

使用Hugo自搭博客系列二:评论等一些配置

使用valine

1
2
3
4
5
6
[params.valine]
    enable = true
    appId = '填下'
    appKey = '填下'
    placeholder = '说点什么吧...'
    visitor = true # 浏览人数

使用Disqus

1
disqusShortname = "your disqus_shortname"
  • 注:若要本地也可以显示评论,需要修改themes\even\layouts\partials\comments.html
1
// if (window.location.hostname === 'localhost') return;

使用Waline

其他配置

配置下作者

1
2
[author]                  # essential                     # 必需
  name = "来个名字"

卜算子 (统计pv|uv)

  • 自带的有bug,我是关闭了
1
2
3
4
5
  [params.busuanzi]         # count web traffic by busuanzi # 是否使用不蒜子统计站点访问量
    enable = false
    siteUV = true
    sitePV = true
    pagePV = true
1
cp themes/loveit/layouts/partials/footer.html layouts/partials/footer.html

layouts/partials/footer.html<div class="footer-container"> 中添加:

1
2
3
4
<div class="footer-line">
    <i class='fas fa-chalkboard'></i>PV:<span id="busuanzi_value_site_pv"></span> | <i class='fas fa-chalkboard-teacher'></i>UV:<span id="busuanzi_value_site_uv"></span>
    <script async src="https://cdn.dusays.com/bsz.js"></script>
</div>

底部的社交链接

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  [params.social]                                         # 社交链接
    a-email = "mailto:dsdsa123@gmail.com"
    # b-stack-overflow = "http://localhost:1313"
    # c-twitter = "http://localhost:1313"
    # d-facebook = "http://localhost:1313"
    # e-linkedin = "http://localhost:1313"
    # f-google = "http://localhost:1313"
    g-github = "https://github.com/dsada123"
    # h-weibo = "http://localhost:1313"
    # i-zhihu = "http://localhost:1313"
    # j-douban = "http://localhost:1313"
    # k-pocket = "http://localhost:1313"
    # l-tumblr = "http://localhost:1313"
    # m-instagram = "http://localhost:1313"
    # n-gitlab = "http://localhost:1313"
    # o-bilibili = "http://localhost:1313"

网站favicon.ico

  • 替换themes\even\static\favicon.ico,还有其他几个icon

  • 执行hugo

  • 懒人可以使用 Logo神器

引入音乐播放器(LoveIt主题)

  • 配置文件修改(本地查看效果需要,线上不需要)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    [params.page.library]
      [params.page.library.css]
        # someCSS = "some.css"
        # located in "assets/" 位于 "assets/"
        # Or 或者
        # someCSS = "https://cdn.example.com/some.css"
        "APlayerCSS" = "https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css"
      [params.page.library.js]
        # someJavascript = "some.js"
        # located in "assets/" 位于 "assets/"
        # Or 或者
        # someJavascript = "https://cdn.example.com/some.js"
        "APlayerJS" = "https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"
        "MetingJS" = "https://cdn.jsdelivr.net/npm/meting@2.0.1/dist/Meting.min.js"
  • 在需要的地方引入短标签
1
{{< music url="/music/Wavelength.mp3" name="Wavelength" artist="oldmanyoung" cover="/images/Wavelength.jpg" fixed="false" >}}

效果:

  • 也可以自定义一个layout,放在footer里,全局引用,不过这不是单页面的,所以切换页面会暂停播放
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<meting-js
    name="rainymood"
    artist="rainymood"
    url="/music/Wavelength.mp3"
    cover="/images/Wavelength.jpg"
    mini="true"
    fixed="true">
    <pre hidden>
        [00:00.00]This
        [00:04.01]is
        [00:08.02]lyric
    </pre>
</meting-js>

加密,这里以 posts 下面的文章加密为例(LoveIt主题)

  • 可防JS禁用 ,可cookie暂储密码

  • 新建 layouts/posts/single.html

1
mkdir layouts/posts/ && cp themes/LoveIt/layouts/posts/single.html layouts/posts/single.html
  • 创建文件 static/js/pwd.js ,代码如下
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

function checkCookie(cname, cvvalue) {
    var cvalue = getCookie(cname);
    if (cvalue != "" && cvalue === cvvalue) {
        return true;
    } else {
        return false;
    }
}

function setCookie(cname, cvalue, exhours) {
    var d = new Date();
    var h = 60 * 60 * 1000
    d.setTime(d.getTime() + (exhours * h));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}

function errorPwd() {
    alert('密码错误!');
    if (history.length === 1) {
        window.opener = null;
        window.open('', '_self');
        window.close();
    } else {
        history.back();
    }
    return false;
}
  • layouts/posts/single.html 中的 {{- $params := .Scratch.Get "params" -}} 之后添加如下代码
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    {{- $password := $params.password | default "" -}}
    {{- $encryptedKey := $params.encryptedKey | default "" -}}
    {{- if ne $password "" -}}
        <noscript><meta http-equiv="refresh" content="0; url=/404.html" /></noscript>
        <script type="text/javascript" src="/js/pwd.js"></script>
        <script>
            (function(){
                document.body.style.display="none"
                if({{ $password }}){
                    if({{ $encryptedKey }}){
                        if (!checkCookie({{ $encryptedKey }},{{ $password }})) {
                            if (prompt('请输入文章密码') != {{ $password }}){
                                return errorPwd();
                            } else {
                                setCookie({{ $encryptedKey }},{{ $password }},1);
                            }
                        }
                    } else if (prompt('请输入文章密码') != {{ $password }}){
                        return errorPwd();
                    }
                }
                document.body.style.display="inherit"
            })();
        </script>
    {{- end -}}
  • posts下面 的文章中使用 : encryptedKey => cookie中的键;password => 密码
1
2
3
4
5
---
encryptedKey: "gsgds"
password: "dsada"

---

使用Hugo自搭博客系列一:安装

安装:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
➜  ~ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal
➜  ~ arch
x86_64
➜  ~ go version
go version go1.18.3 linux/amd64
  • 安装
1
sudo apt-get install hugo

或者 直接下载bin文件,然后解压到/usr/local/bin

1
sudo tar -zxvf /mnt/d/Ubuntu/hugo_extended_0.101.0_Linux-64bit.tar.gz -C /usr/local/bin
  • 查看版本(注意:非extended版本不支持编译scss)
1
2
➜  ~ hugo version
hugo v0.101.0-466fa43c16709b4483689930a4f9ac8add5c9f66+extended linux/amd64 BuildDate=2022-06-16T07:09:16Z VendorInfo=gohugoio

生成网站

  • 在磁盘中找一块风水宝地,开启自搭博客之旅
1
hugo new site my_blog

来个主题

我选的是even主题

1
2
3
4
5
6
cd my_blog
git clone https://github.com/olOwOlo/hugo-theme-even themes/even
rm -rf themes/even/.git
mv themes/even/exampleSite/content/* content/
mv themes/even/exampleSite/config.toml ./
hugo server --theme=even --buildDrafts -D

打开 http://localhost:1313/

可参考文档进行配置config.toml

启动本地服务可能会有警告

1
2
# google_news internal template should be deprecated...
https://github.com/gohugoio/hugo/issues/9172
  • 可以删除掉themes\even\layouts\partials\head.html中的下面代码:
1
{{- template "_internal/google_news.html" . -}}

手机访问wsl2网站

查看wsl2的ip

1
-> % ifconfig

https://static.duan1v.top/images/HrVaUNqWIzm5AFw.png

开启服务,如nginx(配置端口为非80端口)

  • 确保 wsl2的ip:nginx配置中的端口 可以访问网站

cmd 获取主机ip

1
C:\WINDOWS\system32>ipconfig

https://static.duan1v.top/images/AZ56wFbKtrqzRlQ.png

cmd 管理员权限

  • 映射
1
netsh interface portproxy add v4tov4 listenaddress=主机ip listenport=来个端口如8080 connectaddress=wsl2的ip connectport=nginx中配置的端口
  • 查看
1
netsh interface portproxy show all
  • 删除
1
netsh interface portproxy delete v4tov4 listenaddress=主机ip listenport=8080

防火墙新建端口规则:

  • 防火墙->高级设置->入站规则->新建规则,新建一个端口规则

https://static.duan1v.top/images/jDlInVhWBsQiq6b.png

电脑打开移动热点

https://static.duan1v.top/images/6AxiBaImfKcTVsu.png

手机连上移动热点

手机访问

主机ip:8080

docker 使用总结 win10

docker的数据位置及安装位置

  • docker默认的安装位置是在:“C:\Program Files\Docker”
  • 数据位置,包括镜像位置(wsl2)及docker桌面的位置是在:C:\Users\Administrator\AppData\Local\Docker
  • 由于docker所耗存储是大的,且随镜像的增多而增大,所以不能放在C盘,但是: 网上直接迁移docker使用的wsl2所在位置,或者直接软链数据位置(或\wsl\data):都将使docker desktop无法打开或闪退
  • 遂在安装之前,直接将上面两个目录都完整软链;然后再安装docker desktop
1
2
3
4
mklink /j "C:\Users\Administrator\AppData\Local\Docker" "F:\Docker"
mklink /j "C:\Program Files\Docker" "F:\Program Files\Docker"
mklink /j "C:\Users\Administrator\AppData\Roaming\Docker" "F:\Roaming\Docker"
mklink /j "C:\Users\Administrator\AppData\Roaming\Docker Desktop" "F:\Roaming\Docker Desktop"
  • 隔了几天,好像又闪退,无语
  • 感觉和电脑配置以及镜像空间占用大小有关系,面向玄学编程

mysql

  • powershell中拉镜像
1
docker pull mysql
  • 创建并启动容器
1
docker run -p 3307:3306 --name mysql --restart=always --privileged=true -v /g/docker/mysql/data:/var/lib/mysql -v /g/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=来个密码  -d mysql
  • 兼容之前版本的密码认证,需要进入容器,root登录mysql;做以下操作,主机客户端才能连接
1
2
3
4
5
6
CREATE USER '来个用户'@'%' IDENTIFIED BY '来个密码';
GRANT ALL ON *.* TO '上面的用户'@'%';
ALTER USER '上面的用户'@'%' IDENTIFIED BY '来个密码' PASSWORD EXPIRE NEVER;
# 可以修改密码
# ALTER USER '上面的用户'@'%' IDENTIFIED WITH mysql_native_password BY '换个密码';
flush privileges;
  • 配置文件 /g/docker/mysql/conf/my.cnf
1
2
3
4
5
6
chmod -R 644 /g/docker/mysql/conf/my.cnf

[mysqld]
max_connections=1600
secure_file_priv=/var/lib/mysql
skip_ssl

PS:如果出现下面这个错误,等一会就行

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

redis

  • 拉镜像
1
docker pull redis
  • 运行容器
1
docker run -p 6380:6379 --name redis --restart=always -v /g/docker/redis/conf:/etc/redis/redis.conf  -v /g/docker/redis/data:/data  -v /g/docker/redis/persist_data:/persist_data -d redis redis-server /etc/redis/redis.conf/redis.conf

确保主机中的挂载目录存在

redis-server 指定的是文件,而非目录

其中redis的配置G:\docker\redis\conf\redis.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
chmod -R 644 /g/docker/redis/conf/redis.conf

# 注释掉这部分,使redis可以外部访问;否则外部访问将报错EOF
# bind 127.0.0.1 
# 用守护线程的方式启动
daemonize no
# 给redis设置密码
requirepass q123456we
# AOF持久化开启,默认是no
appendonly yes
# 更改本地redis数据库存放文件夹(可选)
dir /persist_data
# 防止出现远程主机强迫关闭了一个现有的连接的错误,默认是300
tcp-keepalive 300 
# AOF持久化配置
appendfsync everysec
# RDB持久化关闭,默认是no
save ""

dockerfile 构建

目录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
➜  /workspace tree .
.
├── Dockerfile
├── download
│   ├── go1.18.3.linux-amd64.tar.gz
│   ├── protoc
│   │   ├── bin
│   │   ├── include
│   │   └── readme.txt
│   └── protoc.tar.gz
├── run.sh
└── zshrc_tmp

命令(注意最后的点)

1
docker build -f Dockerfile -t workspace:go .

dockerfile 部分代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FROM ubuntu:18.04

LABEL Author="yiwei.duan@outlook.com" Description="This image is ubuntu:20.04 with common software." Version="1.0"

WORKDIR /workspace

RUN echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list \
	&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list \
	&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list

RUN apt-get update \
        && apt-get install -y openssh-server \
	&& mkdir /var/run/sshd \
	&& echo 'root:来个密码' | chpasswd \
	&& echo "PermitRootLogin no " >> /etc/ssh/sshd_config \
	&& echo "PasswordAuthentication yes " >> /etc/ssh/sshd_config \
	&& echo "PubkeyAuthentication yes " >> /etc/ssh/sshd_config \
	&& echo "来个新用户名 ALL=(ALL) ALL" >> /etc/sudoers

ADD run.sh /
RUN chmod +x /run.sh
CMD ["/run.sh"]

TIPS:

出现 unexpected EOF,可能是电脑或者网络不太行,直接继续执行之前的命令。

如果,emmm,比如,A容器使用过了2222端口进行了ssh连接,但之后将A删除了,新建B容器,也使用2222端口,需要将~/.ssh/known_hosts中的那条 [127.0.0.1]:2222删除

dockerfile中使用CMD,如下,容器无法启动;但sh文件单独执行是成功的,这时将run.sh中最后追加 " su root “即可

1
2
3
ADD run.sh /
RUN chmod +x /run.sh
CMD ["/run.sh"]

dockerfile中无交互生成ssh,并将本地公钥写进autthorized_key

1
2
3
4
5
RUN ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa \
	&& touch ~/.ssh/authorized_keys \
	&& chmod 600 ~/.ssh/authorized_keys \
	&& chmod 700 ~/.ssh \
	&& echo "主机公钥,位置在~/.ssh/id_rsa.pub" >>  ~/.ssh/authorized_keys

dockerfile中生成登录git的ssh

1
2
3
RUN ssh-keygen -t rsa -C "github邮箱" -P "" -f ~/.ssh/docker_rsa \
	&& touch ~/.ssh/config \
	&& echo "Host github.com\n\tHostName github.com\n\tPreferredAuthentications publickey\n\tIdentityFile ~/.ssh/docker_rsa\n\tUser duan1v" >> ~/.ssh/config 
  • 到github中 New SSH key
  • 测试连接git
1
ssh -T git@github.com

主机使用vscode;配置~/.ssh/config

1
2
3
4
Host 来个名字
    HostName 127.0.0.1
    User docker用户名
    Port docker端口

dockerfile无交互创建新用户

1
2
3
RUN useradd 来个用户名 -m \
	&& echo "来个密码\n重复密码" | passwd 上面那个用户名 \
 	&& usermod -a -G 上面那个用户名,sudo 上面那个用户名 

docker 推送镜像

  • 先创建仓库,创建之后可以看到,仓库名 是以"docker用户名/“为前缀的,还有一部分是自定义的,记住这个仓库名;自定义部分可以取,需要推送的那个镜像名就好,不用加TAG

https://hub.docker.com/repositories

  • 主机powershell
1
2
3
PS C:\Users\Administrator\Desktop> docker login 
Authenticating with existing credentials...
Login Succeeded
  • 将需要上传的 镜像A:A的TAG 重新生成一个与仓库名同名的镜像
1
docker tag 镜像A:A的TAG 仓库名:A的TAG
  • 最后push一下
1
docker push 仓库名:A的TAG

容器使用主机的vpn,dockerfile中添加

1
2
ENV https_proxy="http://host.docker.internal:主机的代理端口" \
	http_proxy="http://host.docker.internal:主机的代理端口" \