5

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?
  • or any other option?
3

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.

0

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:

  1. 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.

  2. Change docker-compose.yml if applicable.

  3. Start the new container for the new volume to be created.

    Standalone containers:

    docker run ...
    # or
    docker-compose up -d
    
  4. 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.

  5. 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.

    To list volumes attached to a container use:

    docker inspect CONTAINER --format '{{json .Mounts}}' | jq
    

    Where CONTAINER - container name or id.

  6. Start the new container.

    docker run ...
    # or
    docker-compose up -d
    
  7. Rejoice :)

In case you want to experiment with different setups, check out the following gist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.