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

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

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文件如下:

version: '3'
services:
  db:
    build: ./mysql/
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - db_volume:/var/lib/mysql

  api-service:
    restart: always
    build: ./
    ports:
      - "8080:80"
    environment:
      - DB_USER=root
      - DB_PASS=root
      - DB_ADDRESS=db:3306
      - DB_PROTOCOL=tcp
      - DB_NAME=puapp
    depends_on:
      - db
    links:
      - db

volumes:
  db_volume:

Go服务的Dockerfile如下:

# syntax=docker/dockerfile:1

# Build stage
FROM golang:1.16-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
WORKDIR /app/src/main
RUN go build -o restserv


# Run stage
FROM alpine:3.13
WORKDIR /app
COPY --from=builder /app/src/main/restserv .
EXPOSE 8080

CMD "./restserv"

MySQL的Dockerfile如下:

FROM mysql:latest
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:

version: '3'
services:
  db:
    build: ./mysql/ 
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - db_volume:/var/lib/mysql

  api-service:
    restart: always
    build: ./
    ports:
      - "8080:80"
    environment:
      - DB_USER=root
      - DB_PASS=root
      - DB_ADDRESS=db:3306
      - DB_PROTOCOL=tcp
      - DB_NAME=puapp
    depends_on:
      - db
    links:
      - db

volumes:
  db_volume:

Dockerfile for go service:

# syntax=docker/dockerfile:1

# Build stage
FROM golang:1.16-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
WORKDIR /app/src/main
RUN go build -o restserv


# Run stage
FROM alpine:3.13
WORKDIR /app
COPY --from=builder /app/src/main/restserv .
EXPOSE 8080

CMD "./restserv"

Dockerfile for MySQL:

FROM mysql:latest
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:

确定