Panic: dial tcp 192.168.160.2:3305: connect: connection refused **NEEDED HELP**

Every time, I run “docker-compose up” I get this error

//Error

> $ docker-compose up --build
Creating network "bucket-api_fullstack" with driver "bridge"
Building bucket
Step 1/17 : FROM golang:1.15-alpine as builder
 ---> 3dae2ccc15b8        
Step 2/17 : RUN mkdir /app
 ---> Using cache
 ---> 70dd1c583533        
Step 3/17 : ADD . /app    
 ---> Using cache
 ---> 4586c50cca46
Step 4/17 : WORKDIR /app
 ---> Using cache
 ---> c3a943c24b9e
Step 5/17 : RUN go clean --modcache
 ---> Using cache
 ---> 200b57f5142e
Step 6/17 : RUN go mod download
 ---> Using cache
 ---> 25e6672cddbe
Step 7/17 : RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./src/
 ---> Using cache
 ---> 625a83d39c9d

Step 8/17 : FROM alpine:latest
 ---> e50c909a8df2
Step 9/17 : RUN apk --no-cache add ca-certificates
 ---> Using cache
 ---> 9fb84fe65803
Step 10/17 : RUN apk add --no-cache git make musl-dev go
 ---> Using cache
 ---> 81520138b2d7
Step 11/17 : COPY --from=builder /app/main .
 ---> Using cache
 ---> c285894862aa
Step 12/17 : ENV GOROOT /usr/lib/go
 ---> Using cache
 ---> c5a2790f18cb
Step 13/17 : ENV GOPATH /go
 ---> Using cache
 ---> 8fdf6d244cd1
Step 14/17 : ENV PATH /go/bin:$PATH
 ---> Using cache
 ---> a40c53d193c1
Step 15/17 : RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
 ---> Using cache
 ---> b199ed72e3f5
Step 16/17 : EXPOSE 8084
 ---> Using cache
 ---> 4ab9ad5a24af
Step 17/17 : CMD ["./main"]
 ---> Using cache
 ---> 4f1478c4b83f

Successfully built 4f1478c4b83f
Successfully tagged bucketapi:latest
Creating bucket-database ... done
Creating bucket-api      ... done
Attaching to bucket-database, bucket-api
bucket-api      | rbucket pagal!123 bucket-database 3305 rb_db
bucket-api      | panic: dial tcp 192.168.160.2:3305: connect: connection refused
bucket-api      |
bucket-api      | goroutine 1 [running]:
bucket-api      | 
bucket-api      |       /app/src/datasources/mysql/bucket_db/bucket_db.go:51 +0x554
bucket-database | 2021-02-12 11:50:17+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-12 11:50:17+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
bucket-database | 2021-02-12 11:50:17+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-12T11:50:18.393432Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
bucket-database | 2021-02-12T11:50:18.941771Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
bucket-database | 2021-02-12T11:50:23.269865Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
bucket-database | 2021-02-12T11:50:23.974635Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
bucket-database | 2021-02-12T11:50:24.336745Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
bucket-database | 2021-02-12T11:50:24.357326Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
bucket-database | 2021-02-12T11:50:25.059647Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
bucket-database | 2021-02-12T11:50:25.060266Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
bucket-database | 2021-02-12T11:50:25.191338Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
bucket-database | 2021-02-12T11:50:25.327133Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/run/mysqld/mysqld.sock' 
 port: 3306  MySQL Community Server - GPL.
bucket-api exited with code 2

THIS MY DOCKER-COMPOSE FILE

>  version: '3.8'
> 
> services: 
>   # MYSQL DATABASE DOCKER
>   bucket-mysql:
>     image: mysql:latest
>     container_name: bucket-database
>     command: --default-authentication-plugin=mysql_native_password
>     ports: 
>       - "127.0.0.1:3305:3306"
>     environment: 
>       MYSQL_ROOT_PASSWORD: "pagal!123"
>       MYSQL_USER: "rbucket"
>       MYSQL_PASSWORD: "root"
>       MYSQL_DATABASE: "rb_db"
>     networks: 
>       - fullstack
>     volumes: 
>       - database_mysql:/var/lib/mysql
>     restart: always
>     cap_add: 
>       - SYS_NICE
>     healthcheck: 
>       test: ["CMD-SHELL", "echo 'select 1' |mysql -u root -p'pagal!123' --silent"]
>       interval: 30s
>       timeout: 20s
>       retries: 6
>   
>   #API DOCKER 
>   bucket:
>     image: bucketapi
>     build: . 
>     container_name: bucket-api
>     environment: 
>       MYSQL_ROOT_PASSWORD: "pagal!123"
>       MYSQL_USER: "rbucket"
>       MYSQL_PASSWORD: "pagal!123"
>       MYSQL_DATABASE: "rb_db"
>       MYSQL_HOST: "bucket-database"
>       MYSQL_PORT: "3305"
>     ports: 
>       - "8084:8084"
>     # restart: on-failure
>     # volumes: 
>     #   - ./src:/usr/src/app/
>     depends_on: 
>       - bucket-mysql
>     networks: 
>       - fullstack
> 
> 
> volumes: 
>     database_mysql:
>   
> networks: 
>   fullstack:
>     driver: bridge

THIS IS MY DOCKER FILE

> #Builder image build the go binary : Setting the alias as builder
> 
> FROM golang:1.15-alpine as builder
> 
> RUN mkdir /app
> 
> # add this file directory to the image
> 
> ADD . /app
> 
> # directory where the source file execution takes place
> 
> WORKDIR /app
> 
> # RUN some go commands
> 
> RUN go clean --modcache
> 
> RUN go mod download
> 
> RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./src/
> 
> # Our production image used to run our app
> 
> FROM alpine:latest
> 
> RUN apk --no-cache add ca-certificates
> 
> RUN apk add --no-cache git make musl-dev go
> 
> COPY --from=builder /app/main .
> 
> # Configure GO
> 
> ENV GOROOT /usr/lib/go
> 
> ENV GOPATH /go
> 
> ENV PATH /go/bin:$PATH
> 
> RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
> 
> EXPOSE 8084
> 
> CMD ["./main"]

IAM GETTING THE ERROR FROM THIS FILE

> //client info
> 
> var (
> 
>     client *sql.DB
> 
> )
> 
> func init() {
> 
>     // loaDerr := godotenv.Load()
> 
>     // if loaDerr != nil {
> 
>     //  log.Fatal("Error loading .env file")
> 
>     // }
> 
>     username := os.Getenv("MYSQL_USER")
> 
>     password := os.Getenv("MYSQL_ROOT_PASSWORD")
> 
>     host := os.Getenv("MYSQL_HOST")
> 
>     port := os.Getenv("MYSQL_PORT")
> 
>     schema := os.Getenv("MYSQL_DATABASE")
> 
>     fmt.Println(username, password, host, port, schema)
> 
>     dataSourceName := fmt.Sprintf(
> 
>         "%s:%s@tcp(%s:%s)/%s?charset=utf8",
> 
>         username,
> 
>         password,
> 
>         host,
> 
>         port,
> 
>         schema,
> 
>     )
> 
>     var err error
> 
>     //connect to the database server
> 
>     client, err = sql.Open("mysql", dataSourceName)
> 
>     if err != nil {
> 
>         panic(err)
> 
>     }
> 
>     //checking the connection
> 
>     if connectionErr := client.Ping(); connectionErr != nil {
> 
>         panic(connectionErr)
> 
>     }
> 
>     fmt.Println("Database connection is been established succesfully")
> 
> }
> 
> //GetClient : this method returns the connected client database
> 
> func GetClient() *sql.DB {
> 
>     return client
> 
> }

I’m not familiar with Docker, so maybe this is irrelevant, but:

This looks to me like MySQL will only listen to incoming connections from the MySQL server itself. Is there some way in Docker that you can listen on any IP or listen to the IP provided by Docker (I think Docker does that)? Perhaps just ":3305:3306"?

I have tried it earlier, it giving me a same error as always

Does this help? https://stackoverflow.com/questions/41637013/docker-mysql-on-different-port/41637060

1 Like

Whom does this IP belong to? Does it belong to the database which has its first output only after your program has already died?

Well, make your program depend on the database container and also add some generous sleep before starting your application, as docker-compose has no notion of a containers “readiness”.

I think this is the IP of Mysql Container, Because I have mention the mysql container name attach with port, can you please provide me a reference regarding this issue…it will be a big help for me, thankuu

sorry, skillian, but this is not helping me, Thankuu

I’m on mobile, just check the documentation for “depends”, to make your application wait for the database to start, also instead of panicking because of the unavailable database, you should probably retry from your application.

I have added the “WAIT” in docker compose and DOCKERFILE , but its not working, Every time it prints this OUTPUT

$ docker-compose up
Creating network "bucket-api_fullstack" with driver "br
Creating bucket-database ... done
Creating bucket-api      ... done
Attaching to bucket-database, bucket-api
bucket-api      | -------------------------------------
bucket-api      |  docker-compose-wait 2.7.2
bucket-api      | ---------------------------
bucket-api      | Starting with configuration:
bucket-api      |  - Hosts to be waiting for: [bucket-m
bucket-api      |  - Timeout before failure: 300 second
bucket-api      |  - TCP connection timeout before retr
bucket-api      |  - Sleeping time before checking for 
bucket-api      |  - Sleeping time once all hosts are a
bucket-api      |  - Sleeping time between retries: 30 
bucket-api      | -------------------------------------
bucket-api      | Checking availability of bucket-mysql
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Entd user 'mysql'
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13T06:01:45.306754Z 0 [Systemn/mysqld (mysqld 8.0.23) starting as process 1
bucket-database | 2021-02-13T06:01:45.415685Z 1 [Systemnitialization has started.
bucket-database | 2021-02-13T06:01:47.738724Z 1 [Systemnitialization has ended.
bucket-database | 2021-02-13T06:01:48.172811Z 0 [System ready for connections. Bind-address: '::' port: 33060,.sock
bucket-database | 2021-02-13T06:01:48.350857Z 0 [System XA crash recovery...
bucket-database | 2021-02-13T06:01:48.357430Z 0 [System recovery finished.
bucket-database | 2021-02-13T06:01:48.743684Z 0 [Warninificate ca.pem is self signed.
bucket-database | 2021-02-13T06:01:48.744238Z 0 [System
mysql_main configured to support TLS. Encrypted connect channel.
bucket-database | 2021-02-13T06:01:48.918000Z 0 [Warnine configuration for --pid-file: Location '/var/run/mysqo all OS users. Consider choosing a different directory
bucket-database | 2021-02-13T06:01:49.233036Z 0 [Systemn/mysqld: ready for connections. Version: '8.0.23'  socck'  port: 3306  MySQL Community Server - GPL.
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Timeout! After 300 seconds some hosts
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Timeout! After 300 seconds some hosts

I have DONE SOME CHANGES IN THE DOCKERFILE and DOCKER-COMPOSE FILE

> DOCKERFILE

#Builder image build the go binary : Setting the alias as builder
FROM golang:1.15-alpine as builder
RUN mkdir /app

add this file directory to the image

ADD . /app

directory where the source file execution takes place

WORKDIR /app

RUN some go commands

RUN go clean --modcache
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./src/

Our production image used to run our app

FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add --no-cache git make musl-dev go
COPY --from=builder /app/main .

Add docker-compose-wait tool -------------------

ENV WAIT_VERSION 2.7.2
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait

Configure GO

ENV GOROOT /usr/lib/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH

RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
EXPOSE 8084
CMD [“./main”]

> DOCKER-COMPOSE FILE

version: '3.8'

services: 

  # MYSQL DATABASE DOCKER

  bucket-mysql:

    image: mysql:latest

    container_name: bucket-database

    command: --default-authentication-plugin=mysql_native_password

    ports: 

      - "3305:3306"

    expose: 

      - "3305"

    environment: 

      MYSQL_ROOT_PASSWORD: "pagal!123"

      MYSQL_USER: "rbucket"

      MYSQL_PASSWORD: "root"

      MYSQL_DATABASE: "rb_db"

    networks: 

      - fullstack

    volumes: 

      - database_mysql:/var/lib/mysql

    restart: always

    cap_add: 

      - SYS_NICE

    healthcheck: 

      test: ["CMD-SHELL", "echo 'select 1' |mysql -u root -p'pagal!123' --silent"]

      interval: 30s

      timeout: 20s

      retries: 6

  

  #API DOCKER 

  bucket:

    image: bucketapi

    build: . 

    container_name: bucket-api

    command: sh -c "/wait"

    environment:

      WAIT_HOSTS: bucket-mysql:3305

      WAIT_HOSTS_TIMEOUT: 300

      WAIT_SLEEP_INTERVAL: 30

      WAIT_HOST_CONNECT_TIMEOUT: 30

      MYSQL_ROOT_PASSWORD: "pagal!123"

      MYSQL_USER: "rbucket"

      MYSQL_PASSWORD: "pagal!123"

      MYSQL_DATABASE: "rb_db"

      # MYSQL_HOST: "bucket-database"

      # MYSQL_PORT: "3305"

    ports: 

      - "8084:8084"

    restart: on-failure

    # volumes: 

    #   - ./src:/usr/src/app/

    depends_on: 

      - bucket-mysql

    networks: 

      - fullstack

volumes: 

    database_mysql:

  

networks: 

  fullstack:

    driver: bridge

> MYSQL CONNECTION FILE

func init() {

    // loaDerr := godotenv.Load()

    // if loaDerr != nil {

    //  log.Fatal("Error loading .env file")

    // }

    username := os.Getenv("MYSQL_USER")

    password := os.Getenv("MYSQL_ROOT_PASSWORD")

    host := os.Getenv("WAIT_HOSTS")

    port := os.Getenv("MYSQL_PORT")

    schema := os.Getenv("MYSQL_DATABASE")

    fmt.Println(username, password, host, port, schema)

    dataSourceName := fmt.Sprintf(

        "%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",

        username,

        password,

        host,

        schema,

    )

    var err error

    //connect to the database server

    client, err = sql.Open("mysql", dataSourceName)

    if err != nil {

        panic(err)

    }

    //checking the connection

    if connectionErr := client.Ping(); connectionErr != nil {

        panic(connectionErr.Error())

    }

    fmt.Println("Database connection is been established succesfully")

}

Help me In this… Please (I have being trying every thing but its not working, if I connect directly the mysql workbench with the mysql container, It connects normally but it giving me an error if I do connection through code)

I have never used this wait tool.

Usually I just do a sleep 30.

Or I just add some proper error handling in the client, that makes it retry to connect to the database with an exponential backoff.

1 Like

ohk…Thanku Nobbz you have been a great help for me… docker container is running fine…thanks for your help :slight_smile:

I think you can try use port 3306 to connect MySQL container.
Like your latest config of ports ("3305:3306") you may can connect mysql server by <your_host>:3305 and <mysql_container_ip>:3306.

1 Like

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