简体   繁体   English

Redis docker-使用数据创建一个容器

[英]redis docker - create a container with data

I want to run a redis container with initial data in it. 我想运行一个带有初始数据的redis容器。 In the documentation of the image, I can use a volume to mount to /data . 在映像的文档中,我可以使用卷来挂载到/data My question is: will redis be able to read the data from it and load it? 我的问题是:redis能够从中读取并加载数据吗? And if so, what should be in the directory that I mount? 如果是这样,我挂载的目录应该是什么? My (very naive) attempt was to put a file with name "someFile" and hopefully redis will know to save it with key "someFile" and the content of the file as the data. 我(非常幼稚)尝试放置一个名称为“ someFile”的文件,希望redis知道将其保存为键“ someFile”,并将文件内容作为数据保存。 Obviously it didn't work. 显然,它没有用。

Any help would be appreciated. 任何帮助,将不胜感激。

You can run the redis container one first time setting an empty directory as the data volume and populate the redis data using the redis CLI. 您可以第一次运行redis容器,将一个空目录设置为数据卷,然后使用redis CLI填充redis数据。 Once you stop the container, the data directory will contain a working redis data set. 一旦停止容器,数据目录将包含一个有效的redis数据集。

If you run another container instance specifying the same directory, redis will use that data. 如果您运行另一个指定相同目录的容器实例,则redis将使用该数据。

Please be aware that you will need to configure redis in order to persist data to the filesystem accordingly (check https://redis.io/topics/persistence ) 请注意,您将需要配置redis以便将数据持久保存到文件系统中(请检查https://redis.io/topics/persistence

Depending on how large your initial data set is and if your initial data doesn't change much, it may be easier to have your clean docker container load it on startup from a *.redis file using redis-cli ( link ). 根据初始数据集的大小以及初始数据没有太大变化,在启动时使用redis-clilink )从*.redis文件*.redis容器加载起来可能会更容易。

Create your seed commands file ( my-data.redis ): 创建您的种子命令文件( my-data.redis ):

SET key1 val1
SET key2 val2
...
...

Create a redis startup shell script ( my-redis.sh ): 创建一个redis启动shell脚本( my-redis.sh ):

# start server in background and wait for 1 sec
redis-server --daemonize yes && sleep 1 
# slurp all data from file to redis in memory db (note the dir)
redis-cli < /my-dir/my-data.redis 
# persist data to disk
redis-cli save 
# stop background server
redis-cli shutdown 
# start the server normally
redis-server 

Create a custom redis docker image with your shell script as CMD, something like this (a better solution would be to hack the entrypoint but who's got time for that?): 使用shell脚本CMD创建一个自定义的redis docker镜像,类似这样(更好的解决方案是破解入口点,但是谁有时间这样做?):

FROM redis:latest
COPY my-data.redis /my-dir/
COPY start-redis.sh /my-dir/
CMD ["sh", "/my-dir/my-redis.sh"]

Done. 做完了 No external volumes or builder containers needed. 无需外部卷或构建器容器。 Build and run: 构建并运行:

docker build -t my-redis:latest .
docker run -p 6379:6379 my-redis:latest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM