I have tried out a docker image using the docker command line without specifying names for volumes. Now I found that I want to continue using this container/image but by defining the container in docker compose.
What is best practice for keeping the data from the anonymous/unnamed volumes and use them in the new container created by docker compose?
Can I somehow convert the unnamed into named volumes?
or should I create a new container with docker compose with named volumes and copy the data?
This method has the benefit of using the docker API. It uses a lightweight Linux image called alpine and its default shell, ash to run a file copy, cp -av from the old volume to the newly named volume.
You cannot currently rename existing volumes. (This is true whether they were previously named or were unnamed and had their names auto-generated.)
You can see this issue for more information on implementation of this feature, as well as add your "+1"/"Thumbs up" to let the developers know that you want it.
Without that, as far as I know, the only good way to do it is to create the new named volume and copy the data.
At the time of this writing the only option is to copy the data. The details may vary depending on your particular setup. The general list of steps to be performed is as follows:
Stop and remove the source container to let the new volume be created at step 3.
docker stop NAME
docker rm NAME
# or
docker-compose stop SERVICE
docker-compose rm SERVICE
In some cases you might be able to postpone removing the source container, e.g. when switching from docker-compose anonymous volume to docker-compose named volume. In this case up would create the new volume. And you can proceed with copying the data. But even in this case stopping the container is advised to avoid data being changed as you copy them. And you've got to remove the source container for docker-compose to switch to the new volume (pay attention to the warnings).
In some cases it's not really needed, e.g. when switching from standalone containers to docker-compose.
Change docker-compose.yml if applicable.
Start the new container for the new volume to be created.
Standalone containers:
docker run ...
# or
docker-compose up -d
Stop the new container to avoid data being changed.
docker stop ...
# or
docker-compose stop
In case of migrating to e.g. a standalone container with a named volume you can just create the volume in place of starting/stopping the container.
Copy the data.
docker run --rm -v "SRC_VOLUME:/from" -v "DST_VOLUME:/to" \
bash -c '
shopt -s dotglob
# rm -r /to/* # e.g. pg might have populated the new volume
cp -r /from/* /to
'
Where SRC_VOLUME, DST_VOLUME - volume name, id, or absolute path to a directory on the host.