Tezza Fazar
Tezza Fazar

Reputation: 17

how to solve this error on Golang running with MySQL in Docker connection refused

I've searched and used this method but it still doesn't work, the connection doesn't work Golang MySQL Docker connection refused

my Dockerfile

# Start from golang base image
FROM golang:alpine as builder

# ENV GO111MODULE=on

# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git

# Set the current working directory inside the container 
WORKDIR /app

# Copy go mod and sum files 
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed 
RUN go mod download 

# Copy the source from the current directory to the working Directory inside the container 
COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Start a new stage from scratch
FROM alpine:latest
RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .
COPY --from=builder /app/.env .       

# Expose port 8080 to the outside world
EXPOSE 2345

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

Docker-compose.yml

version: '3.9'
services:
  app:
    container_name: golang_api_container
    build: .
    ports: 
      - 5000:2345 
    restart: on-failure
    volumes:
      - api:/usr/src/app/
    depends_on:
      - golang-mysql          
    networks:
      - fullstack


  golang-mysql:
    image: mysql:5.7
    container_name: db_mysql_container
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 12345
    ports: 
      - 3306:3306 
    volumes:
      - database_mysql:/var/lib/mysql
    networks:
      - fullstack
  
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin_container
    depends_on:
      - golang-mysql
    environment:
      - PMA_HOST=golang-mysql # Note the "golang-mysql". Must be the name of the what you used as the mysql service.
      # - PMA_USER=root
      # - PMA_PORT=${DB_PORT}
      # - PMA_PASSWORD=
      - PMA_ARBITRARY=1
    ports:
      - 3001:80
    restart: always
    networks:
      - fullstack


volumes:
  api:
  database_mysql:                  

# Networks to be created to facilitate communication between containers
networks:
  fullstack:
    driver: bridge

.env file

# Mysql Live
DB_HOST=db_mysql_container                      
# DB_HOST=127.0.0.1                           # when running the app without docker 
DB_DRIVER=mysql 
API_SECRET=sipil_api2022                          # Used for creating a JWT. Can be anything 
DB_USER=root
DB_PASSWORD=12345
DB_NAME=macan
DB_PORT=3306
# DB_PASSWORD_ROOT=root


# Mysql Test
TEST_DB_HOST=db_mysql_container                        
# TEST_DB_HOST=127.0.0.1                       # when running the app without docker 
TEST_DB_DRIVER=mysql
TEST_API_SECRET=sipil_api2022
TEST_DB_USER=root
TEST_DB_PASSWORD=12345
TEST_DB_NAME=macan_test
TEST_DB_PORT=3306
# DB_PASSWORD_ROOT=root

my db-config.go

func SetupDBConnection() *gorm.DB { 
    errEnv := godotenv.Load() 
    if errEnv != nil {
        panic("Failed to load env file")
    }

    dbUser := os.Getenv("DB_USER")     
    dbPass := os.Getenv("DB_PASSWORD") 
    dbHost := os.Getenv("DB_HOST")     
    dbName := os.Getenv("DB_NAME")     

    dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbName) 
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})                                                             
    if err != nil {
        log.Println("Failed to create a connection to DB")
    } else {
        log.Println("Connection Established to DB")
    }
    return db
}

Still error

enter image description here

I've adjusted the DB_HOST to DB_HOST=db_mysql_container, because it matches the name on the docker container for mysql, but it still doesn't connect and the connection is refused.

Upvotes: 1

Views: 1256

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

When you are deploying a stack/compose, then you should communicate over the services name and not the container name

So your DB_HOST should be just golang-mysql. Also in production, you don't want to map the port 3306:3306 to the host.

Upvotes: 1

Related Questions