Rotem
Rotem

Reputation: 442

Change volume configuration in docker-compose without losing the data

My docker-compose has a data container which isn't mapped to a local directory in the host machine, and I want to change it from:

volumes:
  - /var/www/html

to

volumes:
  - /html:/var/www/html

But when I will restart the container, it will remove the current data container and replace it with a new one.

I know that the container is actually still there, but is there an easy way to do it without the creation of a new data container.

My docker-compose version is 1.7.1 (under boot2docker).

Thanks.

Upvotes: 2

Views: 4129

Answers (2)

mkasberg
mkasberg

Reputation: 17362

One safe way to do this is to create a backup of the data from inside the Docker image. Then restore that backup to the directory on your host machine. The Docker Volumes Tutorial mentions a process like this near the bottom.

Here's how you'd do it:

First, mount a directory from your host machine into the container if you don't already have one mounted in. Maybe a volume like ./:/backup. Next, run a backup command like this:

docker-compose run service-name tar czvf /backup/html_data.tar.gz /var/www/html

Now you have html_data.tar.gz in your current directory. Extract it wherever you want and be on your way!

(I'm assuming, based on the way you indicated your volumes, that you're using docker-compose. The process is similar for vanilla Docker.)


Alternate approach, with --volumes-from

Get the name (or hash) of the container with the data you want to copy. You can do this with docker ps. For this example, let's call it container1. Now run this command to back up its data:

docker run --rm --volumes-from container1 -v $(pwd):/backup ubuntu:latest tar czvf /backup/html_data.tar.gz /var/www/html

Note that the image you use (ubuntu:latest) is not important as long as it can tar things.

Upvotes: 0

Webert Lima
Webert Lima

Reputation: 14035

Try at your own risk:

  • create your host directory /htmlas you wish
  • docker inspect {container_name} | grep Source and grab your volume path on the host system. It'll be something like /var/lib/docker/volumes/abdb15a2eff[...]/_data
  • copy the content of that directory to your host directory
  • recreate the container as you wish.

Upvotes: 2

Related Questions