Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading initial data into a Redis Docker container

Tags:

docker

redis

I'm trying to create a Redis Docker image, based on the redis:3. However, I need to load a small amount of test data into the image (it is used for testing, both on developer machines and on the CI server).

My initial attempt looks like:

FROM redis:3.0.3
MAINTAINER Howard M. Lewis Ship

RUN redis-cli sadd production-stores 5555

But this fails with the error:

Step 2 : RUN redis-cli sadd production-stores 5555
 ---> Running in 60fb98c133c0
Could not connect to Redis at 127.0.0.1:6379: Connection refused
The command '/bin/sh -c redis-cli sadd production-stores 5555' returned a non-zero code: 1

I'm wondering if there's a trick that allows me, in a Docker file, to connect to the server started via the CMD/ENTRYPOINT of the Dockerfile. As I'm guessing right now, the RUN occurs before the command is started.

Alternately, is there a Redis command or trick that would allow me to load some data into its database.

I suspect one approach would be to mount the Redis' data directory in a volume exposed to the Docker host; this is not ideal as

  1. I would need an extra non-Dockerfile command to load the data
  2. The loaded data would not be shared in the image, which is to be used by others on my team
  3. I'm on OS X and am using docker-machine, so the volumes get trickier
like image 407
Howard M. Lewis Ship Avatar asked Oct 08 '15 00:10

Howard M. Lewis Ship


1 Answers

The error tells Redis service is not started before you run redis-cli.

Add a new line to start it first. Let me know if this can fix your issue or not.

FROM redis:3.0.3
MAINTAINER Howard M. Lewis Ship

RUN /usr/sbin/redis-server /etc/redis.conf

RUN redis-cli sadd production-stores 5555

Updates:

Review the official redis image, it has given the way to use redis-cli

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

So I test the image and make it run as below:

# start redis container
$ docker run --name some-redis -d redis

# link redis container and run redis-cli command    
$ docker run -it --link some-redis:redis --rm redis redis-cli -h redis
redis:6379> PING
PONG
redis:6379> exit

# Add the specified members to the set stored at key    
$ docker run -it --link some-redis:redis --rm redis redis-cli -h redis sadd production-stores 5555
(integer) 1
$
like image 170
BMW Avatar answered Oct 11 '22 17:10

BMW