0%

blog制作docker镜像记录

因为 blog 涉及的本地依赖过多, 特意放到 docker 上, 方便发布博客和移植。制作镜像的时候,尽量选择国外服务器,国内的服务器下载包很容易陷入死循环。

1. 宿主机准备

1.1 安装docker

1
2
apt update
apt install docker.io

1.2 宿主机开启镜像加速

image-20230909171630001

https://cr.console.aliyun.com/ 先给本机来个加速。

1
2
3
4
5
6
7
8
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2. 制作镜像

1.1 创建容器安装依赖

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
# 1. 创建容器
docker run -itd --name levon_blog_base ubuntu
docker exec -it levon_blog_base /bin/bash

# 2. 安装必要软件
apt update
apt install -y vim
apt install -y git
apt install -y curl


# 3. 安装blog 依赖
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
nvm install v14
nvm use 14
node -v

apt install -y npm
npm install hexo-cli -g


# 4. 配置git环境
git config --global core.quotepath false
git config --global gui.encoding utf-8
git config --global i18n.commit.encoding utf-8
git config --global i18n.logoutputencoding utf-8
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
export LESSCHARSET=utf-8

1.2 增加自己环境

1
2
git config --global user.email "levonfly@gmail.com"
git config --global user.name "unix2dos"
  • ssh/config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mkdir .ssh
vi ~/.ssh/config

Host coding e.coding.net
HostName e.coding.net
IdentityFile ~/.ssh/github-unix2dos
User levonfly
Host github.com
HostName github.com
IdentityFile ~/.ssh/github-unix2dos
User unix2dos
Host unix2dos
HostName github.com
IdentityFile ~/.ssh/github-unix2dos
User unix2dos
Host levonfly
HostName github.com
IdentityFile ~/.ssh/github-levonfly
User levonfly
  • 自己的私钥
1
2
3
4
# 拷贝私钥

chmod 600 ~/.ssh/github-unix2dos
chmod 600 ~/.ssh/github-levonfly
  • 测试
1
2
3
cd ~ && mkdir workspace && cd workspace/
git clone git@github.com:unix2dos/readwrite.git
rm ~/workspace

1.3 构造并上传镜像

Docker Hub 免费版只有1个私有库,5个私有库要7美元一个月(倒也不是给不起这个钱,只是确实穷….)。自己搭 Docker Registry又嫌麻烦,就放到国内的阿里了(免费)。

https://cr.console.aliyun.com/ 华北2(北京)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 在宿主机构建image
docker commit -a "levonfly" -m "levonfly的博客基础环境" efb25f31a0a0 levonfly/levon_blog_base


# 2. 登录阿里云
docker login --username=l6241425 registry.cn-beijing.aliyuncs.com

# 3. 上传镜像到阿里云
docker tag levonfly/levon_blog_base registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0

docker push registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0


# 4. 下载阿里云镜像
docker run -itd --name hexo_base registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0
docker exec -it levon_blog_base /bin/bash
image-20230910092904490

2. blog环境

2.1 下载镜像

1
2
3
4
docker login --username=l6241425 registry.cn-beijing.aliyuncs.com

docker run -itd --name levon_blog registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0
docker exec -it levon_blog /bin/bash

2.2 下载项目1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cd ~ && mkdir workspace && cd workspace/

# 1. 下载项目
git clone git@github.com:unix2dos/readwrite.git
cd readwrite
git submodule update --init

# 2. 安装依赖
npm i

# 3. 测试部署
hexo server --config source/_data/next.yml
hexo clean --config source/_data/next.yml && hexo g -d --config source/_data/next.yml
./deploy.sh

2.3 下载项目2

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
39
40
41
42
cd ~ && mkdir workspace && cd workspace/

# 1. 下载项目
git clone git@github.com:unix2dos/unix2dos.github.io.git
cd unix2dos.github.io
git checkout source
git submodule update --init

# 2. 安装依赖
npm i


# 3. 插件(不需要)

# 懒加载
git clone https://github.com/theme-next/theme-next-jquery-lazyload source/lib/jquery_lazyload
# 顶部的进度
git clone https://github.com/theme-next/theme-next-pace source/lib/pace
# 读取进度
git clone https://github.com/theme-next/theme-next-reading-progress source/lib/reading_progress
# 分享按钮
git clone https://github.com/theme-next/theme-next-needmoreshare2 source/lib/needsharebutton
# 丝带
git clone https://github.com/theme-next/theme-next-canvas-ribbon source/lib/canvas-ribbon
# 蜘蛛网
git clone https://github.com/theme-next/theme-next-canvas-nest source/lib/canvas-nest
# 三种特效
git clone https://github.com/theme-next/theme-next-three source/lib/three
# 特殊汉字
git clone https://github.com/theme-next/theme-next-han source/lib/Han
# 快速点击
git clone https://github.com/theme-next/theme-next-fastclick source/lib/fastclick
# 图片展示
git clone https://github.com/theme-next/theme-next-fancybox3 source/lib/fancybox
# 文字显示加空格
git clone https://github.com/theme-next/theme-next-pangu.git source/lib/pangu


# 4. 测试部署
hexo server --config source/_data/next.yml
hexo clean --config source/_data/next.yml && hexo g -d --config source/_data/next.yml
./deploy.sh

2.4 使用

1
2
3
4
5
6
7
8
9
10
# 第一个项目
docker run -itd --name blog_unix2dos registry.cn-beijing.aliyuncs.com/levonfly/blog_unix2dos:1.0

docker exec -it blog_unix2dos /bin/bash


# 第二个项目
docker run -itd --name blog_readwrite registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:1.0

docker exec -it blog_readwrite /bin/bash

4.参考资料

给作者打赏,可以加首页微信,咨询作者相关问题!