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

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

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

FROM postgres:latest

ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DB=${POSTGRES_DATABASE}

Action file (.yml):

name: Build and Push Docker Image to Docker Hub

on:
  push:
    branches: ["production/*"]

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: /* my image path */
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          file: ./dockerfiles/postgres-amp-api/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ steps.meta.outputs.tags }}
          secrets: |
            "POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}"
            "POSTGRES_PASSWORD=${{ secrets.DOCKER_POSTGRES_API_PASSWORD }}"
            "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:

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       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

FROM postgres:latest

ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DB=${POSTGRES_DATABASE}

Action file (.yml):

name: Build and Push Docker Image to Docker Hub

on:
  push:
    branches: [ "production/*" ]

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
        with:
          images: /* my image path */
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          file: ./dockerfiles/postgres-amp-api/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ steps.meta.outputs.tags }}
          secrets: |
            "POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}"
            "POSTGRES_PASSWORD=${{ secrets.DOCKER_POSTGRES_API_PASSWORD }}"
            "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:

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       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应该如下所示:

FROM postgres:latest
ARG POSTGRES_USER=postgres
ARG POSTGRES_PASSWORD=postgres
ARG POSTGRES_DATABASE=postgres

ENV POSTGRES_USER ${POSTGRES_USER}
ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD}
ENV POSTGRES_DB ${POSTGRES_DATABASE}

和操作部分:

...
...
...
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          file: ./dockerfiles/postgres-amp-api/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}
            ....

参考链接: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

FROM postgres:latest
ARG POSTGRES_USER=postgres
ARG POSTGRES_PASSWORD=postgres
ARG POSTGRES_DATABASE=postgres

ENV POSTGRES_USER ${POSTGRES_USER}
ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD}
ENV POSTGRES_DB ${POSTGRES_DATABASE}

and action

...
...
...
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          file: ./dockerfiles/postgres-amp-api/Dockerfile
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ steps.meta.outputs.tags }}
          build-args: |
            POSTGRES_USER=${{ secrets.DOCKER_POSTGRES_API_USER }}
            ....

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:

确定