0

When I run my docker app and type the following URLs I get the responses shown...

http://localhost:8080/complex

Hello...dial tcp 172.21.0.2:3306: connect: connection refused

I would have expected the connection to be accepted and then be able toe query the database etc.

If I deliberately change the connection string in my main.go file to a non existent host (added an x).

"root:root@tcp(godockerxDB:3306)/task_man")

Use this url...

http://localhost:8080/complex

I get this response...

Hello...dial tcp: lookup godockerxDB on 127.0.0.11:53: no such host

As I would expect, so the original host seems right but it is just refusing the connection.

I have connected to the db container manually and confirmed the database is there and setup correctly with the table and tasks and I am able to query the database form in there.

My main.go...

    package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gorilla/mux"
    "net/http"
)

func simple(w http.ResponseWriter, r *http.Request) {
    enableCors(&w)
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

var db *sql.DB
var err error

func main() {
    fmt.Print("hello")
    router := mux.NewRouter()
    router.HandleFunc("/complex", complex).Methods("GET")
    router.HandleFunc("/text", simple).Methods("GET")
    err := http.ListenAndServe(":8080", router)
    fmt.Print(err)

}
func enableCors(w *http.ResponseWriter) {
    (*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func complex(w http.ResponseWriter, r *http.Request) {
    enableCors(&w)
    fmt.Fprintf(w, "Hello...")
    db, err = sql.Open("mysql", "root:root@tcp(godockerDB:3306)/task_man")
    if err != nil {
        fmt.Fprintf(w, "Hello David")
        fmt.Fprintf(w, err.Error())
    }
    defer db.Close()
    err2 := db.Ping()


    fmt.Fprintf(w, err2.Error())
}

My App Docker File...

> FROM golang:1.12-alpine
> 
> RUN apk add --no-cache git
> 
> # Set the Current Working Directory inside the container WORKDIR /app/godocker
> 
> 
> COPY go.mod . COPY go.sum .
> 
> RUN go mod download
> 
> COPY . .
> 
> # Build the Go app RUN go build -o ./out/godocker .
> 
> 
> # This container exposes port 8080  EXPOSE 8080
> 
> # Run the binary program produced by `go install` CMD ["./out/godocker"]

My db Dockerfile...

FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=root
COPY setup.sql /docker-entrypoint-initdb.d/

My docker-compose.yml ...

# Use root/example as user/password credentials
version: '3.1'

services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: godockerAPP
    ports:
      - "8080:8080"
    depends_on:
      - db      
    restart: always
  db:
    build:
      context: ./dockerfiles/db
      dockerfile: Dockerfile
    container_name: godockerDB
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - "3306:3306"
    restart: always

The command I run to spin up the docker compose...

docker-compose down
docker-compose build
docker-compose up -d

Building db Step 1/3 : FROM mysql:5.7 ---> 718a6da099d8 Step 2/3 : ENV MYSQL_ROOT_PASSWORD=root ---> Using cache ---> 9087b3db47ac Step 3/3 : COPY setup.sql /docker-entrypoint-initdb.d/ ---> Using cache ---> 195614e33d96 Successfully built 195614e33d96 Successfully tagged godocker_db:latest Building app Step 1/11 : FROM golang:1.12 ---> ffcaee6f7d8b Step 2/11 : ENV GO111MODULE=on ---> Using cache ---> 25377bdeb7af Step 3/11 : ENV CGO_ENABLED=0 ---> Using cache ---> fd86c2a9948c Step 4/11 : WORKDIR /app/server ---> Using cache ---> 8d59571272b7 Step 5/11 : COPY go.mod . ---> Using cache ---> d1c8a0f00d33 Step 6/11 : COPY go.sum . ---> Using cache ---> a1b4b294de83 Step 7/11 : RUN go mod download ---> Using cache ---> 803fd4244374 Step 8/11 : COPY . . ---> 37887395cadd Step 9/11 : RUN go build -o main . ---> Running in efe19e8eedc8 Removing intermediate container efe19e8eedc8 ---> a115e9f17fc3 Step 10/11 : EXPOSE 8080:8080 ---> Running in 4155040d490c Removing intermediate container 4155040d490c ---> 3e431bff9e84 Step 11/11 : CMD ["./main"] ---> Running in f5f9bab56ab0 Removing intermediate container f5f9bab56ab0 ---> a8867809f163 Successfully built a8867809f163 Successfully tagged godocker_app:latest

C:\DEV\Go\src\godocker>docker-compose up -d Creating network "godocker_default" with the default driver Creating godockerDB ... done Creating godockerAPP ... done

C:\DEV\Go\src\godocker>dow e03697533be6 239278c1f0ea

C:\DEV\Go\src\godocker>dup

C:\DEV\Go\src\godocker>docker-compose down Removing godockerAPP ... done Removing godockerDB ... done Removing network godocker_default

C:\DEV\Go\src\godocker>docker-compose build Building db Step 1/3 : FROM mysql:5.7 ---> 718a6da099d8 Step 2/3 : ENV MYSQL_ROOT_PASSWORD=root ---> Using cache ---> 9087b3db47ac Step 3/3 : COPY setup.sql /docker-entrypoint-initdb.d/ ---> Using cache ---> 195614e33d96 Successfully built 195614e33d96 Successfully tagged godocker_db:latest Building app Step 1/11 : FROM golang:1.12 ---> ffcaee6f7d8b Step 2/11 : ENV GO111MODULE=on ---> Using cache ---> 25377bdeb7af Step 3/11 : ENV CGO_ENABLED=0 ---> Using cache ---> fd86c2a9948c Step 4/11 : WORKDIR /app/server ---> Using cache ---> 8d59571272b7 Step 5/11 : COPY go.mod . ---> Using cache ---> d1c8a0f00d33 Step 6/11 : COPY go.sum . ---> Using cache ---> a1b4b294de83 Step 7/11 : RUN go mod download ---> Using cache ---> 803fd4244374 Step 8/11 : COPY . . ---> 05a487690611 Step 9/11 : RUN go build -o main . ---> Running in 9d9cc6a3b214 Removing intermediate container 9d9cc6a3b214 ---> ee950a1706f5 Step 10/11 : EXPOSE 8080:8080 ---> Running in faa43abcfe40 Removing intermediate container faa43abcfe40 ---> 877be92dc560 Step 11/11 : CMD ["./main"] ---> Running in dd2dcf9ce4be Removing intermediate container dd2dcf9ce4be ---> cf9bea6d2348 Successfully built cf9bea6d2348 Successfully tagged godocker_app:latest

C:\DEV\Go\src\godocker>docker-compose up -d Creating network "godocker_default" with the default driver Creating godockerDB ... done Creating godockerAPP ... done

C:\DEV\Go\src\godocker>dow d56302aeb09b 0cef3117aac8

C:\DEV\Go\src\godocker>dup

C:\DEV\Go\src\godocker>docker-compose down Removing godockerAPP ... done Removing godockerDB ... done Removing network godocker_default

C:\DEV\Go\src\godocker>docker-compose build Building db Step 1/3 : FROM mysql:5.7 ---> 718a6da099d8 Step 2/3 : ENV MYSQL_ROOT_PASSWORD=root ---> Using cache ---> 9087b3db47ac Step 3/3 : COPY setup.sql /docker-entrypoint-initdb.d/ ---> Using cache ---> 195614e33d96 Successfully built 195614e33d96 Successfully tagged godocker_db:latest Building app Step 1/11 : FROM golang:1.12 ---> ffcaee6f7d8b Step 2/11 : ENV GO111MODULE=on ---> Using cache ---> 25377bdeb7af Step 3/11 : ENV CGO_ENABLED=0 ---> Using cache ---> fd86c2a9948c Step 4/11 : WORKDIR /app/server ---> Using cache ---> 8d59571272b7 Step 5/11 : COPY go.mod . ---> Using cache ---> d1c8a0f00d33 Step 6/11 : COPY go.sum . ---> Using cache ---> a1b4b294de83 Step 7/11 : RUN go mod download ---> Using cache ---> 803fd4244374 Step 8/11 : COPY . . ---> 8a1880bb8b56 Step 9/11 : RUN go build -o main . ---> Running in 84f20ebb4606 Removing intermediate container 84f20ebb4606 ---> e0304d6454d4 Step 10/11 : EXPOSE 8080:8080 ---> Running in 7a0b6392be6e Removing intermediate container 7a0b6392be6e ---> 3b18860bca2c Step 11/11 : CMD ["./main"] ---> Running in 018d6769b721 Removing intermediate container 018d6769b721 ---> bbf448ab621c Successfully built bbf448ab621c Successfully tagged godocker_app:latest

C:\DEV\Go\src\godocker>docker-compose up -d Creating network "godocker_default" with the default driver Creating godockerDB ... done Creating godockerAPP ... done

9
  • 1
    I see in your docker compose file container name is godockerDB (this is nothing but your db DNS name) but in app tries to connect to godockerxDB . Can you please check you have spelling mistake in app DB connection string? Commented Sep 4, 2020 at 8:43
  • i added the x to see how it changes the result, i have include my main.go now and you will see dodockerDB as the host name. Commented Sep 4, 2020 at 8:51
  • Ah ok. Looks like problem is the mysql is not allowing the connection from the IP. Somehow you need to mange to add bind-address = 0.0.0.0 to your [mysqld] section of your /etc/mysql/mysql.conf.d/mysqld.cnf file during mysql deployment. This will allow all the IPs to connect mysql server. Commented Sep 4, 2020 at 9:22
  • Or, you may try adding running 'GRANT ALL ON . to user@'%' IDENTIFIED BY 'password'; ' SQL after deployment. Where user will be your user and password will be root password. Commented Sep 4, 2020 at 9:27
  • 'No such host' is not a connection refusal. Be clear. This is a DNS issue. Commented Sep 4, 2020 at 9:38

1 Answer 1

1

To connect, you have to use the service name declared in your docker-compose.yml instead of container name.

db:3306 // instead of godockerDB:3306
Sign up to request clarification or add additional context in comments.

2 Comments

tried this to no avail, however now found another issue. which was not there earlier I will revert and retry this
Using ‘db’ or ‘godockerDB’ will not make any difference. As Service name and Container Name (if defined) can be used as dns name. Both will be pointing to the same IP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.