Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Created April 25, 2019 11:20
Show Gist options
  • Save wayneashleyberry/bd3ed716ff7dfdb8229aa3252ec258f1 to your computer and use it in GitHub Desktop.
Save wayneashleyberry/bd3ed716ff7dfdb8229aa3252ec258f1 to your computer and use it in GitHub Desktop.
version: '3.6'
services:
database:
restart: always
image: mysql:5.6
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_USER=test
- MYSQL_PASSWORD=test
- MYSQL_DATABASE=test
app:
build: .
restart: always
depends_on:
- database
volumes:
- .:/code
FROM golang
COPY . /code
ENV GO111MODULE on
WORKDIR /code
RUN ["go", "run", "-mod=vendor", "main.go"]
package main
import (
"fmt"
"os"
"time"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
func main() {
conf := mysql.NewConfig()
conf.User = "root"
conf.Passwd = "123456"
conf.Addr = "database:3306"
conf.DBName = "test"
conf.Net = "tcp"
dsn := conf.FormatDSN()
for i := 0; i <= 60; i++ {
_, err := sqlx.Connect("mysql", dsn)
if err == nil {
os.Exit(0)
}
fmt.Println(err)
time.Sleep(1 * time.Second)
}
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment