How do I make docker data persistent using volumes?



  • To make docker persistent, docker containers use volumes.

    This command will create a container with a volume, and touch a file in that volume.

     $ docker run --name training -v /trainingdocs alpine touch /trainingdocs/class1
    

    You can inspect the container using the inspect command: first get the container id.

    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    23f00b24aa59        alpine              "touch /trainingdo..."   5 seconds ago       Exited (0) 5 seconds ago                       training
    

    Inspect the container. List just the Mounts section.

           "Mounts": [
                {
                    "Type": "volume",
                    "Name": "af5f9fd9cb153f5e9554aaba4e7428283d42fc2307ae2df84b387739a91ee09d",
                    "Source": "/var/lib/docker/volumes/af5f9fd9cb153f5e9554aaba4e7428283d42fc2307ae2df84b387739a91ee09d/_data",
                    "Destination": "/trainingdocs",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
            ],
    

    List your file in the volume.

    sudo ls /var/lib/docker/volumes/af5f9fd9cb153f5e9554aaba4e7428283d42fc2307ae2df84b387739a91ee09d/_data
    class1
    


© Lightnetics 2024