简体   繁体   English

Docker-compose api 和数据库配置

[英]Docker-compose api and database configuration

I have a problem with connecting Api with MySQL database running in containers.我在将 Api 与在容器中运行的 MySQL 数据库连接时遇到问题。 I have Dockerfile for Golang Api:我有 Golang Api 的 Dockerfile:

FROM golang:latest
 WORKDIR /app
 COPY go.mod go.sum ./
RUN go mod download
 COPY . .
WORKDIR /app/bin
 EXPOSE 8080
 RUN go run ./../cmd/web/

I usually connect with database in the application using database/sql:我通常使用 database/sql 连接应用程序中的数据库:

dsn = "user1:pass@tcp(wpmysql:3306)/wp?parseTime=true"
db, err := sql.Open("mysql", dsn)

My docker-compose.yml:我的 docker-compose.yml:

version: '3'
services:
  db:
    image: mysql:5.7
    container_name: ${MYSQL_CONTAINER_NAME}
    ports: 
        - 3306:3306
    command: --init-file /usr/src/app/init.sql
    volumes:
        - ./init.sql:/usr/src/app/init.sql
    environment:
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASS}
        - MYSQL_DATABASE=${MYSQL_DB}
        - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
    networks:
        - fullstack
  web:
    container_name: wpapi
    build: .
    ports:
        - 8080:8080
    restart: on-failure
    volumes:
        - .:/usr/src/app/
    depends_on:
        - db
    networks:
        - fullstack

networks:
    fullstack:
        driver: bridge

In the same directory as docker-compose.yml is file .env:在与 docker-compose.yml 相同的目录中是文件 .env:

DB_PASSWORD=pass
MYSQL_PORT=3306
MYSQL_USER=user1
MYSQL_PASS=pass
MYSQL_DB=wp
MYSQL_CONTAINER_NAME=wpmysql

After call commends:通话后称赞:

$ docker-compose up -d db
$ docker-compose build web

I get error ERROR main.go:46: dial tcp: lookup wpmysql on 37.8.214.2:53: no such host .我收到错误ERROR main.go:46: dial tcp: lookup wpmysql on 37.8.214.2:53: no such host List of containers looks like:容器列表如下所示:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
9fbaf67df5bf        2778fcda2046        "/bin/sh -c 'go run …"   14 seconds ago      Up 13 seconds       8080/tcp                            mystifying_shannon
7f6c76cc9c4f        mysql:5.7           "docker-entrypoint.s…"   40 minutes ago      Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   wpmysql

Moreover when I try to connect in application by dsn = "user1:pass@tcp(localhost:3306)/wp?parseTime=true" or dsn = "root:pass@tcp(localhost:3306)/wp?parseTime=true" I get another error: dial tcp 127.0.0.1:3306: connect: connection refused although I can go into container ( docker exec -it wpmysql bash -l ) and sign in with root and user1 credentials此外,当我尝试通过dsn = "user1:pass@tcp(localhost:3306)/wp?parseTime=true"dsn = "root:pass@tcp(localhost:3306)/wp?parseTime=true"我收到另一个错误: dial tcp 127.0.0.1:3306: connect: connection refused docker exec -it wpmysql bash -l尽管我可以进入容器( docker exec -it wpmysql bash -l )并使用rootuser1凭据sign in

In your docker file you have:在您的 docker 文件中,您有:

RUN go run ./../cmd/web/

This will attempt to build AND run your executable during the build process.这将尝试在构建过程中构建和运行您的可执行文件。 The network fullstack is not available at this time.网络全fullstack不可用。 I think you probably meant to use:我想你可能打算使用:

CMD go run ../cmd/web/

This will set the default command run when you start (ie docker-compose up ) the container to go run ../cmd/web/ .这将设置在您启动(即docker-compose up )容器时运行的默认命令go run ../cmd/web/ Even better would be:更好的是:

RUN go build ../cmd/web/
CMD ../cmd/web/web

This will build your application as part of the process of building the container and then set the executable produced as the default command.这将构建您的应用程序作为构建容器过程的一部分,然后将生成的可执行文件设置为默认命令。 The benefit of doing this is that compile errors become apparent when you build the image (and it means the application is not built every time you start the container).这样做的好处是编译错误在您构建映像时变得明显(这意味着不会在每次启动容器时构建应用程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM