首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从api-service连接到docker容器内的db主机,以便在golang中使用goose进行迁移

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

Stack Overflow用户
提问于 2019-04-06 16:23:25
回答 2查看 736关注 0票数 2

goose是帮助我运行所有*sql文件并在数据库中运行查询的迁移工具。我想在我的api服务上的docker容器中使用这个工具自动迁移(创建表和其他东西)。问题是当docker运行命令"goose run“时,它会收到一个错误-goose run: dial tcp: lookup db on 192.168.63.6:53: no The host。

docker-compose

代码语言:javascript
复制
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"

Dockerfile接口

代码语言:javascript
复制
RUN go get -u github.com/pressly/goose/cmd/goose

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

回答 2

Stack Overflow用户

发布于 2019-04-06 20:24:07

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

代码语言:javascript
复制
# 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指定在启动容器时要执行的命令。参数将附加到此命令。因此,要添加连接字符串,可以执行以下操作

代码语言:javascript
复制
api:
  build: .
  restart: always
  command: "host=db user=user dbname=dbname sslmode=disable password=123"
  ports:
    - 8080:8080
  links:
    - db

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

然而,imho首先使用命令行标志来配置容器是不好的做法。一种更灵活的方法是使用环境变量。例如,在kubernetes中,您可以使用use a config map来设置环境变量,这些变量依次配置一个pod。但是,环境变量也可以与docker-compose或docker swarm一起使用。

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

代码语言:javascript
复制
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。请注意,像https://github.com/spf13/cobra/cobrahttps://gopkg.in/alecthomas/kingpin.v2这样的项目使得使用环境变量变得更加容易,因为它们提供了自动类型转换、轻松设置默认值等功能。

至于更深入地解释为什么要使用环境变量,您可能需要阅读part 3 of the 12 factor app methodology

hth

票数 1
EN

Stack Overflow用户

发布于 2019-04-06 19:22:43

运行命令在构建阶段执行。在此阶段,还没有容器。

用于连接到其他容器的命令应该在Dockerfile中的CMD或ENTRYPOINT中定义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55547207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档