I have two containers, one of my api in golang and the other of my mysql database... both go up correctly, but when the api connects to the container, I keep getting this try again error. I had adapted the code to try again and it works, but all the operations I perform give the same problem, so I suppose there is something wrong and I don't know what it could be. Can you help me?
As mentioned above, I adapted the code to keep trying until it works, but as it gives an error in all operations, it is kind of impracticable to adapt it to try until it works in all operations.
Here are my files:
version: '3.3'
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: api
ports:
- "8080:8080"
env_file:
- .env
depends_on:
- db
command: ["/app/main"]
db:
image: mysql:5.7
container_name: db-mysql
restart: always
env_file:
- .env
networks:
- mysql-net
ports:
- "3306:3306"
expose:
- 3306
volumes:
- my-db:/var/lib/mysql
- ./init:/docker-entrypoint-initdb.d/
networks:
mysql-net:
driver: bridge
volumes:
my-db:
# Build stage
FROM golang:1.19-alpine3.16 AS builder
WORKDIR /app
COPY . .
RUN go build -o main cmd/server/main.go
# Run stage
FROM alpine:3.16
WORKDIR /app
COPY --from=builder /app/main .
COPY .env .
EXPOSE 8080
CMD [ "/app/main" ]
This is the part I get the error:
err = db.Ping()
if err != nil {
panic(err)
}