1. Volumes are the preferred mechanism for persisting data generated by and used by docker containers.
    In docker whenever, you create a container they has to be someplace where the data for the container will be stored. Now in case you do not provide any explicit location, for that data to be stored it gets stored within the container, and when you delete the container or remove the container the data is also lost. However, when you work on enterprise projects, we won't that data is not lost, We can actually remove a container but still persist the data. in this case, It required to create more containers with the old data or to share the data between container it should be possible.
  2. Details about docker volume
  3. $ docker volume
    $docker volume --help


    1. Create a volume
      docker volume create name_of_volume
      $ docker volume create myvol

    2. c_vol

    3. To check the list of volume
      $ docker volume ls

    4. 2 2

    5. To get the details about the volume
      $ docker volume inspect myvol


      Mountpoint: is the location of your volume and it can not be edited by the functions locally, that is one of the advantages and this volume is safe now.

    6. To remove the volume
      docker volume rm name_of_volume
      $ docker volume rm myvol
    7. 3

    8. To remove all unused volume you can use:-
      $ docker volume prune


      It will remove all local volumes which are not used by any one of the containers.

    9. 4 2

    How it works

    1. Pull the jenkins image
      $ pull the jenkins image
    2. To start container
      --name =name_of_container
      -p:flag to expose the ports=8080 on the local system and 8080 on the jenkins server.
      For the API(Application Programming Interface) we using port=50000, with the name of image.
      Run this command, it will surely start the Jenkins container and we will be able to have it running on localhost:8080
      Attach a volume= -v:flag name_of_volume that we just created. This will correspond to the jenkins home directory

      docker run --name name_of_container -v name_of_volume:it should get data from where jenkins home

      $ docker run --name myjenkins2 -v myvol2:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins

      6 1

      We want all the data of jenkins home to be available in our volume

    3. Need to copy this initial admin password
    4. 6 2

    5. Open localhost:8080 on the browser
      Provide the admin password that we copied
    6. 6 3

    7. We getting a option to select plugins to install

      6 4

      -> Click 'none' from the menu bar to all, to save time and then install it.

      -> Skip the process by click on continue as admin

      -> click on 'start using jenkins'

    8. 6 5

    9. Main idea here is to show how can we share the volumes.
    10. In jenkins create a new job
    11. 6 6

    12. Enter an item name
      eg: testjob1
      Click on freestyle project
    13. 6 7

    14. Add build steps->click on 'execute shell' write command (like: ls) and apply to save it.
    15. 6 8

    16. Go to the dash board click on jenkins :- you can see the testjob1 is created
    17. 6 9

    18. Now start another jenkins container:- open another terminal
      Change the name_of_container and use same volume that we used for eariler jenkins container
      Use another port because 8080 is already in use.
      $ docker run --name myjenkins3 -v myvol2:/var/jenkins_home -p 9090:8080 -p 60000:50000 jenkins

      6 10

      It will start another instance of Jenkins or a different docker container with Jenkins

    19. Now we need to check the volume is shared between these two Jenkins or not
      Jenkins is fully up an running
      Open localhost:9090

      6 11

      It has not given us the initial plug-in screen it is just giving us login screen


      same password that we had for last jenkins

    20. If this allows us to log in means the data is being shared with these two Jenkins containers.It allowed us to login and you can see this has the testjob1 is here. So data is being shared between both Jenkins container which running on 8080 port and this container which is running on port 9090. So we are able to share the volume between these two containers. The volume will remain intact even if we close any of these containers or even delete or remove the containers. We will still have our volume that we can use for any other container as well.
    21. 6 12

    Bind Mount

  4. A file or directory on the host machine is mounted into a container.
    1. Bind mounts means you can actually use physical location instead of volume.
      $ docker run --name myjenkins_bm -v /home/pardise/work/jenkins:/var/jenkins_home -p 9191:8080 -p 40000:50000 jenkins
      change port no = 9191 and 40000
      name = myjenkins_bm(container_name)
      instead of using volume we going to use physical location of our system.
      Create a folder jenkins, all the data of jenkins_home will get stored here.
    2. 6 13

    3. Some new file and folder copied in jenkins folder.
      Open localhost:9191


      Open another terminal and check running containers

      $ docker ps

    4. 6 14

    5. Stop the running containers
    6. 6 15

    7. Remove the container
    8. 6 16

    9. Remove all containers
    10. 6 17

    11. Now check the volume myvol2 is still there
    12. 6 18

    13. We can use same volume with another container to share the data with any other container
      $ docker volume inspect myvol2
    14. 6 19

      Our physical jenkins_home is created in our system.