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

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

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文件时,出现以下错误:

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

当我使用以下命令构建docker容器时:

  1. sudo docker build . -t tvappbuilder:latest

然后尝试运行它:

  1. sudo docker run -p 8080:8080 tvappbuilder:latest --env-file .env -it
  2. Config Path .
  3. 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. 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映像如下:

  1. sudo docker image list
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. <none> <none> 6e5f035abda5 18 hours ago 1.82GB
  4. tvappbuilder latest 6166e24a47e0 21 hours ago 21.8MB
  5. <none> <none> cedcaf2facd1 21 hours ago 1.82GB
  6. hello-world latest feb5d9fea6a5 12 months ago 13.3kB
  7. golang 1.15.1 9f495162f677 2 years ago 839MB

以下是docker-compose.yml文件的内容:

  1. version: 3.7
  2. services:
  3. server:
  4. container_name: server
  5. build: .
  6. depends_on:
  7. - database
  8. ports:
  9. - 8080:8080
  10. environment:
  11. - APP_ID: $APP_ID
  12. - APP_CERTIFICATE: $APP_CERTIFICATE
  13. - CUSTOMER_ID: $CUSTOMER_ID
  14. - CUSTOMER_CERTIFICATE: $CUSTOMER_CERTIFICATE
  15. - BUCKET_NAME: $BUCKET_NAME
  16. - BUCKET_ACCESS_KEY: $BUCKET_ACCESS_KEY
  17. - BUCKET_ACCESS_SECRET: $BUCKET_ACCESS_SECRET
  18. - CLIENT_ID: $CLIENT_ID
  19. - CLIENT_SECRET: $CLIENT_SECRET
  20. - PSTN_USERNAME: $PSTN_USERNAME
  21. - PSTN_PASSWORD: $PSTN_PASSWORD
  22. - SCHEME: $SCHEME
  23. - ALLOWED_ORIGIN: ""
  24. - ENABLE_NEWRELIC_MONITORING: false
  25. - RUN_MIGRATION: true
  26. - DATABASE_URL: postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@database:5432/$POSTGRES_DB?sslmode=disable
  27. database:
  28. container_name: server_database
  29. image: postgres-14.5
  30. restart: always
  31. hostname: database
  32. environment:
  33. - POSTGRES_USER: $POSTGRES_USER
  34. - POSTGRES_PASSWORD: $POSTGRES_PASSWORD
  35. - POSTGRES_DB: $POSTGRES_DB

以下是Dockerfile的内容:

  1. ## Using Dockerfile from the following post: https://medium.com/@petomalina/using-go-mod-download-to-speed-up-golang-docker-builds-707591336888
  2. FROM golang:1.15.1 as build-env
  3. # All these steps will be cached
  4. RUN mkdir /server
  5. WORKDIR /server
  6. COPY go.mod .
  7. COPY go.sum .
  8. # Get dependancies - will also be cached if we won't change mod/sum
  9. RUN go mod download
  10. # COPY the source code as the last step
  11. COPY . .
  12. # Build the binary
  13. RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/server /server/cmd/video_conferencing
  14. # Second step to build minimal image
  15. FROM scratch
  16. COPY --from=build-env /go/bin/server /go/bin/server
  17. COPY --from=build-env /server/config.json config.json
  18. ENTRYPOINT ["/go/bin/server"]

以下是.env文件的内容:

  1. ENCRYPTION_ENABLED=0
  2. POSTGRES_USER=admin
  3. POSTGRES_PASSWORD=<correct pswd for admin>
  4. POSTGRES_DB=tvappbuilder
  5. APP_ID=<my real app ID>
  6. APP_CERTIFICATE=<my real app cert>
  7. CUSTOMER_ID=<my real ID>
  8. CUSTOMER_CERTIFICATE=<my real cert>
  9. RECORDING_REGION=0
  10. BUCKET_NAME=<my bucket name>
  11. BUCKET_ACCESS_KEY=<my real key>
  12. BUCKET_ACCESS_SECRET=<my real secret>
  13. CLIENT_ID=
  14. CLIENT_SECRET=
  15. PSTN_USERNAME=
  16. PSTN_PASSWORD=
  17. PSTN_ACCOUNT=
  18. PSTN_EMAIL=
  19. SCHEME=esports1_agora
  20. ENABLE_SLACK_OAUTH=0
  21. SLACK_CLIENT_ID=
  22. SLACK_CLIENT_SECRET=
  23. GOOGLE_CLIENT_ID=
  24. ENABLE_GOOGLE_OAUTH=0
  25. GOOGLE_CLIENT_SECRET=
  26. ENABLE_MICROSOFT_OAUTH=0
  27. MICROSOFT_CLIENT_ID=
  28. MICROSOFT_CLIENT_SECRET=
  29. APPLE_CLIENT_ID=
  30. APPLE_PRIVATE_KEY=
  31. APPLE_KEY_ID=
  32. APPLE_TEAM_ID=
  33. ENABLE_APPLE_OAUTH=0
  34. 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:

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

When I build my docker container with

  1. sudo docker build . -t tvappbuilder:latest

and then try to run it with:

  1. sudo docker run -p 8080:8080 tvappbuilder:latest --env-file .env -it
  2. Config Path .
  3. 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. 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:

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

Here is the docker-compose.yml:

  1. version: 3.7
  2. services:
  3. server:
  4. container_name: server
  5. build: .
  6. depends_on:
  7. - database
  8. ports:
  9. - 8080:8080
  10. environment:
  11. - APP_ID: $APP_ID
  12. - APP_CERTIFICATE: $APP_CERTIFICATE
  13. - CUSTOMER_ID: $CUSTOMER_ID
  14. - CUSTOMER_CERTIFICATE: $CUSTOMER_CERTIFICATE
  15. - BUCKET_NAME: $BUCKET_NAME
  16. - BUCKET_ACCESS_KEY: $BUCKET_ACCESS_KEY
  17. - BUCKET_ACCESS_SECRET: $BUCKET_ACCESS_SECRET
  18. - CLIENT_ID: $CLIENT_ID
  19. - CLIENT_SECRET: $CLIENT_SECRET
  20. - PSTN_USERNAME: $PSTN_USERNAME
  21. - PSTN_PASSWORD: $PSTN_PASSWORD
  22. - SCHEME: $SCHEME
  23. - ALLOWED_ORIGIN: &quot;&quot;
  24. - ENABLE_NEWRELIC_MONITORING: false
  25. - RUN_MIGRATION: true
  26. - DATABASE_URL: postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@database:5432/$POSTGRES_DB?sslmode=disable
  27. database:
  28. container_name: server_database
  29. image: postgres-14.5
  30. restart: always
  31. hostname: database
  32. environment:
  33. - POSTGRES_USER: $POSTGRES_USER
  34. - POSTGRES_PASSWORD: $POSTGRES_PASSWORD
  35. - POSTGRES_DB: $POSTGRES_DB

Here is the Dockerfile:

  1. ## Using Dockerfile from the following post: https://medium.com/@petomalina/using-go-mod-download-to-speed-up-golang-docker-builds-707591336888
  2. FROM golang:1.15.1 as build-env
  3. # All these steps will be cached
  4. RUN mkdir /server
  5. WORKDIR /server
  6. COPY go.mod .
  7. COPY go.sum .
  8. # Get dependancies - will also be cached if we won&#39;t change mod/sum
  9. RUN go mod download
  10. # COPY the source code as the last step
  11. COPY . .
  12. # Build the binary
  13. RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/server /server/cmd/video_conferencing
  14. # Second step to build minimal image
  15. FROM scratch
  16. COPY --from=build-env /go/bin/server /go/bin/server
  17. COPY --from=build-env /server/config.json config.json
  18. ENTRYPOINT [&quot;/go/bin/server&quot;]

and here is the .env file:

  1. ENCRYPTION_ENABLED=0
  2. POSTGRES_USER=admin
  3. POSTGRES_PASSWORD=&lt;correct pswd for admin&gt;
  4. POSTGRES_DB=tvappbuilder
  5. APP_ID=&lt;my real app ID&gt;
  6. APP_CERTIFICATE=&lt;my real app cert&gt;
  7. CUSTOMER_ID=&lt;my real ID&gt;
  8. CUSTOMER_CERTIFICATE=&lt;my real cert&gt;
  9. RECORDING_REGION=0
  10. BUCKET_NAME=&lt;my bucket name&gt;
  11. BUCKET_ACCESS_KEY=&lt;my real key&gt;
  12. BUCKET_ACCESS_SECRET=&lt;my real secret&gt;
  13. CLIENT_ID=
  14. CLIENT_SECRET=
  15. PSTN_USERNAME=
  16. PSTN_PASSWORD=
  17. PSTN_ACCOUNT=
  18. PSTN_EMAIL=
  19. SCHEME=esports1_agora
  20. ENABLE_SLACK_OAUTH=0
  21. SLACK_CLIENT_ID=
  22. SLACK_CLIENT_SECRET=
  23. GOOGLE_CLIENT_ID=
  24. ENABLE_GOOGLE_OAUTH=0
  25. GOOGLE_CLIENT_SECRET=
  26. ENABLE_MICROSOFT_OAUTH=0
  27. MICROSOFT_CLIENT_ID=
  28. MICROSOFT_CLIENT_SECRET=
  29. APPLE_CLIENT_ID=
  30. APPLE_PRIVATE_KEY=
  31. APPLE_KEY_ID=
  32. APPLE_TEAM_ID=
  33. ENABLE_APPLE_OAUTH=0
  34. 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:

确定