Sling Academy
Home/Golang/Using Go + Docker Compose + MySQL: An Example

Using Go + Docker Compose + MySQL: An Example

Last updated: November 27, 2024

In this article, we'll walk through an example of how to set up a Go application with a MySQL database using Docker Compose. This approach simplifies development by managing dependencies and configurations using containers.

Prerequisites

  • Basic understanding of Go programming language.
  • Docker and Docker Compose installed on your machine.

Step 1: Set Up the Go Application

First, let’s create a simple Go application that will connect to a MySQL database. Start by creating a directory for your project:

mkdir go-docker-example
cd go-docker-example

Create a Go module:

go mod init go-docker-example

Next, create a simple Go application file:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    dsn := "user:password@tcp(mysql:3306)/dbname"
    db, err := sql.Open("mysql", dsn)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }
    fmt.Println("Connected to MySQL database!")
}

This code Example is a very basic Go application that connects to a MySQL database using the Go SQL package and MySQL driver.

Step 2: Create the Docker Compose File

Create a docker-compose.yml file in your project directory:

version: '3.8'

services:
  app:
    build: .
    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dbname
      MYSQL_USER: user
      MYSQL_PASSWORD: password

This YAML configuration defines two services: one for your Go application and another one for MySQL. Ensure to update the credentials as you set in your Go application.

Step 3: Create Dockerfile for Go Application

Create a Dockerfile in the same directory:

FROM golang:1.17 AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o main .

FROM scratch
COPY --from=builder /app/main /
CMD ["/main"]

This Dockerfile uses a multi-stage build, optimizing our Go binary for deployment.

Step 4: Building and Running the Application

With docker-compose.yml and the Dockerfile in place, you can now build and run the application:

docker-compose up

This command starts both the Go app and MySQL within separate containers. After it runs successfully, the Go application will connect to the MySQL database as expected.

Conclusion

By following these steps, you can efficiently manage a Go application and a MySQL database using Docker Compose. This setup makes it easier to handle dependencies, and testing across different environments becomes significantly straightforward thanks to containerization.

Next Article: Caching data with Redis in Go (Golang)

Previous Article: Using Go + Docker Compose + PostgreSQL: An Example

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant