Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save a docker redis container

Tags:

I'm having trouble creating an image of a docker redis container with the data in the redis database. At the moment I'm doing this:

docker pull redis docker run --name my-redis -p 6379:6379 -d redis redis-cli 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> save OK 127.0.0.1:6379> exit docker stop my-redis docker commit my-redis redis_with_data docker run --name my-redis2 -p 6379:6379 -d redis_with_data redis-cli 127.0.0.1:6379> keys * (empty list or set) 

I'm obviously not understanding something pretty basic here. Doesn't docker commit create a new image from an existing container?


okay, i've been doing some digging. The default redis image on hub.docker uses a data-volume which is then mounted at /data in a container. In order to share this volume between containers, you have to start a new container with the following argument:

docker run -d --volumes-from <name-of-container-you-want-the-data-from> \   --name <new-container-name> -p 6379:6379 redis 

Note that the order of the arguments is important, otherwise docker run will fail silently.

docker volume ls 

will tell you which data volumes have already been created by docker on your computer. I haven't yet found a way to give these volumes a trivial name, rather than a long random string.

I also haven't yet found a way to mount a data-volume, but rather just use the --volumes-from command.


Okay. I now have it working, but it's cludgey.

With

docker volume ls docker volume inspect <id of docker volume> 

you can find the path of the docker volume on the local file-system. You can then mount this in a new container as follows:

docker run -d -v /var/lib/docker/volumes/<some incredibly long string>/_data:/data \    --name my-redis2 -p 6379:6379 redis 

This is obviously not the way you're meant to do this. I'll carry on digging.


I put all that i've discovered upto now in a blog post: my blog post on medium.com

Maybe that will be useful for somebody

like image 632
ehrt1974 Avatar asked Apr 11 '17 08:04

ehrt1974


People also ask

Where does Redis store data docker?

If persistence is enabled, data is stored in the VOLUME /data , which can be used with --volumes-from some-volume-container or -v /docker/host/dir:/data (see docs. docker volumes).

How do I access Redis docker?

To connect to a Redis instance from another Docker container, add --link [Redis container name or ID]:redis to that container's docker run command. To connect to a Redis instance from another Docker container with a command-line interface, link the container and specify the host and port with -h redis -p 6379.

How do I make Redis persistent?

Within Redis, there are two different ways of persisting data to disk. One is a method called snapshotting that takes the data as it exists at one moment in time and writes it to disk. The other method is called AOF, or append—only file, and it works by copying incoming write commands to disk as they happen.

How do I run Redis in a docker container?

To run a Redis instance in a Docker container named my-redis-container, use the command: To connect to a Redis instance from another Docker container, add --link [Redis container name or ID]:redis to that container's docker run command.

How do I set up port forwarding for a Redis container?

To use Docker's port forwarding for Redis, add the flag -p [host port]:6379 to the docker run command. For example, to set up port forwarding so that you can connect to the container using port 7001, the docker run command is: You can then switch to another server and access the my-redis-container container with the command:

How do I connect to Redis in Linux terminal?

Connect to Redis with redis-cli Start the interactive redis-cli command shell using the following command: sudo docker exec -it my-first-redis sh Note: You can also use the unique container ID (b36263951bf4) instead of the container name.

How to run a new instance of a restore Docker image?

After you have your restored image on your local machine, you can use the docker run command to run a new instance of the restored docker image. You can use the command below to do so. In the above article, we have seen how to backup and restore a docker container.


2 Answers

Data in docker is not persistent, when you restart the container your data will be gone. To prevent this you have to share a map on the host machine with your container. When you container restarts it will get the data from the map on the host.

You can read more about it in the Docker docs: https://docs.docker.com/engine/tutorials/dockervolumes/#data-volumes

From the redis container docs:

Run redis-server

docker run -d --name redis -p 6379:6379 dockerfile/redis 

Run redis-server with persistent data directory. (creates dump.rdb)

docker run -d -p 6379:6379 -v <data-dir>:/data --name redis dockerfile/redis 

Run redis-server with persistent data directory and password.

docker run -d -p 6379:6379 -v <data-dir>:/data --name redis dockerfile/redis redis-server /etc/redis/redis.conf --requirepass <password> 

Source: https://github.com/dockerfile/redis

like image 93
Frenus Avatar answered Oct 26 '22 11:10

Frenus


Using data volume and sharing RDB file manually is not ugly, actually that's what data volume is designed for, to separate data from container.

But if you really need/want to save data to image and share it that way, you can just change the redis working directory from volume /data to somewhere else:

  • Option 1 is changing --dir when start the redis container:

    docker run -d redis --dir /tmp 

    Then you can follow your steps to create new image. Note that only /tmp could be used by this method due to permission issue.

  • Option 2 is creating a new image with changed WORKDIR:

    FROM redis  RUN mkdir /opt/redis && chown redis:redis /opt/redis WORKDIR /opt/redis 

    Then docker build -t redis-new-image and use this image to do your job.

like image 27
shizhz Avatar answered Oct 26 '22 10:10

shizhz