Docker容器中的Postgres在使用GitHub Actions推送时未设置任何变量。

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

Docker container with Postgres has no variables set when pushed with GitHub Actions

问题

I try to push my Postgres container with Github Actions to Docker Hub. I created .yml file, Dockerfile and have all secrets inside my repo:

Secrets
Docker容器中的Postgres在使用GitHub Actions推送时未设置任何变量。

Dockerfile

  1. FROM postgres:latest
  2. ENV POSTGRES_USER=${POSTGRES_USER}
  3. ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  4. ENV POSTGRES_DB=${POSTGRES_DATABASE}

Action file (.yml):

  1. name: Build and Push Docker Image to Docker Hub
  2. on:
  3. push:
  4. branches: ["production/*"]
  5. jobs:
  6. push_to_registry:
  7. name: Push Docker image to Docker Hub
  8. runs-on: ubuntu-latest
  9. steps:
  10. - name: Check out the repo
  11. uses: actions/checkout@v3
  12. - name: Set up Docker Buildx
  13. uses: docker/setup-buildx-action@v2
  14. - name: Log in to Docker Hub
  15. uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
  16. with:
  17. username: ${{ secrets.DOCKER_USERNAME }}
  18. password: ${{ secrets.DOCKER_PASSWORD }}
  19. - name: Extract metadata (tags, labels) for Docker
  20. id: meta
  21. uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
  22. with:
  23. images: /* my image path */
  24. - name: Build and push Docker image
  25. uses: docker/build-push-action@v2
  26. with:
  27. context: .
  28. push: true
  29. file: ./dockerfiles/postgres-amp-api/Dockerfile
  30. labels: ${{ steps.meta.outputs.labels }}
  31. tags: ${{ steps.meta.outputs.tags }}
  32. secrets: |
  33. "POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}"
  34. "POSTGRES_PASSWORD=${{ secrets.DOCKER_POSTGRES_API_PASSWORD }}"
  35. "POSTGRES_DATABASE=${{ secrets.DOCKER_POSTGRES_API_DB }}"

It was all working when I was putting plain text db, user and password inside Dockerfile, but for some reason when I do it with the current approach, I can see in Docker Desktop that my variables are empty:

Docker容器中的Postgres在使用GitHub Actions推送时未设置任何变量。

and I get this error on container startup:

  1. Error: Database is uninitialized and superuser password is not specified.
  2. You must specify POSTGRES_PASSWORD to a non-empty value for the
  3. superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
  4. You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
  5. connections without a password. This is *not* recommended.

Do I miss anything? Is there any syntax error that I am not aware of?

英文:

I try to push my Postgres container with Github Actions to Docker Hub. I created .yml file, Dockerfile and have all secrets inside my repo:

Secrets
Docker容器中的Postgres在使用GitHub Actions推送时未设置任何变量。

Dockerfile

  1. FROM postgres:latest
  2. ENV POSTGRES_USER=${POSTGRES_USER}
  3. ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  4. ENV POSTGRES_DB=${POSTGRES_DATABASE}

Action file (.yml):

  1. name: Build and Push Docker Image to Docker Hub
  2. on:
  3. push:
  4. branches: [ "production/*" ]
  5. jobs:
  6. push_to_registry:
  7. name: Push Docker image to Docker Hub
  8. runs-on: ubuntu-latest
  9. steps:
  10. - name: Check out the repo
  11. uses: actions/checkout@v3
  12. - name: Set up Docker Buildx
  13. uses: docker/setup-buildx-action@v2
  14. - name: Log in to Docker Hub
  15. uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
  16. with:
  17. username: ${{ secrets.DOCKER_USERNAME }}
  18. password: ${{ secrets.DOCKER_PASSWORD }}
  19. - name: Extract metadata (tags, labels) for Docker
  20. id: meta
  21. uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
  22. with:
  23. images: /* my image path */
  24. - name: Build and push Docker image
  25. uses: docker/build-push-action@v2
  26. with:
  27. context: .
  28. push: true
  29. file: ./dockerfiles/postgres-amp-api/Dockerfile
  30. labels: ${{ steps.meta.outputs.labels }}
  31. tags: ${{ steps.meta.outputs.tags }}
  32. secrets: |
  33. "POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}"
  34. "POSTGRES_PASSWORD=${{ secrets.DOCKER_POSTGRES_API_PASSWORD }}"
  35. "POSTGRES_DATABASE=${{ secrets.DOCKER_POSTGRES_API_DB }}"

It was all working when I was putting plain text db, user and password inside Dockerfile, but for some reason when I do it with current approach, I can see in Docker Desktop that my variables are empty:

Docker容器中的Postgres在使用GitHub Actions推送时未设置任何变量。

and I get this error on container startup:

  1. Error: Database is uninitialized and superuser password is not specified.
  2. You must specify POSTGRES_PASSWORD to a non-empty value for the
  3. superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
  4. You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
  5. connections without a password. This is *not* recommended.

Do I miss anything? Is there any syntax error that I am not aware of?

答案1

得分: 1

Dockerfile中的变量在使用build-args传递它们之前不会被解析。

你的Dockerfile应该如下所示:

  1. FROM postgres:latest
  2. ARG POSTGRES_USER=postgres
  3. ARG POSTGRES_PASSWORD=postgres
  4. ARG POSTGRES_DATABASE=postgres
  5. ENV POSTGRES_USER ${POSTGRES_USER}
  6. ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD}
  7. ENV POSTGRES_DB ${POSTGRES_DATABASE}

和操作部分:

  1. ...
  2. ...
  3. ...
  4. - name: Build and push Docker image
  5. uses: docker/build-push-action@v2
  6. with:
  7. context: .
  8. push: true
  9. file: ./dockerfiles/postgres-amp-api/Dockerfile
  10. labels: ${{ steps.meta.outputs.labels }}
  11. tags: ${{ steps.meta.outputs.tags }}
  12. build-args: |
  13. POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}
  14. ....

参考链接:https://www.baeldung.com/ops/dockerfile-env-variable#dynamic_values

英文:

The variable in Dockerfile will not be resolved until you pass them using build-args.

You dockerfile should look like

  1. FROM postgres:latest
  2. ARG POSTGRES_USER=postgres
  3. ARG POSTGRES_PASSWORD=postgres
  4. ARG POSTGRES_DATABASE=postgres
  5. ENV POSTGRES_USER ${POSTGRES_USER}
  6. ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD}
  7. ENV POSTGRES_DB ${POSTGRES_DATABASE}

and action

  1. ...
  2. ...
  3. ...
  4. - name: Build and push Docker image
  5. uses: docker/build-push-action@v2
  6. with:
  7. context: .
  8. push: true
  9. file: ./dockerfiles/postgres-amp-api/Dockerfile
  10. labels: ${{ steps.meta.outputs.labels }}
  11. tags: ${{ steps.meta.outputs.tags }}
  12. build-args: |
  13. POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}
  14. ....

reference https://www.baeldung.com/ops/dockerfile-env-variable#dynamic_values

huangapple
  • 本文由 发表于 2023年6月8日 06:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427541.html
匿名

发表评论

匿名网友

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

确定