api | panic: dial tcp: lookup db-mysql: Try again

c3frrgcw  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(171)

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:

  1. version: '3.3'
  2. services:
  3. app:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. container_name: api
  8. ports:
  9. - "8080:8080"
  10. env_file:
  11. - .env
  12. depends_on:
  13. - db
  14. command: ["/app/main"]
  15. db:
  16. image: mysql:5.7
  17. container_name: db-mysql
  18. restart: always
  19. env_file:
  20. - .env
  21. networks:
  22. - mysql-net
  23. ports:
  24. - "3306:3306"
  25. expose:
  26. - 3306
  27. volumes:
  28. - my-db:/var/lib/mysql
  29. - ./init:/docker-entrypoint-initdb.d/
  30. networks:
  31. mysql-net:
  32. driver: bridge
  33. volumes:
  34. my-db:
  1. # Build stage
  2. FROM golang:1.19-alpine3.16 AS builder
  3. WORKDIR /app
  4. COPY . .
  5. RUN go build -o main cmd/server/main.go
  6. # Run stage
  7. FROM alpine:3.16
  8. WORKDIR /app
  9. COPY --from=builder /app/main .
  10. COPY .env .
  11. EXPOSE 8080
  12. CMD [ "/app/main" ]

This is the part I get the error:

  1. err = db.Ping()
  2. if err != nil {
  3. panic(err)
  4. }
y53ybaqx

y53ybaqx1#

Your containers don't have any networks: in common. Your application is on the Compose-provided default network, but the database is on a separate mysql-net network. Since they're not on the same network, they can't talk to each other.
Compose provides a network named default for you and for most application this works fine. The easiest thing to do here is to delete all of the networks: blocks from the entire file.
Once you're doing that cleanup, there are several other things you can simplify. There's a shorter form of build: if you're using the standard Dockerfile name; you don't need to manually specify container_name: or to repeat the image's command: ; the obsolete expose: option does nothing and can be safely removed. This would leave you with:

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. ports:
  6. - "8080:8080"
  7. env_file:
  8. - .env # setting MYSQL_HOST=db
  9. depends_on:
  10. - db
  11. db:
  12. image: mysql:5.7
  13. restart: always
  14. env_file:
  15. - .env
  16. ports:
  17. - "3306:3306"
  18. volumes:
  19. - my-db:/var/lib/mysql
  20. - ./init:/docker-entrypoint-initdb.d/
  21. volumes:
  22. my-db:

I note one further change here. Both the container names and the Compose service names are usable as host names. Normally, though, you can do almost all operations using the Compose service name, and let Compose generate the underlying container name itself. This applies to host names for cross-container calls as well: in your .env file use the Compose service name db as a host name, rather than trying to set container_name: manually.

展开查看全部

相关问题