Converting a mounted volume to a Docker Image


I have a running container,with a volume mounted on it.
I want to convert the whole container along with the volume contents to a docker image.

  • Docker compose v3: The difference between volume type mount and bind
  • Exposing Docker Container Ports
  • Implementing watchdog for docker services under sysd
  • Can't connect Celery server to RabbitMQ on localhost
  • Why docker showing localhost data?
  • Docker container for SQL Server Linux keeps exiting
  • I am tried using

    docker commit container-name
    docker push repo/imagename:tag

    but it only pushed the container, no data from volume was preserved.

    Is there any way to convert data on mounted docker volume to a docker image?

  • Where is Docker storing the images? [closed]
  • Logging in to private docker registry v2 behind haproxy
  • Is there a way to disallow other docker containers using the same CPU?
  • Backslash preservation with Docker, echo, and Perl
  • Trying to create postgis inside docker file gives the error
  • Execute docker commands in jenkins (in docker container)
  • 2 Solutions collect form web for “Converting a mounted volume to a Docker Image”

    Use the following steps:

    1. Use docker cp to copy the contents of the mount point to the docker host.
    2. Create a new container using the same image.
    3. Use docker cp to copy the content into the new container at desired location.
    4. Commit the new container with content in it and push it to your repository.

    Another way to do this is to create a DockerFile, then use From directive pointing to desired base image, and use COPY directive to copy the content to desired location (from dockerhost to image) at docker build time.

    Neither export or commit will preserve your volume data. you have 2 options though.

    First: covert it to image using commit or export it then move volumes manually

    Second: copy the content of your volume to any location on your container then commit it and you have now all of the data inside your image. then after transferring. Move the volume data back to its original location for ex:-

    cp /my-volume-dir /my-backup-dir

    then after you transfer the image

    mv /my-backup-dir /my-volume-dir

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.