Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
Decoupling container from storage. Share volume (storage/data) among different containers. Attach volume to a container. On deleting container, volume doesn't gets deleted.
Note - Docker has two options, for containers to store files in the host system. Volumes and bind mounts.
docker volume create myvol1
docker volume list
docker volume inspect myvol1
docker volume prune
- Volumes are managed by docker and are isolated from the core functionality of the host machine.
- A given volume can be mounted into multiple containers simultaneously.
docker run --name myjenkins1 -v myvol1:/var/jenkins_home -p 9090:8080 jenkins
Run the same command as above. Just change the name to "myjenkins2" and port.
docker run --name myjenkins2 -v myvol1:/var/jenkins_home -p 9091:8080 jenkins
Note - you will be able to see the jobs (which were created in the first container), in the second container.
Bind Mount - A file or dir on host machine, is mounted into a container. (basically you can use physical location instead of volumes)
docker run --name myjenkins3 -v /Users/home/jenkins_temp:/var/jenkins_home/ -p 9092:8080 jenkins
Note - "/Users/home/jenkins_temp", will be automatically created, if you don't already have it.