docker安装redis命令及运行

docker安装redis:

docker run -d -p 6379:6379 --name redis redis:latest

-d: 以 守护进程模式 运行容器,容器启动后会进入后台运行,并脱离当前命令行会话。

-p: 显示端口号。

-p 6379:6379: 将容器内部的 6379 端口映射到宿主机 6379 端口,可以直接从宿主机访问容器内部的 Redis 服务。

--name redis: 为容器指定名称为 redis。

redis:latest: 指定镜像版本,本例中使用的是官方 Redis 镜像的最新版本。

输入docker ps查看:

C:\WINDOWS\system32>docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED      STATUS         PORTS                    NAMES
9b49cbee1854   redis:latest   "docker-entrypoint.s…"   2 days ago   Up 3 minutes   0.0.0.0:6379->6379/tcp   redis

C:\WINDOWS\system32>

检查docker日志,输入docker logs redis:

C:\WINDOWS\system32>docker logs redis
1:C 04 May 2024 10:16:00.691 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 04 May 2024 10:16:00.691 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 04 May 2024 10:16:00.692 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 04 May 2024 10:16:00.692 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 04 May 2024 10:16:00.692 * monotonic clock: POSIX clock_gettime
1:M 04 May 2024 10:16:00.693 * Running mode=standalone, port=6379.
1:M 04 May 2024 10:16:00.693 * Server initialized
1:M 04 May 2024 10:16:00.693 * Ready to accept connections tcp
1:M 04 May 2024 11:16:01.012 * 1 changes in 3600 seconds. Saving...
1:M 04 May 2024 11:16:01.013 * Background saving started by pid 28
28:C 04 May 2024 11:16:01.033 * DB saved on disk
28:C 04 May 2024 11:16:01.033 * Fork CoW for RDB: current 0 MB, peak 0 MB, average 0 MB
1:M 04 May 2024 11:16:01.113 * Background saving terminated with success
1:signal-handler (1714827307) Received SIGTERM scheduling shutdown...
1:M 04 May 2024 12:55:07.346 * User requested shutdown...
1:M 04 May 2024 12:55:07.346 * Saving the final RDB snapshot before exiting.
1:C 06 May 2024 12:08:59.747 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 May 2024 12:08:59.747 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 May 2024 12:08:59.747 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 May 2024 12:08:59.748 * monotonic clock: POSIX clock_gettime
1:M 06 May 2024 12:08:59.750 * Running mode=standalone, port=6379.
1:M 06 May 2024 12:08:59.751 * Server initialized
1:M 06 May 2024 12:08:59.753 * Loading RDB produced by version 7.2.4
1:M 06 May 2024 12:08:59.753 * RDB age 170032 seconds
1:M 06 May 2024 12:08:59.753 * RDB memory usage when created 0.90 Mb
1:M 06 May 2024 12:08:59.753 * Done loading RDB, keys loaded: 1, keys expired: 0.
1:M 06 May 2024 12:08:59.753 * DB loaded from disk: 0.002 seconds
1:M 06 May 2024 12:08:59.753 * Ready to accept connections tcp
1:C 06 May 2024 12:36:43.078 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 May 2024 12:36:43.078 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 May 2024 12:36:43.078 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 May 2024 12:36:43.079 * monotonic clock: POSIX clock_gettime
1:M 06 May 2024 12:36:43.081 * Running mode=standalone, port=6379.
1:M 06 May 2024 12:36:43.082 * Server initialized
1:M 06 May 2024 12:36:43.084 * Loading RDB produced by version 7.2.4
1:M 06 May 2024 12:36:43.084 * RDB age 171696 seconds
1:M 06 May 2024 12:36:43.084 * RDB memory usage when created 0.90 Mb
1:M 06 May 2024 12:36:43.084 * Done loading RDB, keys loaded: 1, keys expired: 0.
1:M 06 May 2024 12:36:43.084 * DB loaded from disk: 0.002 seconds
1:M 06 May 2024 12:36:43.084 * Ready to accept connections tcp

C:\WINDOWS\system32>

若想运行redis,直接输入redis-cli -h localhost则会出错:

C:\WINDOWS\system32>redis-cli -h localhost
'redis-cli' 不是内部或外部命令,也不是可运行的程序或批处理文件。

因为是在docker中运行的redis,本机存储中没有redis环境,故输入如下:

docker exec -it 9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849 redis-cli

其中9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849为reids容器container的id,也可以直接用redis容器的名称直接替换。

如果出现如下错误:

C:\WINDOWS\system32>docker exec -it 9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849 redis-cli
Error response from daemon: container 9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849 is not running

What's next?
  Try Docker Debug for seamless, persistent debugging tools in any container or image → docker debug 9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849
  Learn more at https://docs.docker.com/go/debug-cli/

C:\WINDOWS\system32>

则说明docker中的redis容器没有运行起来,在docker中运行或者输入命令:

docker run --name redis -d -p 6379:6379 redis

这里容器的名字name就是redis。不可用id代替。

重新运行docker exec,发现出现了127.0.0.1:6379>。表明成功。

用简单的redis案例来熟悉一下:

C:\WINDOWS\system32>docker exec -it 9b49cbee1854b320cff3cdcde0dd095314ca13f433a389d5badbf8016ca98849 redis-cli
127.0.0.1:6379> SET firstkey "hello world"
OK
127.0.0.1:6379> get firstkey
"hello world"
127.0.0.1:6379>

打开redis桌面可视化工具redis insight,可以看见我的键值对已经写入本地6379端口:

如果想停止Redis容器,可以在新cmd中使用以下命令:

docker stop redis

删除容器则是:

docker rm redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值