I'm trying to build a simplified example of an app which uses username-and-password authentication to connect to Cassandra. To this end, I've adapted the docker-compose.yml
from https://hub.docker.com/r/bitnami/cassandra/ as follows:
version: '2'
services:
cassandra:
image: 'docker.io/bitnami/cassandra:3-debian-10'
ports:
- '7000:7000'
- '9042:9042'
volumes:
- 'cassandra_data:/bitnami'
environment:
- CASSANDRA_SEEDS=cassandra
- CASSANDRA_PASSWORD_SEEDER=yes
- CASSANDRA_PASSWORD=cassandra
backend:
build: .
environment:
- CASSANDRA_USERNAME=cassandra
- CASSANDRA_PASSWORD=cassandra
volumes:
cassandra_data:
driver: local
where my directory structure is
.
├── Dockerfile
├── docker-compose.yml
├── go.mod
├── go.sum
└── main.go
the multi-stage Dockerfile
is
FROM golang AS builder
WORKDIR /go/src/app/
COPY . .
RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/app/app .
CMD ["./app"]
and main.go
attempts to create a Cassandra session using gocql
:
package main
import (
"log"
"os"
"github.com/gocql/gocql"
)
func main() {
cluster := gocql.NewCluster("cassandra")
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: os.Getenv("CASSANDRA_USERNAME"),
Password: os.Getenv("CASSANDRA_PASSWORD"),
}
if _, err := cluster.CreateSession(); err != nil {
log.Fatalf("CreateSession: %v", err)
}
}
The username and password have both been set to the default values of "cassandra" as in the "Connecting to other containers" example in https://hub.docker.com/r/bitnami/cassandra/.
The problem is that if I try to run this multi-container application using docker-compose build
followed by docker-compose up
, I get the following error:
backend_1 | 2020/11/04 15:10:34 CreateSession: gocql: unable to create session: unable to discover protocol version: dial tcp 172.18.0.3:9042: connect: connection refused
I don't understand what is going wrong here: the hostname, port number, username, and password all seem correct. Any ideas why the connection is refused?