0%

blog制作docker镜像记录

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

1. 宿主机准备(gcp)

参考:https://www.liuvv.com/p/b7e5827a.html

1.1 安装docker

1
2
apt update
apt install -y docker.io

1.2 宿主机开启镜像加速

image-20230909171630001

https://cr.console.aliyun.com/cn-beijing/instances/mirrors 先给本机来个加速。

1
2
3
4
5
6
7
8
9
10
11
12
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. 制作基础镜像 (levon_blog_base)

https://cr.console.aliyun.com/cn-beijing/instance/repositories

2.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
29
# 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

2.2 增加相关配置

  • git 配置
1
2
git config --global user.email "levonfly@gmail.com"
git config --global user.name "unix2dos"
  • ssh/config
1
2
mkdir .ssh
vi ~/.ssh/config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
  • 测试ssh是否通
1
2
3
cd ~ && mkdir workspace && cd workspace/
git clone git@github.com:unix2dos/blog_readwrite.git
rm ~/workspace

2.3 构造并上传镜像

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

https://cr.console.aliyun.com/cn-beijing/instance/repositories 华北2(北京)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

3. 制作镜像和使用 (blog)

3.1 下载基础镜像

https://cr.console.aliyun.com/cn-beijing/instance/repositories

1
2
3
4
5
6
# 登录,需要输入密码
docker login --username=l6241425 registry.cn-beijing.aliyuncs.com
# 下载
docker run -itd --name levon_blog_base registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0
# 进入
docker exec -it levon_blog_base /bin/bash

3.2 blog1 (liuvv)

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
29
30
31
32
33
34
35
36
37
# 0. 下载基础镜像
docker rm -f levon_blog_base
docker run -itd --name levon_blog_base registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0
docker exec -it levon_blog_base /bin/bash

# 1. 下载项目
cd ~ && mkdir workspace && cd workspace/
git clone --depth 1 git@github.com:unix2dos/blog-liuvv.git

cd blog-liuvv && 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-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


# 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. 制作镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看hash
docker ps

# 提交镜像
docker commit -a "v1.0.0" -m "blog_liuvv" 66b6a88bed57 levonfly/blog_liuvv

# 推送1
docker tag levonfly/blog_liuvv registry.cn-beijing.aliyuncs.com/levonfly/blog_liuvv:v1.0.0
docker push registry.cn-beijing.aliyuncs.com/levonfly/blog_liuvv:v1.0.0

# 推送2
docker tag levonfly/blog_liuvv registry.cn-beijing.aliyuncs.com/levonfly/blog_liuvv:lastest
docker push registry.cn-beijing.aliyuncs.com/levonfly/blog_liuvv:lastest

3. 使用镜像

1
2
3
4
5
# 1. 下载最新镜像
docker run -itd --name blog_liuvv registry.cn-beijing.aliyuncs.com/levonfly/blog_liuvv:lastest

# 2. 操作
docker exec -it blog_liuvv /bin/bash

3.3 blog2 (readwrite)

1. 下载基础镜像和数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 0. 下载基础镜像
docker run -itd --name levon_blog_base registry.cn-beijing.aliyuncs.com/levonfly/levon_blog_base:1.0
docker exec -it levon_blog_base /bin/bash

# 1. 下载项目
cd ~ && mkdir workspace && cd workspace/
git clone --depth 1 git@github.com:unix2dos/blog-readwrite.git
cd blog-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. 制作镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看hash
docker ps

# 1. 提交镜像
docker commit -a "v1.0.0" -m "blog_readwrite" d8cccfedee06 levonfly/blog_readwrite

# 2. 打tag1
docker tag levonfly/blog_readwrite registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:v1.0.0

docker push registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:v1.0.0


# 3. 推送2
docker tag levonfly/blog_readwrite registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:lastest

docker push registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:lastest

3. 使用镜像

1
2
3
4
5
# 1. 下载最新镜像
docker run -itd --name blog_readwrite registry.cn-beijing.aliyuncs.com/levonfly/blog_readwrite:lastest

# 2. 操作
docker exec -it blog_readwrite /bin/bash

3.4 使用问题

  • 如果docker 内无法访问 github,可以尝试替换代理试试。

4.参考资料

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