Calling redis-cli in docker-compose setup

后端 未结 3 1312
一整个雨季
一整个雨季 2021-01-31 09:31

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

myredis:
  image: redis

How can run

相关标签:
3条回答
  • 2021-01-31 10:02

    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.

    0 讨论(0)
  • 2021-01-31 10:03

    You can also use this command:

    docker-compose run myredis redis-cli -h myredis
    
    0 讨论(0)
  • 2021-01-31 10:04

    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>
    
    0 讨论(0)
提交回复
热议问题