- Newest
- Most votes
- Most comments
Hi
you're defining environment variables for the build job (DB_USER and DB_NAME) and then using them during the build process. However, inside the Dockerfile, you're also defining an environment variable for DB_HOST.
Option 1:
In your Github Actions workflow, add DB_HOST to the environment variables with the desired value:
env:
DB_USER: ${{ vars.DB_USER }}
DB_NAME: tickerdb
DB_HOST: your_db_host # Replace with your actual DB host
DockerFile:
ENV DB_PORT=$DB_PORT
ENV DB_NAME=$DB_NAME
ENV DB_USER=$DB_USER
Pass DB_HOST as a build argument during the build step:
- name: Build, tag, and push image to Amazon ECR
...
run: |
docker build --build-arg DB_USER=$DB_USER --build-arg DB_NAME=$DB_NAME --build-arg DB_HOST=$DB_HOST -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
Option 2: Use a multi-stage build
FROM golang:1.19.1 AS builder
WORKDIR /app
COPY go.mod go.sum ./
# Set arguments for the build
ARG DB_USER
ARG DB_NAME
ARG DB_PORT=5454
# Set the environment variable
ENV DB_HOST=mydb.hostname.ap-southeast-2.rds.amazonaws.com
ENV DB_PORT=$DB_PORT
ENV DB_NAME=$DB_NAME
ENV DB_USER=$DB_USER
RUN go mod download
COPY . .
RUN go build -o main .
# Final stage - copy the binary and environment variables
FROM alpine:latest
COPY --from=builder /app/main /app/main
ENV DB_HOST=$DB_HOST
ENV DB_PORT=$DB_PORT
ENV DB_NAME=$DB_NAME
ENV DB_USER=$DB_USER
EXPOSE 8080
CMD ["./main"]
This approach builds the application in a separate stage with the environment variables defined. Then, it copies only the binary and the environment variables to the final, lightweight image. try it and lets see
Thanks for that super fast response GK.
Sorry typo there. Its DB_USER that isn't coming through (have updated my original question).
env: DB_USER: ${{ vars.DB_USER }}
Defined right from the start and appears in all the echos but not in the last part when I check the env variable within Go
Hi Mike, Let me know if you are successful otherwise, please comment the issue again Thanks
Still no luck on this GK.
Option 1 As mentioned above DB_USER is the one not coming through correctly so this doesn't help.
Option 2 This returns an error during the build ERROR: failed to solve: builder: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
One other piece of information to note. This only happens when I use GitHub to build. If I am on my local machine and use: docker build --build-arg DB_USER=tickertime -t backend-app . It passes the environment variable across just fine into the container with my original Dockerfile
Relevant content
- asked 10 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
Curious to know whether renaming
DB_USER
toDB_USER_1
allows your Go program to pick it up and print it. If it does, it would suggest that there's something else in your environment that is causingDB_USER
to be overridden. Also, could you explicitly try:CMD DB_USER=... && ./main
instead of what you have?