The command
instruction in a Docker Compose file maps to the CMD directive in a Dockerfile, which is the one and only command that the container will execute upon launching (the command can always be overridden when you launch the container, which is what docker-compose is doing under the hood when you set that option).
For a Redis container, whose purpose is to bring up a Redis server, the command should always be the command that actually launches the server. When you override it to instead run redis-cli commands, the server does not come up, so the client can't connect and you see the errors you're getting. The --help
option for redis-cli doesn't open a connection, which is why it doesn't error (to answer your question #1).
For question #2, see above as to why the process is exiting after the command
runs. To actually solve your problem, there are a range of options, but I would suggest a second container which just runs a script that a) waits until Redis is reachable and b) initializes the data structures once it is. A utility like wait-for-it can make step A easier, though you might be able to get away with just Compose's default depends_on
functionality (it doesn't wait for the actual service to be ready, just the container, but Redis comes up very quickly):
version: "3.8"
services:
redis:
image: redis
ports:
- 6379:6379
redis_init:
image: redis # we need an image with redis-cli available, so might as well re-use it
command: "redis-cli -h redis SET counter "4""
depends_on:
- redis
From testing locally, this works, though I can't say how reliable it might be on every system.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…