docker pull redis
docker run --name my-redis -d -p 6379:6379 redis
# 创建本地配置文件目录
mkdir -p ~/docker/redis/conf
touch ~/docker/redis/conf/redis.conf
# 运行容器并挂载配置文件
docker run --name my-redis \
-v ~/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d -p 6379:6379 redis redis-server /usr/local/etc/redis/redis.conf
# 创建数据目录
mkdir -p ~/docker/redis/data
# 运行容器并挂载数据卷
docker run --name my-redis \
-v ~/docker/redis/data:/data \
-d -p 6379:6379 redis redis-server --appendonly yes
docker run --name my-redis \
-d -p 6379:6379 redis redis-server --requirepass "yourpassword"
创建 docker-compose.yml
文件:
version: '3'
services:
redis:
image: redis
container_name: my-redis
ports:
- "6379:6379"
volumes:
- ~/docker/redis/data:/data
- ~/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
restart: unless-stopped
然后运行:
docker-compose up -d
docker exec -it my-redis redis-cli
docker exec -it my-redis redis-cli -a yourpassword
docker ps
docker logs my-redis
docker stop my-redis
docker start my-redis
docker rm my-redis
希望这个指南能帮助你快速搭建 Redis 容器环境!