docker | database connection refused between two containers

6,720

Solution 1

Your golang api should be included in the compose file and added to the same network.

It also looks like when you run the golang api that you are adding it to a network other than backend, which is the network that is created in your compose file.

Solution 2

In addition to connecting app container with same backend network, you will also need to fix MySQL host parameter in connection string.

Looking at main function you have MySQL host set to localhost

serverName := "localhost:3306"

However, MySQL is not listening at lcoalhost of appc container you will need to change it to MySQL service name in compose file, i.e. db

Share:
6,720

Related videos on Youtube

ma77c
Author by

ma77c

Updated on September 18, 2022

Comments

  • ma77c
    ma77c over 1 year

    I have a mysql database running with the following docker-compose.yml:

    version: '3.3'
        services:
            db:
                image: mysql:5.7
                restart: always
                environment:
                    MYSQL_DATABASE: 'demo'
                    # So you don't have to use root, but you can if you like
                    MYSQL_USER: 'user'
                    # You can use whatever password you like
                    MYSQL_PASSWORD: 'password'
                    # Password for root access
                    MYSQL_ROOT_PASSWORD: 'password'
                ports:
                # <Port exposed> : < MySQL Port running inside container>
                    - '3306:3306'
                expose:
                    # Opens port 3306 on the container
                    - '3306'
                # Where our data will be persisted
                volumes:
                    - my-db:/var/lib/mysql
                networks:
                    - backend
    
    networks:
        backend:
            driver: bridge
    # Names our volume
    volumes:
        my-db:
    

    $ docker-compose build

    $ docker-compose up


    I have a basic golang rest api with the following Dockerfile:

    # Start from golang:1.12-alpine base image
    FROM golang:1.12-alpine
    
    # Adding git, bash and openssh to the image
    RUN apk update && apk upgrade && \
        apk add --no-cache bash git openssh
    
    
    # Set the Current Working Directory inside the container
    WORKDIR /app
    
    
    # Copy the source from the current directory to the Working Directory inside the container
    COPY . .
    RUN go get -d github.com/gorilla/mux
    RUN go get -d github.com/go-sql-driver/mysql
    RUN go get -d github.com/golang-migrate/migrate
    RUN go get -d github.com/golang-migrate/migrate/database/mysql
    RUN go get -d github.com/golang-migrate/migrate/source/file
    
    
    
    # Build the Go app
    RUN go build -o main .
    
    # Expose port 8080 to the outside world
    EXPOSE 8080
    
    # Run the executable
    CMD ["./main"]
    

    and the following function is called:

    func CreateDatabase() (*sql.DB, error) {
        serverName := "localhost:3306"
        user := "user"
        password := "password"
        dbName := "demo"
    
        connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=true&multiStatements=true", user, password, serverName, dbName)
        db, err := sql.Open("mysql", connectionString)
        if err != nil {
            return nil, err
        }
    
        if err := migrateDatabase(db); err != nil {
            return db, err
        }
    
        return db, nil
    }
    

    $ docker run -p 80:8080 --network=<appname>-mysql_backend <imageid>

    $ Database connection failed: %sdial tcp 127.0.0.1:3306: connect: connection refused


    I cannot get the api to establish a database connection with the database container?