登录
首页 >  Golang >  Go问答

Docker容器或docker compose在使用Postgres、Golang、Debian 11和Agora appbuilder后端时遇到错误

来源:stackoverflow

时间:2024-02-07 17:45:23 117浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Docker容器或docker compose在使用Postgres、Golang、Debian 11和Agora appbuilder后端时遇到错误》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

我在 aws 上启动了 debian 11 ec2,并在其上安装了 postgres 14.5,并在其上安装了 docker 和 docker compose。我使用密码向 postgres 添加了一个超级用户“admin”。我创建了 docker-compose.yml 文件和一个 .env 文件。

当我尝试使用 docker-compose.yml 文件时,我得到:

sudo docker compose up -d
services.database.environment must be a mapping

当我使用 构建我的 docker 容器时

sudo docker build . -t tvappbuilder:latest

然后尝试运行它:

sudo docker run -p 8080:8080 tvappbuilder:latest --env-file .env -it
config path .
4:47pm inf server/utils/logging.go:105 > logging configured filelogging=true filename=app-builder-logs logdirectory=./logs maxageindays=0 maxbackups=0 maxsizemb=0
4:47pm ftl server/cmd/video_conferencing/server.go:71 > error initializing database error="pq: could not detect default username. please provide one explicitly"

这是迄今为止的码头工人:

sudo docker image list
repository     tag       image id       created         size
             6e5f035abda5   18 hours ago    1.82gb
tvappbuilder   latest    6166e24a47e0   21 hours ago    21.8mb
             cedcaf2facd1   21 hours ago    1.82gb
hello-world    latest    feb5d9fea6a5   12 months ago   13.3kb
golang         1.15.1    9f495162f677   2 years ago     839mb

这是 docker-compose.yml:

version: 3.7
services:
    server:
        container_name: server
        build: .
        depends_on:
            - database
        ports:
           - 8080:8080
        environment:
            - app_id: $app_id
            - app_certificate: $app_certificate
            - customer_id: $customer_id
            - customer_certificate: $customer_certificate
            - bucket_name: $bucket_name
            - bucket_access_key: $bucket_access_key
            - bucket_access_secret: $bucket_access_secret
            - client_id: $client_id
            - client_secret: $client_secret
            - pstn_username: $pstn_username
            - pstn_password: $pstn_password
            - scheme: $scheme
            - allowed_origin: ""
            - enable_newrelic_monitoring: false
            - run_migration: true
            - database_url: postgresql://$postgres_user:$postgres_password@database:5432/$postgres_db?sslmode=disable

    database:
        container_name: server_database
        image: postgres-14.5
        restart: always
        hostname: database
        environment: 
            - postgres_user: $postgres_user
            - postgres_password: $postgres_password
            - postgres_db: $postgres_db

这是 dockerfile:

## using dockerfile from the following post: https://medium.com/@petomalina/using-go-mod-download-to-speed-up-golang-docker-builds-707591336888

from golang:1.15.1 as build-env

# all these steps will be cached
run mkdir /server
workdir /server
copy go.mod . 
copy go.sum .

# get dependancies - will also be cached if we won't change mod/sum
run go mod download
# copy the source code as the last step
copy . .

# build the binary
run cgo_enabled=0 goos=linux goarch=amd64 go build -a -installsuffix cgo -o /go/bin/server /server/cmd/video_conferencing

# second step to build minimal image
from scratch
copy --from=build-env /go/bin/server /go/bin/server
copy --from=build-env /server/config.json config.json

entrypoint ["/go/bin/server"]

这是 .env 文件:

ENCRYPTION_ENABLED=0
POSTGRES_USER=admin
POSTGRES_PASSWORD=
POSTGRES_DB=tvappbuilder
APP_ID=
APP_CERTIFICATE=
CUSTOMER_ID=
CUSTOMER_CERTIFICATE=
RECORDING_REGION=0
BUCKET_NAME=
BUCKET_ACCESS_KEY=
BUCKET_ACCESS_SECRET=
CLIENT_ID=
CLIENT_SECRET=
PSTN_USERNAME=
PSTN_PASSWORD=
PSTN_ACCOUNT=
PSTN_EMAIL=
SCHEME=esports1_agora
ENABLE_SLACK_OAUTH=0
SLACK_CLIENT_ID=
SLACK_CLIENT_SECRET=
GOOGLE_CLIENT_ID=
ENABLE_GOOGLE_OAUTH=0
GOOGLE_CLIENT_SECRET=
ENABLE_MICROSOFT_OAUTH=0
MICROSOFT_CLIENT_ID=
MICROSOFT_CLIENT_SECRET=
APPLE_CLIENT_ID=
APPLE_PRIVATE_KEY=
APPLE_KEY_ID=
APPLE_TEAM_ID=
ENABLE_APPLE_OAUTH=0
PAPERTRAIL_API_TOKEN=

根据此:https://pkg.go.dev/github.com/lib/pq 我可能不需要使用 pq,而是直接使用 postgres,但看起来它 就是这样设置的。

非常感谢您的指点!


正确答案


根据评论,您的设置存在许多问题。

第一个是运行 docker compose up -d 时出现错误 services.database.environment Must be aapping。这是由 docker-compose.yml 中的 - APP_ID: $APP_ID 等行引起的 - 根据 documentation 使用 APP_ID: $APP_ID- APP_ID=$APP_ID

另一个问题是您在裸操作系统上安装了 Postgres,然后使用 postgres 容器。您只需要执行其中之一(但如果使用 docker,您将需要使用卷或挂载 Postgres 数据(否则在重建容器时它将丢失)。

可能还有其他问题,但以上内容应该可以帮助您入门。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>