0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RedisをDockerで動かす

Posted at

業務でRedisを使う機会があったので、備忘録としてRedisをDockerコンテナで動かす手順を書き起こします。

環境

  • Windows11
  • WSL2 Ubuntu20.04
  • Docker 20.10.22

redisコンテナを起動する

redisイメージを取得する

$ docker pull redis:7.2.4-alpine

※ 2024年2月時点で最新かつ軽量の7.2.4-alpineを利用しています。

redisコンテナを起動する

$ docker run -d --name redis -it redis:7.2.4-alpine
  • -d : コンテナをバックグラウンドで起動する
  • --name : コンテナ名

redisの基本操作

redisコンテナにログインする

$ docker exec -it redis sh

redisを操作する

redis-cliコマンドを使って操作します。redis-cliコマンドを最初に実行して対話モードに切り替えても良いですし、redis-cli [command] でコマンドライン上で操作しても良いです。

ここでは対話モードを使います。

/data # redic-cli
127.0.0.1:6379>
疎通確認
127.0.0.1:6379> ping
PONG
文字列データ
# set <key> <value> でデータを登録
127.0.0.1:6379> set hoge fuga
OK

# get <key> でデータを取得
127.0.0.1:6379> get hoge
"fuga"

# キーが存在しない場合nilが返る
127.0.0.1:6379> get bar
(nil)

# 数値も文字列データとして扱われる
127.0.0.1:6379> set fizz 1
OK
127.0.0.1:6379> get fizz
"1"
ハッシュデータ
# hset <key> <field1> <value1> <field2> <value2> ... <fieldX> <valueX>
127.0.0.1:6379> hset shoes1 brand On model CloudMonster size 26cm
(integer) 3

# hget <key> <field>
127.0.0.1:6379> hget shoes1 model
"CloudMonster"

# hgetall <key>
127.0.0.1:6379> hgetall shoes1
1) "brand"
2) "On"
3) "model"
4) "CloudMonster"
5) "size"
6) "26cm"
データ全削除
127.0.0.1:6379> flushall
OK

127.0.0.1:6379> hget hoge
(nil)

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?