How to run redis on by docker on local mac?

Refer link:  https://blog.csdn.net/weixin_45821811/article/details/116211724 )
 
1. docker pull redis
2. docker run with redis config and data file
- create dir
- prepare config file
 
- redis.conf文件位置: /home/redis/myredis/redis.conf
- redis的data文件位置 : /home/redis/myredis/data
# can get the file from redis website
 
3. run
docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name myredis -v /home/redis/myredis/myredis.conf:/etc/redis/redis.conf -v /home/redis/myredis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass 000415
 
--restart=always 總是開機啓動
--log是日誌方面的
-p 6379:6379 將6379端口掛載出去
--name 給這個容器取一個名字
-v 數據卷掛載 - /home/redis/myredis/myredis.conf:/etc/redis/redis.conf 這裏是將 liunx 路徑下的myredis.conf 和redis下的redis.conf 掛載在一起。 - /home/redis/myredis/data:/data 這個同上
-d redis 表示後臺啓動redis
redis-server /etc/redis/redis.conf 以配置文件啓動redis,加載容器內的conf文件,最終找到的是掛載的目錄 /etc/redis/redis.conf 也就是liunx下的/home/redis/myredis/myredis.conf
–appendonly yes 開啓redis 持久化
–requirepass 000415 設置密碼 (如果你是通過docker 容器內部連接的話,就隨意,可設可不設。但是如果想向外開放的話,一定要設置)
 
4. Test
- docker ps -a | grep myredis
- myredis docker ps
- docker logs --since 30m myredis 查看日誌
- docker exec -it myredis /bin/bash 進行容器內部連接
myredis docker exec -it myredis redis-cli
127.0.0.1:6379> get s1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth yourpwd
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "yourpwd"
127.0.0.1:6379>

Simeple way

To run Redis on Docker on your Mac, you can follow these steps:

  1. Install Docker on your Mac.
  2. Open a terminal window.
  3. Run the following command to pull the latest Redis image from Docker Hub:
Code snippet
docker pull redis
Use code with caution.   Learn more
content_copy
  1. Run the following command to create a new Docker container and start the Redis server:
Code snippet
docker run -d -p 6379:6379 redis

# --name give a name for the instance
docker run --name some-redis -d redis
 

The -d flag tells Docker to run the container in detached mode, so it will not appear in the terminal output. The -p 6379:6379 flag tells Docker to map port 6379 on the Docker host to port 6379 on the Docker container. This will allow you to connect to the Redis server from your Mac using the standard Redis client.

  1. To verify that the Redis server is running, you can run the following command:
Code snippet
docker ps
 

This will list all of the Docker containers that are running on your Mac. You should see a line that looks like this:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 35213590687c redis "docker-entrypoint.sh …" 5 seconds ago Up 4 seconds 6379/tcp redis

  1. To connect to the Redis server from your Mac, you can use the Redis client. For example, to list all of the keys in the Redis database, you can run the following command:
Code snippet
redis-cli -h localhost -p 6379
 

This will connect to the Redis server on your Mac and list all of the keys in the database.

Refer Link: 

https://www.docker.com/blog/how-to-use-the-redis-docker-official-image/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章