32

I run the official Redis image https://hub.docker.com/_/redis/ in a docker-compose setup.

myredis:
  image: redis

How can run redis-cli with docker-compose on that image?
I tried the following, but it didn't connect:

docker-compose run myredis redis-cli
> Could not connect to Redis at 127.0.0.1:6379: Connection refuse

The docs of the image says that I should run:

docker run -it --rm \
--link some-redis:redis \
redis \
sh -c 'exec redis-cli -h "$REDIS_PORT_6379_TCP_ADDR" -p "$REDIS_PORT_6379_TCP_PORT"'

How does this translate to docker-compose run?

3 Answers 3

69

That would override the default CMD [ "redis-server" ]: you are trying to run redis-cli on a container where the redis-server was never executed.

As mentioned here, you can also test with:

docker exec -it myredis redis-cli

From docker-compose, as mentioned in this docker/compose issue 2123:

rcli:
  image: redis:latest
  links:
    - redis
  command: >
     sh -c 'redis-cli -h redis '

This should also works:

rcli:
  image: redis:latest
  links:
    - redis
  command: redis-cli -h redis

As the OP ivoba confirms (in the comments), the last form works.
Then:

docker-compose run rcli

ivoba also adds:

docker-compose run redis redis-cli -h redis works also when the containers are running.
This way its not necessary to declare a separate rcli container.

Sign up to request clarification or add additional context in comments.

6 Comments

Great! works. I took the last approach and run it with: docker-compose run rcli
@ivoba Excellent! I have included your comment in the answer for more visibility.
'docker-compose run redis redis-cli -h redis' works also when the containers are running. This way its not necessary to declare a seperate rcli container.
@ivoba OK. I have again included your comment in the answer for more visibility.
docker-compose run redis redis-cli -h redis gives me a Could not connect to Redis at redis:6379: Try again :/ It also happens when the container is running. Edit: the solution with rcli works however. I guess this is related to networking somehow.
|
9

You can also use this command:

docker-compose run myredis redis-cli -h myredis

1 Comment

this is already said in above answer just with different names
1

I followed as @VonC suggest, but in my case I run redis on predefined network so it did not worked.

So in the case redis container run in specific network, network field should be specified in docker-compose.yaml file

rcli:
  image: redis:latest
  links:
    - redis
  command: redis-cli -h redis
  networks: 
    - <network redis run on>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.