Skip to content

Files

Latest commit

224784e · Nov 11, 2024

History

History

16-Docker-Volumes-with-Containers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 11, 2024

title: "Learn to Use Docker Volumes with Docker Containers" description: "Understand how to use Docker volumes with containers using the '--mount' and '-v' flags."

Learn to Use Docker Volumes with Docker Containers


Introduction

In this guide, you will learn how to:

  • Create Docker volumes when starting containers.
  • Use the --mount and -v flags to mount volumes into containers.

Docker volumes are essential for persisting data generated by and used by Docker containers. They are the preferred method for managing data in Docker containers.


Step 1: Start a Container with a Volume Using --mount Flag

The --mount flag is the newer and more verbose way to specify volume mounts in Docker. It provides a clearer syntax and more options than the -v flag.

Single-Line Format

# Start a container with a named volume using the --mount flag
docker run --name volume-demo1 -p 8090:80 --mount type=volume,source=myvol101,target=/myapps -d nginx:alpine-slim

Readable Format

docker run \
    --name volume-demo1 \
    -p 8090:80 \
    --mount type=volume,source=myvol101,target=/myapps \
    -d \
    nginx:alpine-slim

Explanation:

  • --mount type=volume,source=myvol101,target=/myapps:
    • type=volume: Specifies that we're mounting a Docker volume.
    • source=myvol101: The name of the Docker volume to use. If it doesn't exist, Docker will create it.
    • target=/myapps: The directory inside the container where the volume will be mounted.

Verify the Container and Volume Mount

# List Docker Containers
docker ps

# Format the output for clarity
docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Status}}\t{{.ID}}\t{{.Ports}}"

# Connect to the container
docker exec -it volume-demo1 /bin/sh

# Inside the container, check the mounted volumes
df -h

# Navigate to the mounted directory
cd /myapps

# List contents (should be empty initially)
ls

# Exit the container shell
exit

Inspect the Docker Container

# Inspect the container's mounts
docker inspect volume-demo1

# Extract just the Mounts information in JSON format
docker inspect --format='{{json .Mounts}}' volume-demo1

# For better readability, pipe the output to 'jq' (JSON processor)
docker inspect --format='{{json .Mounts}}' volume-demo1 | jq

Expected Output:

  • The Mounts section should show that myvol101 is mounted to /myapps inside the container.
  • Inside the container, the /myapps directory corresponds to the Docker volume myvol101.

Step 2: Start a Container with a Volume Using -v Flag

The -v or --volume flag is the older syntax for mounting volumes. It is still widely used and works well for simple volume mounts.

Single-Line Format

# Start a container with a named volume using the -v flag
docker run --name volume-demo2 -p 8091:80 -v myvol102:/myapps -d nginx:alpine-slim

Readable Format

docker run \
    --name volume-demo2 \
    -p 8091:80 \
    -v myvol102:/myapps \
    -d \
    nginx:alpine-slim

Explanation:

  • -v myvol102:/myapps:
    • myvol102: The name of the Docker volume to use. If it doesn't exist, Docker will create it.
    • /myapps: The directory inside the container where the volume will be mounted.

Verify the Container and Volume Mount

# List Docker Containers
docker ps

# Format the output for clarity
docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Status}}\t{{.ID}}\t{{.Ports}}"

# Connect to the container
docker exec -it volume-demo2 /bin/sh

# Inside the container, check the mounted volumes
df -h

# Navigate to the mounted directory
cd /myapps

# List contents (should be empty initially)
ls

# Exit the container shell
exit

Expected Output:

  • The Mounts section (viewable via docker inspect volume-demo2) should show that myvol102 is mounted to /myapps inside the container.
  • Inside the container, the /myapps directory corresponds to the Docker volume myvol102.

Clean-Up

After completing the steps, it's good practice to clean up the Docker resources to free up system resources.

# Stop and remove all containers
docker rm -f $(docker ps -aq)

# Remove all Docker images (use with caution)
docker rmi $(docker images -q)

# Remove the volumes if desired
docker volume rm myvol101 myvol102

# Verify that volumes are removed
docker volume ls

Note:

  • Be careful with the docker rmi $(docker images -q) command as it will remove all Docker images from your system.
  • Ensure you do not need the images before running this command.
  • The same applies to removing volumes. Only remove volumes that you no longer need.

Conclusion

You have successfully:

  • Learned how to create Docker volumes when starting containers.
  • Used the --mount flag to mount volumes into containers with explicit options.
  • Used the -v flag as a shorthand to mount volumes into containers.
  • Verified the volume mounts inside the containers.
  • Cleaned up Docker containers, images, and volumes.

Additional Notes

  • Difference Between --mount and -v:

    • The --mount flag is more verbose but provides a clear syntax and supports all volume options.
    • The -v flag is shorter but can be ambiguous and doesn't support all volume options.
  • When to Use Which Flag:

    • Use --mount when you need more control over the volume settings and want clear syntax.
    • Use -v for simple, straightforward volume mounts.
  • Volume Persistence:

    • Data stored in Docker volumes persists even after the container is removed.
    • This is useful for maintaining data between container restarts or upgrades.
  • Common Use Cases for Volumes:

    • Storing database data.
    • Sharing configuration files between containers.
    • Persisting application data generated by the container.

Additional Resources


Happy Dockerizing!