RUN cat /test_dir/seed.txt | redis-cli -a <password> --pipe 将在您执行docker build 时执行,此时ENTRYPOINT ["redis-server", "/test_dir/redis.conf"] 仍然没有运行,因为它仅在容器启动时被调用。所以,你肯定会出错。
因此,您可以使用下一个解决方法:
/test_dir/seed.txt:
/test_dir/init.sh:
while :
do
redis-cli -h redis-svr -p 6379 quit
if [ $? -eq 0 ]; then
cat /test_dir/seed.txt | redis-cli -h redis-svr -p 6379 --pipe
break
else
echo "server not ready, wait then retry..."
sleep 3
fi
done
docker-compose.yaml:
version: '3'
services:
redis-svr:
image: redis
redis-cli:
image: redis
volumes:
- /test_dir:/test_dir
entrypoint: sh -c /test_dir/init.sh
执行:
$ docker-compose up
WARNING: Found orphan containers (20210910_redis_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Starting 20210910_redis-svr_1 ... done
Starting 20210910_redis-cli_1 ... done
Attaching to 20210910_redis-cli_1, 20210910_redis-svr_1
redis-cli_1 | Could not connect to Redis at redis-svr:6379: Connection refused
redis-cli_1 | server not ready, wait then retry...
redis-svr_1 | 1:C 11 Sep 2021 05:55:33.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-svr_1 | 1:C 11 Sep 2021 05:55:33.872 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis-svr_1 | 1:C 11 Sep 2021 05:55:33.872 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.873 * monotonic clock: POSIX clock_gettime
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.879 * Running mode=standalone, port=6379.
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.879 # Server initialized
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.890 * Loading RDB produced by version 6.2.5
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.890 * RDB age 266 seconds
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.890 * RDB memory usage when created 0.77 Mb
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.890 * DB loaded from disk: 0.010 seconds
redis-svr_1 | 1:M 11 Sep 2021 05:55:33.890 * Ready to accept connections
redis-cli_1 | OK
redis-cli_1 | All data transferred. Waiting for the last reply...
redis-cli_1 | Last reply received from server.
redis-cli_1 | errors: 0, replies: 0
20210910_redis-cli_1 exited with code 0
说明:
redis-svr 将在一个容器中启动一个服务器,redis-cli 将启动另一个容器,它将首先调用init.sh,init.sh 将尝试链接到 redis 服务器以查看服务器是否真正启动与redis-cli -h redis-svr -p 6379 quit。如果没有,它会等待一段时间并重试,如果服务器已经启动,那么它可以调用客户端命令将初始数据导入服务器。
EDIT20210912 基于 OP 的评论使用一个容器:
文件夹结构:
$ tree
.
├── docker-compose.yaml
├── Dockerfile
├── init.sh
├── my-entrypoint.sh
└── seed.txt
docker-compose.yaml:
version: '3'
services:
redis-svr:
build: ./
Dockerfile:
FROM redis:6.2.5
COPY . /tmp
RUN chmod -R 777 /tmp
ENTRYPOINT ["/tmp/my-entrypoint.sh"]
CMD ["redis-server"]
init.sh:
while :
do
redis-cli quit
if [ $? -eq 0 ]; then
echo "Server ready now, start to import data ..."
cat ./seed.txt | redis-cli --pipe
break
else
echo "Server not ready, wait then retry..."
sleep 3
fi
done
my-entrypoint.sh:
#!/bin/sh
/tmp/init.sh &
docker-entrypoint.sh $1
执行:
$ docker-compose up
Recreating test_dir_redis-svr_1 ... done
Attaching to test_dir_redis-svr_1
redis-svr_1 | Could not connect to Redis at 127.0.0.1:6379: Connection refused
redis-svr_1 | Server not ready, wait then retry...
redis-svr_1 | 7:C 12 Sep 2021 08:36:05.512 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-svr_1 | 7:C 12 Sep 2021 08:36:05.512 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=7, just started
redis-svr_1 | 7:C 12 Sep 2021 08:36:05.512 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-svr_1 | 7:M 12 Sep 2021 08:36:05.513 * monotonic clock: POSIX clock_gettime
redis-svr_1 | 7:M 12 Sep 2021 08:36:05.515 * Running mode=standalone, port=6379.
redis-svr_1 | 7:M 12 Sep 2021 08:36:05.515 # Server initialized
redis-svr_1 | 7:M 12 Sep 2021 08:36:05.515 * Ready to accept connections
redis-svr_1 | OK
redis-svr_1 | Server ready now, start to import data ...
redis-svr_1 | All data transferred. Waiting for the last reply...
redis-svr_1 | Last reply received from server.
redis-svr_1 | errors: 0, replies: 0
这定义了自定义入口点,让init.sh有机会在redis服务器运行之前被执行。