Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container restart without data loss

I am using Rancher as orchestration tool. I have a single node Cassandra installed in a container and would like to restart it, because of I have made some changes in configuration file(cassandra.yaml) recently. There is already data in database, so would like to prevent data loss. I use usman/docker-rancher-cassandra:3.1 image. Which command should use to do this?

like image 847
plaidshirt Avatar asked Jan 07 '23 05:01

plaidshirt


1 Answers

Restarting a container won't affect your data in the container. If you are trying to recreate the container with the new image, you may lose the data.

If you are using volumes, your data will be persisted in the host. It depends on how did you started your container. You can check the volume info from docker inspect <container_name>.

By going through the dockerfile which you are using, the cassandra data directory is a volume.

VOLUME /var/lib/cassandra 

If you explicitly mounted your container volume using

docker run -d -v <HOST_PATH>:/var/lib/cassandra usman/docker-rancher-cassandra:3.1 

Then you can upgrade the container with new version. Otherwise, you need to copy the data from directory before recreating container.

SOLUTION 1:

Start another container from the volumes of an existing container.

docker stop cassandra
docker run -it --volumes-from cassandra \ 
               --name="cassandra-new" \
               usman/docker-rancher-cassandra:3.1 

If everything works fine, remove the previous container and rename the container

docker rm cassandra
docker rename cassandra-new cassandra

SOLUTION 2:

docker inspect to get the volume path.

docker inspect <container_name>

You can copy the data from /var/lib/docker/volumes/531419fb27d1dff9c5371769edc08d6160f71061c9fe3af05309a3a0c51bb84d/_data to some other directory before recreating container. You need mount that directory while starting the new container using

docker run -v <HOST_PATH>:/var/lib/cassandra

like image 95
Gangaraju Avatar answered Jan 14 '23 15:01

Gangaraju