创建镜像
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
RUN apt-get update && apt-get install -y git && apt-get install -y protobuf-compiler RUN protoc --version
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
RUN git clone https://github.com/cheedonghu/hacker-news-digest.git .
RUN pip install --no-cache-dir -r ./page_content_extractor/requirements.txt
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
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
|
使用镜像
步骤
1 2 3 4 5 6 7 8 9 10 11
| nano docker-compose.yml
mkdir ./logs && mkdir ./config
nano ./config/config.toml
docker compose up -d
|
使用本地构建的镜像
1 2 3 4 5 6 7 8 9 10
| services: news_app: 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: news_app: image: cheedonghu/news2tg:latest volumes: - ./config:/config - ./logs:/config/logs restart: unless-stopped volumes: config: logs:
|
其他方式
如果是本机直接跑则构建运行即可。
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:
|