8000 更新 Dockerfile by luyanci · Pull Request #2 · luyanci/remake_bot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

更新 Dockerfile #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@

RUN go mod tidy -v;CGO_ENABLED=0 go build -o /bot

FROM alpine:latest
WORKDIR /src
RUN apk update && \
apk add --no-cache postgresql postgresql-client && \
apk add --no-cache build-base && \
rm -rf /var/cache/apk/*
# 使用官方的 PostgreSQL 基础镜像
FROM postgres:14-bookworm


# 设置环境变量,定义数据库用户名和密码
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=114514

Check warning on line 15 in Dockerfile

View workflow job for this annotation

GitHub Actions / Publish to ghcr.io

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "POSTGRES_PASSWORD") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/
ENV POSTGRES_DB=postgres

RUN mkdir -p /var/lib/postgresql/data
RUN chown -R postgres:postgres /var/lib/postgresql/data
# 将初始化 SQL 脚本复制到镜像中(如果有额外的初始化需求)
# COPY init.sql /docker-entrypoint-initdb.d/
COPY --from=build /bot /bot
COPY --from=build /src/countries.json /countries.json
COPY --from=build /src/user_list.json /user_list.json

RUN su - postgres -c "initdb -D /var/lib/postgresql/data"

COPY start_postgres.sh /start_postgres.sh
RUN chmod +x /start_postgres.sh
# 暴露 PostgreSQL 的默认端口
EXPOSE 5432

COPY --from=build /bot bot
COPY --from=build /src/countries.json countries.json
COPY --from=build /src/user_list.json user_list.json
# 复制启动脚本到镜像中
COPY start_postgres.sh /start.sh
RUN chmod +x /start.sh

EXPOSE 5432
CMD ["/start_postgres.sh", "/src/bot" ]
# 定义容器启动时执行的命令,运行启动脚本
CMD ["/start.sh"]
20 changes: 13 additions & 7 deletions start_postgres.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh
#!/bin/bash
set -e

# 启动 PostgreSQL 服务
su - postgres -c "pg_ctl -D /var/lib/postgresql/data -l logfile start"
# 等待 PostgreSQL 服务启动并就绪
echo "Waiting for PostgreSQL to start..."
while ! pg_isready -U $POSTGRES_USER -d $POSTGRES_DB -h localhost -p 5432; do
sleep 1
done
echo "PostgreSQL is ready."

# 等待数据库启动(可以根据实际情况调整等待时间或检查方式)
sleep 5
# 在这里添加启动你应用程序的命令,假设你的应用程序可执行文件名为 app
# 请根据实际情况替换为你的应用程序启动命令
/bot

# 运行传入的命令(这里是 /src/bot)
exec "$@"
# 如果你的应用程序是在后台运行的,可以使用以下方式启动并等待
# /app &
# wait $!
0