【问题标题】:failed run Postgres in alpine dockerfile在 alpine dockerfile 中运行 Postgres 失败
【发布时间】:2020-09-21 16:15:49
【问题描述】:

我正在尝试在 docker 中使用 Alpine 部署我的 Go 应用程序,我能够在我的 Mac 上使用它,然后使用 Centos 8 进行生产时遇到问题 这是我的 Dockerfile:

FROM golang:alpine

RUN apk add --no-cache postgresql

RUN apk update && apk add --no-cache gcc && apk add --no-cache libc-dev && apk add --no-cache --update make

# Set the current working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. they will be cached of the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the WORKDIR inisde the container
COPY . .

# Build the Go app
RUN go build .

RUN rm -rf /usr/local/var/postgres/postmaster.pid

// this commands below like "psql -c ;'DROP DATABASE IF EXISTS prod'"
// "psql -c ;'CREATE USER prod'"
RUN make setup

# Exporse port 3000 or 8000 to the outisde world
EXPOSE 3000..

CMD ["make", "run" ]

然后我得到了错误:

psql: error: could not connect to server: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

在我的make setup 上,我进行迁移、创建用户、数据库

也可以为那个 alpine 在 psql 上创建 SUPERUSER 吗??

你可以在上面的语法中看到什么,有什么错误以及如何纠正它?我从昨天开始卡住了

【问题讨论】:

  • 请在此处发布some syntax here...... 部分。删除您的密码、IP。
  • 刚刚更新了@SachithMuhandiram

标签: linux postgresql docker command-line centos


【解决方案1】:

从第 8 行到第 20 行删除您的原始 docker 文件并添加这些。

如果你的文件夹结构是这样的:

- directory
    |
    -> Dockerfile
    -> go.mod
    -> go.sum
    -> go source files
# Copy go mod and sum files
COPY . /app

# Set the current working Directory inside the container
WORKDIR /app

RUN go mod download

RUN go build .

【讨论】:

  • 执行 COPY 命令时出错:When using COPY with more than one source file, the destination must be a directory and end with a /
  • @certual 错误也给了你答案...COPY go.mod go.sum /app/,我也更新了答案。
  • 再次出错:can't load package: package .: no Go files in /app
  • 当我运行 RUN ls 我的文件不存在时,只有 go.mod 和 go.sum
  • 我应该为此安装 postgres 容器吗?我没有将它安装在容器上?只需在 docker golang:alpine 上使用它
【解决方案2】:

您不能在 Dockerfile 中运行数据库命令。

以此类推,考虑go generate 命令:您可以在 Go 源代码中嵌入特殊的 cmets,要求 Go 编译器为您运行程序,通常是生成其他源文件。在源代码中假设您 //go:generate: psql ... 并运行 go generate ... && go install . 现在您在不同的系统上运行该编译的二进制文件。由于您不再指向同一个数据库,因此数据库设置会丢失。

以同样的方式,Dockerfile 会生成一个已编译的工件(在本例中为 Docker 映像),并且它需要独立于其主机环境运行。在您的示例中,您可以 docker push 将您在 MacOS 上构建的映像添加到注册表中,然后将其从 CentOS 主机 docker run 无需重新构建它(这对于生产系统来说可能是更好的做法)。

对于您在问题中显示的特定命令,您可以将它们放在数据库容器的 /docker-entrypoint-initdb.d 目录中,或者只需在指向您的数据库时运行它们。对于更通用的数据库设置,您可能会考虑在应用程序启动时运行数据库迁移工具,无论是在程序的 main() 函数中还是在包装入口点脚本中。

【讨论】:

  • 您的意思是我应该将go app 的image 推送到docker hub,然后通过拉取该图像在CentOS 上运行它吗?在那个 Makefile 脚本上,我做了很多运行命令psql,你有没有这方面的例子,到目前为止我还是不明白:D
  • 假设我有容器 go_app 和 go_postgres,然后在 go_app 上没有任何 psql 包,我可以使用//go:generate: psql 吗?
  • 重要的细节是“构建应用程序”和“配置数据库”是独立的步骤:您可以构建应用程序或 Docker 映像一次,然后针对多个不同的数据库使用生成的工件。作为部署过程的一部分,您可能需要从 Makefile 中运行手动 psql 步骤,与您对 Docker 所做的任何事情分开。
  • 要迁移,我需要在我的应用程序上生成 sql-migrate,这是否意味着我也应该在配置数据库容器上安装 go?
猜你喜欢
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 2018-04-09
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多