gpt4 book ai didi

docker - 无法连接到 docker 容器中的数据库主机,从 api-service 到 db-service,以便在 golang 中使用 goose 进行迁移

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:58 27 4
gpt4 key购买 nike

goose 是帮助我运行所有 *sql 文件并在数据库中运行查询的迁移工具。我想在我的 api 服务的 docker 容器中使用此工具自动执行迁移(创建表和其他内容)。问题是当 docker 运行命令“goose run”时出现错误 -goose run: dial tcp: lookup db on 192.168.63.6:53: no such host。

docker-compose

services:
db:
build: ./db
volumes:
- ./db/pgdata:/pgdata
image: postgres
ports:
- "5432"
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_DB=dbname
- POSTGRES_PASSWORD=123
- PGDATA=/pgdata

api:
build:
context: ./api
restart: always
volumes:
- ./api:/go/src/github.com/gitlees/todoapp/api
ports:
- "5000:8080"
links:
- "db"

API 文件

RUN go get -u github.com/pressly/goose/cmd/goose

RUN goose postgres "host=db user=user dbname=dbname sslmode=disable password=123" up

最佳答案

首先,让我们深入了解一下 Dockerfile。我已经设置了一个 repository for demo purposes for this question ,也是。

# We use a so called two stage build.
# Basically this means we build our go binary in one image
# which has the go compiler and all the required tools and libraries.
# However, since we do not need those in our production image,
# we copy the binary created into a basic alpine image
# resulting in a much smaller image for production.

# We define our image for the build environment...
FROM golang:1.11-alpine3.8 as build

# ...and copy our project directory tp the according place.
COPY . "/go/src/github.com/mwmahlberg/so-postgres-compose"
# We set our work directory...
WORKDIR /go/src/github.com/mwmahlberg/so-postgres-compose
# ...and add git, which - forever reason, is not included into the golang image.
RUN apk add git

# We set up our dependency management and make sure all dependencies outside
# the standard library are installed.
RUN set -x && \
go get github.com/golang/dep/cmd/dep && \
dep ensure -v
# Finally, we build our binary and name it accordingly
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /apiserver

# Now for the second stage, we use a basic alpine image...
FROM alpine:3.8
# ... update it...
RUN apk --no-cache upgrade
# .. and take the binary from the image we build above.
# Note the location: [/usr]{/bin:/sbin} are reserved for
# the OS's package manager. Binaries added to an OS by
# the administrator which are not part of the OS's package
# mangement system should always go below /usr/local/{bin,sbin}
COPY --from=build /apiserver /usr/local/bin/apiserver
# Last but not least, we tell docker which binary to execute.
ENTRYPOINT ["/usr/local/bin/apiserver"]

最后一行实际上应该可以解决问题:ENTRYPOINT 指定容器启动时要执行的命令。参数附加到此命令。因此,要添加连接字符串,您可以执行以下操作

api:
build: .
restart: always
command: "host=db user=user dbname=dbname sslmode=disable password=123"
ports:
- 8080:8080
links:
- db

您应该做的最后一件事是对 docker 镜像进行静态配置,就像您在示例中展示的那样。您基本上设置了一个静态连接字符串,这剥夺了您使用容器的大部分灵 active 。

但是,恕我直言,首先使用命令行标志来配置容器是不好的做法。一种更灵活的方法是使用环境变量。例如,在 kubernetes 中你会 use a config map设置环境变量,进而配置 pod。但是,环境变量也可以与 docker-compose 或 docker swarm 一起使用。

因此,我会将 docker-compose.yml 更改为如下内容:

version: '3'
services:
db:
volumes:
- ./db/pgdata:/pgdata
image: postgres
ports:
- "5432"
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_DB=dbname
- POSTGRES_PASSWORD=123
- PGDATA=/pgdata

# adminer added for convenience
adminer:
image: adminer
restart: always
ports:
- 8081:8080
depends_on:
- db

api:
build: .
restart: always
ports:
- 8080:8080
depends_on:
- db
environment:
- POSTGRES_USER=user
- POSTGRES_DB=dbname
- POSTGRES_PASSWORD=123
- POSTGRES_HOST=db
- POSTGRES_PORT=5432

并使用环境变量配置二进制文件。

我添加了一个 simple example how to use environment variables in Go到 repo 协议(protocol)。请注意,类似 https://github.com/spf13/cobra/cobra 的项目或 https://gopkg.in/alecthomas/kingpin.v2使使用环境变量变得更加容易,因为它们提供自动类型转换、轻松设置默认值的能力等等。

关于为什么要使用环境变量的更深入的推理,您可能需要阅读 part 3 of the 12 factor app methodology .

第一个

关于docker - 无法连接到 docker 容器中的数据库主机,从 api-service 到 db-service,以便在 golang 中使用 goose 进行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55547207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com