Search code examples
dockerdocker-composedocker-volume

Docker mount non-empty container folder to host


I am running some containers, defined by a docker-compose file and the containers are already used in production.

I found out that one of the containers (odoo) is using a data directory where it places some process-related files which are important and should be saved.

Unfortunately, I forgot to use a volume for the data to make it persistent and want to do it now.

I tried to edit the docker-compose.yml file and define a volume in the odoo service like:

volumes:
  - odoo_web_data:/var/lib/odoo

and run

docker-compose up -d

to recreate the container.

This would work if the container were initially created, however, all I am getting now is a warning:

WARNING: Service "odoo" is using volume "/var/lib/odoo" from the previous container. Host mapping "user_odoo_customer_web_data" has no effect. Remove the existing containers (with docker-compose rm odoo-customer) to use the host volume mapping.

I guess this means that it cannot be mounted, due to the folder being non-empty and linked to the previous instance of the container.

How can I persistently mount a folder from a running container on the host?


Solution

  • What you could try is to first backup all the files from /var/lib/odoo over to your host computer:

    i.e.

    docker run --rm -v c:\migrate:/backup alpine tar -cjf /backup/my_data.tar.bz2 -C /var/lib/odoo ./
    

    then import that tar.bz2 into the odoo_web_data volume you created:

    docker run --rm -v odoo_web_data:/volume -v c:\migrate:/backup alpine tar -C /volume/ -xjf /backup/my_data.tar.bz2
    

    Add this to the bottom of your docker-compose.yml:

    volumes:
      odoo_web_data:
        external: true
    

    Then go ahead and start the container again using the docker-compose up -d with the volume mount you have in your code already.

    At that point you should have all the previous /var/lib/odoo data in your new named volume, and the container will be using (and storing) it from that volume.