创建news2tg的docker镜像

创建镜像

1.创建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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 使用多阶段构建
FROM rust:latest AS rust-builder
WORKDIR /usr/src/news2tg

# 安装git,protobuf-compiler等工具
RUN apt-get update && apt-get install -y git && apt-get install -y protobuf-compiler
RUN protoc --version

# 从GitHub克隆Rust仓库
RUN git clone https://github.com/cheedonghu/news2tg.git .
RUN cargo build --release

FROM python:3.11
WORKDIR /app

RUN apt-get update && apt-get install -y git

# 从GitHub克隆Python仓库
RUN git clone https://github.com/cheedonghu/hacker-news-digest.git .

# 安装Python依赖
RUN pip install --no-cache-dir -r ./page_content_extractor/requirements.txt

# 从Rust构建阶段复制编译好的二进制文件
COPY --from=rust-builder /usr/src/news2tg/target/release/news2tg /app/news2tg

# 创建配置文件目录
RUN mkdir /config

# 创建日志目录
RUN mkdir /logs

# 设置环境变量指向配置文件位置
ENV RUST_CONFIG_PATH=/config/config.toml

# 暴露端口(其实没必要暴露)
EXPOSE 50051

# 下载 wait-for-it.sh 脚本
RUN curl -o /usr/local/bin/wait-for-it.sh https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh
RUN chmod +x /usr/local/bin/wait-for-it.sh

# 启动命令
CMD ["sh", "-c", "python -m page_content_extractor.main & wait-for-it.sh localhost:50051 -- ./news2tg -c $RUST_CONFIG_PATH >> /logs/news2tg.log 2>&1"]

2.构建镜像

1
2
# 最好在网络通畅环境下运行,避免网络问题
docker build -t news2tg .

3.(可选)推送到dockerhub

这里我build机器和运行的机器不一致所以需要推送后在另一台机器拉取镜像运行。

1
2
3
4
5
6
7
8
# 先登录
docker login

# 再给镜像加上标签
docker tag news2tg cheedonghu/news2tg:latest

# 推送镜像
docker push cheedonghu/news2tg:latest

其他方式:github workflow

  1. 在 GitHub 仓库的 Settings -> Secrets 页面中添加以下 Secrets
  • DOCKER_USERNAME:您的 Docker Hub 用户名
  • DOCKER_PASSWORD:您的 Docker Hub 密码或访问令牌
  • 确保 Dockerfile 位于仓库根目录中
  1. 选择项目下Actions->Docker image->Configure

  2. 创建yml

    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
    name: News2tg Docker Image CI

    on:
    push:
    branches: [ "main" ]
    pull_request:
    branches: [ "main" ]

    jobs:

    build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
    uses: actions/checkout@v2

    # 登录到 Docker Hub(需要在仓库的 Secrets 中配置 DOCKER_USERNAME 和 DOCKER_PASSWORD)
    - name: Login to Docker Registry
    uses: docker/login-action@v2
    with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}

    # 使用 docker/build-push-action 构建并推送镜像
    - name: Build and push Docker image
    uses: docker/build-push-action@v3
    with:
    context: .
    file: ./Dockerfile
    platforms: linux/amd64
    push: true
    tags: ${{ secrets.DOCKER_USERNAME }}/news2tg:latest

使用镜像

步骤

1
2
3
4
5
6
7
8
9
10
11
# 1. 创建docker-compose.yml 按需复制下面的yml
nano docker-compose.yml

# 2. 创建挂载的文件夹
mkdir ./logs && mkdir ./config

# 3. 把配置文件放在config文件夹下
nano ./config/config.toml

# 4. 运行
docker compose up -d

使用本地构建的镜像

1
2
3
4
5
6
7
8
9
10
services:
news2tg:
image: news2tg
volumes:
- ./config:/config
- ./logs:/config/logs
restart: unless-stopped
volumes:
config:
logs:

使用dockerhub上的镜像

1
2
3
4
5
6
7
8
9
10
services:
news2tg:
image: cheedonghu/news2tg:latest
volumes:
- ./config:/config
- ./logs:/config/logs
restart: unless-stopped
volumes:
config:
logs:

更新镜像

1
2
3
4
5
# 更新镜像至最新版本
1. sudo docker compose pull news2tg

# 在docker-compose.yml同级目录执行重建并启动
2. sudo docker compose up -d

其他方式

如果是本机直接跑则构建运行即可。

1
2
3
4
5
6
7
8
9
10
11
12
services:
news2tg:
build:
context: .
dockerfile: path/to/your/Dockerfile
volumes:
- ./config:/config
- ./logs:/config/logs
restart: unless-stopped
volumes:
config:
logs: