Error running an Docker container or docker compose with postgres, golang and Debian 11, Agora appbuilder backend

huangapple go评论95阅读模式
英文:

Error running an Docker container or docker compose with postgres, golang and Debian 11, Agora appbuilder backend

问题

我在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"

目前的docker映像如下:

sudo docker image list
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
<none>         <none>    6e5f035abda5   18 hours ago    1.82GB
tvappbuilder   latest    6166e24a47e0   21 hours ago    21.8MB
<none>         <none>    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=<correct pswd for admin>
POSTGRES_DB=tvappbuilder
APP_ID=<my real app ID>
APP_CERTIFICATE=<my real app cert>
CUSTOMER_ID=<my real ID>
CUSTOMER_CERTIFICATE=<my real cert>
RECORDING_REGION=0
BUCKET_NAME=<my bucket name>
BUCKET_ACCESS_KEY=<my real key>
BUCKET_ACCESS_SECRET=<my real 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=<my real token>

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

非常感谢您的帮助!

英文:

I spun up a Debian 11 EC2 on AWS, and installed postgres 14.5 on it and docker and docker compose on it.I added a superuser to postgres of "admin' with a password. I created my docker-compose.yml file and a .env file.

When I try to use the docker-compose.yml file, I get:

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

When I build my docker container with

sudo docker build . -t tvappbuilder:latest

and then try to run it with:

sudo docker run -p 8080:8080 tvappbuilder:latest --env-file .env -it
Config Path .
4:47PM INF server/utils/logging.go:105 &gt; 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 &gt; Error initializing database error=&quot;pq: Could not detect default username. Please provide one explicitly&quot;

Here are the dockers so far:

sudo docker image list
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
&lt;none&gt;         &lt;none&gt;    6e5f035abda5   18 hours ago    1.82GB
tvappbuilder   latest    6166e24a47e0   21 hours ago    21.8MB
&lt;none&gt;         &lt;none&gt;    cedcaf2facd1   21 hours ago    1.82GB
hello-world    latest    feb5d9fea6a5   12 months ago   13.3kB
golang         1.15.1    9f495162f677   2 years ago     839MB

Here is the 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: &quot;&quot;
            - 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

Here is the 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&#39;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 [&quot;/go/bin/server&quot;]

and here is the .env file:

ENCRYPTION_ENABLED=0
POSTGRES_USER=admin
POSTGRES_PASSWORD=&lt;correct pswd for admin&gt;
POSTGRES_DB=tvappbuilder
APP_ID=&lt;my real app ID&gt;
APP_CERTIFICATE=&lt;my real app cert&gt;
CUSTOMER_ID=&lt;my real ID&gt;
CUSTOMER_CERTIFICATE=&lt;my real cert&gt;
RECORDING_REGION=0
BUCKET_NAME=&lt;my bucket name&gt;
BUCKET_ACCESS_KEY=&lt;my real key&gt;
BUCKET_ACCESS_SECRET=&lt;my real secret&gt;
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=&lt;my real token&gt;

According to this: https://pkg.go.dev/github.com/lib/pq
I probably should not need to use pq, and instead use postgres directly, but it appears it
was set up this way.

Many thanks for any pointers!

答案1

得分: 1

根据评论,你的设置存在一些问题。

首先是在运行docker compose up -d时出现的错误services.database.environment must be a mapping。这是由于在你的docker-compose.yml中的类似- APP_ID: $APP_ID的行引起的。根据文档,你可以使用APP_ID: $APP_ID- APP_ID=$APP_ID

另一个问题是你在裸机上安装了Postgres,然后又使用了一个postgres容器。你只需要选择其中一种方式(但如果使用docker,你需要为Postgres数据使用卷或挂载,否则在重新构建容器时数据将丢失)。

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

英文:

As per the comments there are a number of issues with your setup.

The first is the error services.database.environment must be a mapping when running docker compose up -d. This is caused by lines like - APP_ID: $APP_ID in your docker-compose.yml - use either APP_ID: $APP_ID or - APP_ID=$APP_ID as per the documentation.

A further issue is that you installed Postgres on the bare OS and are then using a postgres container. You only need to do one or the other (but if using docker you will want to use a volume or mount for the Postgres data (otherwise it will be lose when the container is rebuilt).

There are probably further issues but the above should get you started.

huangapple
  • 本文由 发表于 2022年9月20日 03:37:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73778480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定