连接Go REST API中的Mysql出错,使用Docker Compose。

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

Error connecting Mysql from Go REST API with Docker Compose

问题

我非常新手Docker,我正在尝试使用Docker Compose将Go REST API和MySQL数据库容器化,以便它们可以相互通信。我遇到了错误[main] Error 1049: Unknown database 'puapp'

Docker Compose文件如下:

  1. version: '3'
  2. services:
  3. db:
  4. build: ./mysql/
  5. restart: always
  6. environment:
  7. - MYSQL_ROOT_PASSWORD=root
  8. volumes:
  9. - db_volume:/var/lib/mysql
  10. api-service:
  11. restart: always
  12. build: ./
  13. ports:
  14. - "8080:80"
  15. environment:
  16. - DB_USER=root
  17. - DB_PASS=root
  18. - DB_ADDRESS=db:3306
  19. - DB_PROTOCOL=tcp
  20. - DB_NAME=puapp
  21. depends_on:
  22. - db
  23. links:
  24. - db
  25. volumes:
  26. db_volume:

Go服务的Dockerfile如下:

  1. # syntax=docker/dockerfile:1
  2. # Build stage
  3. FROM golang:1.16-alpine AS builder
  4. WORKDIR /app
  5. COPY . .
  6. RUN go mod download
  7. WORKDIR /app/src/main
  8. RUN go build -o restserv
  9. # Run stage
  10. FROM alpine:3.13
  11. WORKDIR /app
  12. COPY --from=builder /app/src/main/restserv .
  13. EXPOSE 8080
  14. CMD "./restserv"

MySQL的Dockerfile如下:

  1. FROM mysql:latest
  2. ADD dump.sql /docker-entrypoint-initdb.d

完整的代码可以在这里找到:https://github.com/bens-schreiber/restservproj
如果需要添加其他内容,请告诉我。

英文:

I'm very new to Docker, and I'm trying to dockerize a Go REST API and MySQL database to communicate with each other using Docker Compose. I am getting the error [main] Error 1049: Unknown database 'puapp'

Docker compose:

  1. version: '3'
  2. services:
  3. db:
  4. build: ./mysql/
  5. restart: always
  6. environment:
  7. - MYSQL_ROOT_PASSWORD=root
  8. volumes:
  9. - db_volume:/var/lib/mysql
  10. api-service:
  11. restart: always
  12. build: ./
  13. ports:
  14. - "8080:80"
  15. environment:
  16. - DB_USER=root
  17. - DB_PASS=root
  18. - DB_ADDRESS=db:3306
  19. - DB_PROTOCOL=tcp
  20. - DB_NAME=puapp
  21. depends_on:
  22. - db
  23. links:
  24. - db
  25. volumes:
  26. db_volume:

Dockerfile for go service:

  1. # syntax=docker/dockerfile:1
  2. # Build stage
  3. FROM golang:1.16-alpine AS builder
  4. WORKDIR /app
  5. COPY . .
  6. RUN go mod download
  7. WORKDIR /app/src/main
  8. RUN go build -o restserv
  9. # Run stage
  10. FROM alpine:3.13
  11. WORKDIR /app
  12. COPY --from=builder /app/src/main/restserv .
  13. EXPOSE 8080
  14. CMD "./restserv"

Dockerfile for MySQL:

  1. FROM mysql:latest
  2. ADD dump.sql /docker-entrypoint-initdb.d

连接Go REST API中的Mysql出错,使用Docker Compose。

连接Go REST API中的Mysql出错,使用Docker Compose。

Full code - https://github.com/bens-schreiber/restservproj
Let me know if I need to add anything

答案1

得分: 1

容器将拥有自己的IP地址,因此API容器将无法通过127.0.0.1访问MySQL容器。如评论中所述,您希望利用容器的名称从一个容器中访问另一个容器的地址。有关详细信息,请参阅此页面

英文:

Containers will be having their own ip addresses, so API container won't be able to access mysql container over 127.0.0.1. As mentioned in the comments, you want to utilize container's names to addresses from container from another. See this page for details.

huangapple
  • 本文由 发表于 2021年10月29日 05:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69761136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定