Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Manage Data within Docker Volumes

    Mark ShustMark Shust

    Learn about the concepts of managing data within Docker volumes, including all of the commands to create and manage volumes. We'll demonstrate how to mount a Docker volume to one or many containers by creating and mounting a volume to an Nginx proxy, and then verify the output with curl. You'll also see how changes to files within volumes of one container propagate to other containers with the same volume mounted, and learn various commands to inspect Docker volumes.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:01 Container-based filesystems in Docker are by default ephemeral, meaning that they are not persistent discs and you can expect data within them to go away at any time. It's not recommended to store data inside a container filesystem if you ever plan to change or modify the files within that container.

    00:17 Docker has the concept of volumes, which allow you to mount persistent discs to one or many containers. Let's create a new named volume within Docker with the Docker volume create command. We'll provide the name Web Data so we can easily reference this volume later from any container.

    00:34 Now let's run a new NGINX container. Start by typing Docker run, followed by the dash D flag, to run this container as a background daemon. We'll also give it a name so we can reference it later. Let's call it Web1.

    00:48 Next is the dash D flag, which allows you to specify a volume from which to mount a disc from. Let's specify Web Data, the name of the volume we created before, followed by a colon and a path of where to mount this volume.

    01:00 Since NGINX serves files from the user share NGINX html directory, we'll specify that directory here. We'll then expose NGINX to our post at port 8000 and map that to the default NGINX port 80. Finally, we'll use NGINX as the name of the image from which we'll start this container from.

    01:19 Let's check to see the contents of NGINX with Curl by typing curl localhost:8000. We can see this is the standard welcome to NGINX output. Let's change the default index.html page that NGINX is serving. We can do that without directly entering the container with the Docker exec command. Type Docker exec, followed by the name of our running NGINX container, Web1.

    01:47 Let's pass a command to BASH to indecipherable string and overwrite the default NGINX index.html file. Let's again check to see the contents of NGINX with Curl. Let's now stop and remove this container. We can do that with Docker stop Web1 and Docker rm Web1.

    02:10 Let's verify that no containers currently exist by running Docker PSA-A. Let's create a new Docker container by typing the same line as before. We want to test to make sure our data is persisting after container halts and terminations. If we check the output with Curl again, we can see that our data is persisting.

    02:32 Let's see if we can mount the same volume to multiple Docker containers by starting another container with the name Web2 and listening on port 8001. Then let's check the output with Curl.

    02:47 Let's change the index.html output to make sure our data mount is propagating changes between both of our web servers. We should be able to make either Web1 or Web2 because the data is mounted to both containers. Then let's check the Curl output of both containers.

    03:05 There are some other commands specific to Docker volumes that we can check out. We can use Docker inspect with a dash F flag to specify a format to filter results, followed by the name of the container we want to inspect.

    03:18 We can see the names of the volumes mounted to a specific container, along with the locations of where the volumes are being mounted. We can also drill down to find more information about a specific volume by using Docker volume inspect, followed by the name of the volume. Docker volume ls lists all volumes available.

    03:40 To remove a volume, all containers must be stopped and removed. You can do this by Docker stop Docker rm commands. Now we can remove the volume by running Docker volume rm followed by the name of the volume. We can then verify the volume was removed by running Docker volume ls.