I am trying to write a cloud build yaml file to be able to deploy my project on GCE… I have a go project that is running on VM with MySQL connected to it…
I wrote my Dockerfile and docker-compose.yaml file and it’s working perfectly… So I decided to move it to cloud run on GCP but getting into the issue that I am unable to connect my go project with mysql…
The below configuration:
docker-compose.yaml
version: '3.8'
services:
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_USER: ${DB_ROOT_USER}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_TCP_PORT: ${DB_PORT}
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
app:
build: .
ports:
- "8080:8080"
depends_on:
- mysql
environment:
DB_HOST: ${DB_HOST}
DB_DATABASE: ${DB_DATABASE}
DB_USER: ${DB_ROOT_USER}
DB_PASSWORD: ${DB_ROOT_PASSWORD}
DB_PORT: ${DB_PORT}
volumes:
db_data:
cloudbuild.yaml:
steps:
- name: "gcr.io/cloud-builders/docker"
args: [ "build", "-t", "gcr.io/benergy/go-ecommerce", ".", "-f", "Dockerfile" ]
- name: "gcr.io/cloud-builders/docker"
args: [ "push", "gcr.io/benergy/go-ecommerce" ]
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'sh'
args:
- '-c'
- 'gcloud run deploy go-ecommerce --image gcr.io/benergy/go-ecommerce:latest
--region us-central1
--set-env-vars DB_USER=$$DB_USER
--set-env-vars DB_PASSWORD=$$DB_PASSWORD
--set-env-vars DB_ROOT_USER=$$DB_ROOT_USER
--set-env-vars DB_ROOT_PASSWORD=$$DB_ROOT_PASSWORD
--set-env-vars DB_PORT=$$DB_PORT
--set-env-vars DB_HOST=$$DB_HOST
--set-env-vars DB_DATABASE=$$DB_DATABASE
--cpu 1 --memory 1024Mi --port $$DB_PORT --allow-unauthenticated --platform managed
'
secretEnv: [
'DB_USER',
'DB_PASSWORD',
'DB_ROOT_USER',
'DB_ROOT_PASSWORD',
'DB_PORT',
'DB_HOST',
'DB_DATABASE'
]
- name: 'gcr.io/cloud-builders/docker-compose'
args: ['up', '--build', '--no-cache', '--detach']
availableSecrets:
secretManager:
- versionName: projects/859473192921/secrets/DB_USER/versions/latest
env: 'DB_USER'
- versionName: projects/859473192921/secrets/DB_PASSWORD/versions/latest
env: 'DB_PASSWORD'
- versionName: projects/859473192921/secrets/DB_ROOT_USER/versions/latest
env: 'DB_ROOT_USER'
- versionName: projects/859473192921/secrets/DB_ROOT_PASSWORD/versions/latest
env: 'DB_ROOT_PASSWORD'
- versionName: projects/859473192921/secrets/DB_PORT/versions/latest
env: 'DB_PORT'
- versionName: projects/859473192921/secrets/DB_HOST/versions/latest
env: 'DB_HOST'
- versionName: projects/859473192921/secrets/DB_DATABASE/versions/latest
env: 'DB_DATABASE'
Dockerfile:
FROM golang:1.22.2-alpine
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
COPY ./entrypoint.sh /entrypoint.sh
RUN apk update && apk add --no-cache make protobuf-dev
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
RUN protoc --go_out=./ models/proto/cachemodels/*.proto
# Install Compile Daemon for go. We'll use it to watch changes in go files
RUN go install -mod=mod github.com/githubnemo/CompileDaemon
# wait-for-it requires bash, which alpine doesn't ship with by default. Use wait-for instead
ADD https://raw.githubusercontent.com/eficode/wait-for/v2.1.0/wait-for /usr/local/bin/wait-for
RUN chmod +rx /usr/local/bin/wait-for /entrypoint.sh
ENTRYPOINT [ "sh", "/entrypoint.sh" ]
entrypoint.sh:
wait-for "${DB_HOST}:${DB_PORT}" -- "$@"
# Watch your .go files and invoke go build if the files changed.
CompileDaemon --build="go build -o ecommerce main.go" --command=./ecommerce
The nice part is that the env variables are correctly replaced, but the issue I am getting
through logs in go project is:
"error":"dial tcp: lookup mysql on 169.254.169.254:53: no such host", "level":"warn", "logatag":"Could not initialize mysql connection", "msg":"Could not initialize mysql connection", "ts":1.717317656117359E9}
Kindly help me please on this as I am not sure if I did something wrong !