Dockerizing Your Go Gin Project with MySQL: A Step-by-Step Guide

Dockerizing Your Go Gin Project with MySQL: A Step-by-Step Guide

Welcome to this comprehensive guide on Dockerizing Go Gin project! If you’re considering enhancing your application’s security and user experience, we recommend checking out our previous tutorial on Go Gin JWT Authentication, which provides valuable insights into implementing secure authentication.

Dockerizing your Go Gin project allows you to encapsulate your application, its dependencies, and even the database into containers. This guide will walk you through the process, including setting up a MySQL database using Docker.

Why Docker?

Docker is a powerful tool for packaging, distributing, and running applications in isolated environments known as containers. Containerization ensures consistency across different environments, simplifies deployment, and enhances scalability. By Dockerizing your Go Gin project, you can streamline the deployment process and mitigate compatibility issues.

Prerequisites

Before you begin, make sure you have the following tools installed on your machine:

Steps

1. Create Dockerfile

In the root directory of your Go Gin project, create a Dockerfile. This file defines how your Go application should be built and run within a Docker container.

# Use the official Golang image as the base image
FROM golang:latest

# Set the working directory inside the container
WORKDIR /app

# Copy the local code into the container
COPY . .

# Download Go modules
RUN go mod download

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./main"]

This Dockerfile:

  • Uses the official Golang image as a base.
  • Sets the working directory.
  • Copies the local code into the container.
  • Downloads Go modules and builds the application.
  • Exposes port 8080.
  • Defines the command to run the application.

2. Create docker-compose.yml

Create a docker-compose.yml file in the project root to define the services, including the MySQL database.

version: '3.8'

services:
  # Go Gin application service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db

  # MySQL database service
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: mywebapp
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_password
    ports:
      - "3306:3306"

This docker-compose.yml file:

  • Defines two services: app for the Go Gin application and db for the MySQL database.
  • Specifies the build context and Dockerfile for the app service.
  • Configures the MySQL service with environment variables for root password, database name, user, and password.

3. Update Database Configuration

Update your Go application’s database configuration to use the MySQL Docker container. Modify the .env file with the following:

DB_DRIVER=mysql
DB_HOST=db
DB_PORT=3306
DB_USER=db_user
DB_PASSWORD=db_password
DB_NAME=mywebapp

4. Build and Run

Now, build and run the Docker containers using the following commands:

docker-compose build
docker-compose up

This will start your Go Gin application and MySQL database within Docker containers.

5. Access Your Application

Your Go Gin application should be accessible at http://localhost:8080. You can use tools like curl or Postman to interact with your API.

6. Access MySQL Database

To access the MySQL database, you can use a MySQL client or connect to the MySQL Docker container directly. For example:

mysql -h 127.0.0.1 -P 3306 -u db_user -p

Enter the password (db_password) when prompted.

Conclusion

Dockerizing your Go Gin project along with MySQL provides a consistent and isolated environment for development and deployment. It simplifies the setup process and ensures that your application runs consistently across different environments.