Docker + Go + Redis を試す

環境

  • Mac OS 14.6
  • Docker v28.1.1
  • Go v1.25.0
  • Redis v8.8 か v8.7

Redis とは

Redis(レディス)とは、データをディスクではなくメインメモリ上に保存する「インメモリデータベース」です。データの読み書きが圧倒的に高速であるため、Webサイトやアプリの表示速度を上げる「キャッシュ」や、リアルタイム処理に広く活用されています。

だそう。

使うライブラリとか

サンプルコード

go run して確認するだけ。
ファイル抜粋。

docker-compose.yaml

services:
    go-sample-app:
        image: nginx:1.27.0-perl
        ports:
            - 8080:80
        tty: true
        depends_on:
            - go-sample-go
        volumes:
            - ./app:/var/www/html
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        working_dir: /var/www/html
    go-sample-go:
        build:
            context: .
            dockerfile: ./go/Dockerfile
        volumes:
            - ./app:/var/www/html
        working_dir: /var/www/html
    go-sample-redis:
        image: redis:8.8-m03

app/main.go

サンプルコードほぼコピペ。

package main

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
)

var ctx = context.Background()

func main() {
	rdb := redis.NewClient(&redis.Options{
		// Addr:     "localhost:6379",
		Addr:     "go-sample-redis:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	defer rdb.Close()

	err := rdb.Set(ctx, "key", "value", 0).Err()
	if err != nil {
		panic(err)
	}

	val, err := rdb.Get(ctx, "key").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("key", val)

	val2, err := rdb.Get(ctx, "key2").Result()
	if err == redis.Nil {
		fmt.Println("key2 does not exist")
	} else if err != nil {
		panic(err)
	} else {
		fmt.Println("key2", val2)
	}
	// Output: key value
	// key2 does not exist
}

確認する

# go のコンテナ名を確認
$ docker ps

# go のコンテナに入る
$ docker exec -it go-sample-go-sample-go-1 /bin/bash

# 必要であればインストール
$ go mod tidy

# 確認
$ go run main.go
key value
key2 does not exist

おまけ

Additionally, if you want to use your own redis.conf ... - Redis - DockerHub にて、
設定ファイルを読み込ませれるらしい。

redis/Dockerfile

普通にコピペして使うと設定ファイルが見つからないというエラーが起きる。
パスを色々変更してみてもダメだった。

うまく COPY が出来てないぽい。
ここら辺似たような経験した気がするけど思い出せない。

FROM redis
# COPY ががうまくいかないのでコメントアウトした
# COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

エラー内容。

Starting Redis Server
go-sample-redis-1       |  Fatal error, can't open config file '/usr/local/etc/redis/redis.conf': No such file or directory

docker-compose.yaml

COPY がうまくできなかったので、
volumes を使ってみたけど、
別のエラーが出た。

services:
    go-sample-app:
        image: nginx:1.27.0-perl
        ports:
            - 8080:80
        tty: true
        depends_on:
            - go-sample-go
        volumes:
            - ./app:/var/www/html
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        working_dir: /var/www/html
    go-sample-go:
        build:
            context: .
            dockerfile: ./go/Dockerfile
        volumes:
            - ./app:/var/www/html
        working_dir: /var/www/html
    go-sample-redis:
        build:
            context: .
            dockerfile: ./redis/Dockerfile
        volumes:
            - ./redis/redis.conf:/usr/local/etc/redis/redis.conf

エラー内容。
多分 Redis が起動できてない。かも。
設定ファイルのほうで対応した。

Configuration loaded
Failed to write PID file: Permission denied

redis/redis.conf

Redis configuration - Redis から、
できるだけ最新のバージョンの設定ファイル(v8.6)をコピペした。
Docker で試した Redis は v8.8 だった。

以下変更したら接続できた。

bind 127.0.0.1 -::1
↓
bind 0.0.0.0
protected-mode yes
↓
protected-mode no
pidfile /var/run/redis_6379.pid
↓
pidfile ""