Connection refused

I have a connection issue, connection refused, with connecting to MySQL in one Docker file from a Go app in another Docker file.

version: ‘3’
services:
fullstack-mysql:
container_name: db_mysql
build:
context: ./MySQL
ports:
- 3306:3306
volumes:
- database_mysql:/var/lib/mysql
networks:
- fullstack

app:
container_name: golang_app
env_file:
- ./Go/.env
build:
context: ./Go
ports:
- 9000:9000
restart: on-failure
volumes:
- api:/usr/src/app/
depends_on:
- fullstack-mysql
networks:
- fullstack

phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_container
env_file:
- ./MySQL/.env
depends_on:
- fullstack-mysql
environment:
- PMA_HOST=fullstack-mysql #Note the “mysql”. Must be the name of the what you used as the mysql service.
- PMA_USER=root
- PMA_PORT=3306
- PMA_PASSWORD=root
ports:
- 9090:80
restart: always
networks:
- fullstack

volumes:
api:
database_mysql:

networks:
fullstack:
driver: bridge

./Go/Dockerfile

FROM golang:alpine AS builder
ENV GO111MODULE=on
ENV MYSQL_PASSWORD=$(MYSQL_PASSWORD)
ENV MYSQL_USER=$(MYSQL_USER)
ENV MYSQL_DATABASE=$(MYSQL_DATABASE)
ENV MYSQL_PORT=$(MYSQL_PORT)
ENV MYSQL_DRIVER=$(MYSQL_DRIVER)
RUN mkdir /app
ADD . /app/
WORKDIR /app
COPY ./structs.go .
COPY ./handlers.go .
COPY ./server.go .
COPY ./favicon.ico .
COPY ./assets /assets
RUN go mod init stuff.com
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(ls -1 *.go)
EXPOSE 9000
CMD [“go”, “run”, “.”]

./MySQL/Dockerfile

FROM mysql:8.0
ENV MYSQL_PASSWORD=$(MYSQL_PASSWORD)
ENV MYSQL_USER=$(MYSQL_USER)
ENV MYSQL_DATABASE=$(MYSQL_DATABASE)
ENV MYSQL_PORT=$(MYSQL_PORT)
ENV MYSQL_DRIVER=$(MYSQL_DRIVER)
COPY test.sql /docker-entrypoint-initdb.d/test.sql
EXPOSE 3306

./Go/main.go

func db() error {
//connStr:=config.mysqlUser+":"+config.mysqlPassword+"@tcp(db:"+config.mysqlHost+":"+config.mysqlPort+")/"+config.mysqlDB //Ping Error: dial tcp: lookup db:127.0.0.1:3306: no such host

connStr:=config.mysqlUser+":"+config.mysqlPassword+"@tcp(db:"+config.mysqlPort+")/"+config.mysqlDB //Ping Error: dial tcp 23.195.69.108:3306: connect: connection refused

log.Println(connStr)
db, err := sql.Open(config.mysqlDriver, connStr)
if err != nil {
log.Println(whereami.WhereAmI(), err.Error())
} else {
log.Println(fmt.Sprintf("%s", db), whereami.WhereAmI())
}
defer db.Close()

err = db.Ping()
if err != nil {
log.Println("Ping Error: " + err.Error())
} else {
dbx.conn = db
}

log.Println(fmt.Sprintf("%s", dbx.conn), whereami.WhereAmI())
log.Println(fmt.Sprintf("%T", dbx.conn), whereami.WhereAmI())
return err
}

log.Println(connStr)->docker:docker@tcp(db:3306)/test_db

./Go/.env

MYSQL_ROOT_PASSWORD=root
MYSQL_PASSWORD=docker
MYSQL_USER=docker
MYSQL_DATABASE=test_db
MYSQL_HOST=fullstack-mysql
MYSQL_PORT=3306
MYSQL_DRIVER=mysql

COOKIE_NAME=panda
COOKIE_VALUE=123456987
COOKIE_MAX_AGE=0
COOKIE_PATH=/
COOKIE_HTTP_ONLY=true
DOMAIN=127.0.0.1
PORT=:9000
API_PATH=/api/
API_VERSION=v1
APP_KEY=secret-sauce
PROTOCOL=http://

.Mysql/.env

MYSQL_ROOT_PASSWORD=root
MYSQL_PASSWORD=docker
MYSQL_USER=docker
MYSQL_DATABASE=test_db
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_DRIVER=mysql

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.