Definition: Docker volumes are used to persist data generated by and used by Docker containers. Volumes are stored on the host filesystem and are independent of the container’s lifecycle.
Usage: Volumes allow data to be shared between containers and the host system, making data persistent across container restarts and updates.
Lifecycle: Volumes are created using the docker volume create command or automatically when specified in a Docker Compose file. They can be managed (listed, removed) using Docker CLI commands.
Volumes have 2 different uses:
Code Synchronisation (Development):
During development, it’s common to mount your project directory to the container to reflect code changes immediately without rebuilding the container.
This allows you to edit your code on your host machine and see changes instantly within the container, which is useful for rapid development and debugging.
Persistence:
If your backend service writes data to the filesystem that needs to persist across container restarts, using volumes ensures this data is not lost when the container is stopped or recreated.