4

I am using docker-compose to create a redis container. However, I need it to start with some default key values. Is this possible?

1 Answer 1

8

You need to modify your DockerCompose file, You can also add from some file which contains key value but here is the simplest example that adds and get key in DockerCompose file.

version: '2'

services:
  redis:
    image: 'bitnami/redis:latest'
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    ports:
      - '6379:6379'
    command:
      - /bin/sh
      - -c
      - |
          nohup redis-server &
          sleep 5
          echo "adding some default key value"
          redis-cli SET docker awesome
          echo "Get docker key value"
          redis-cli GET docker
          # this will keep container running
          tail -f /dev/null
4
  • Note that, in the possibility that the Redis server crashes, since it's not the main container process, Docker won't notice this. You have "a container that does nothing, that happens to also be running Redis" as opposed to "a container that runs Redis". The PostgreSQL entrypoint script, for example, goes to some lengths to do this correctly.
    – David Maze
    Jul 8, 2019 at 1:01
  • yes, this is not main the process but a way to add key value.
    – Adiii
    Jul 8, 2019 at 1:05
  • 1
    Worked a treat! Thanks Jul 9, 2019 at 0:29
  • But it starts the redis container in protected mode. How to disable protected mode here in this yaml file itself? Dec 7, 2020 at 14:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.