- 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.
- Details about docker volume $ docker volume
- Create a volume
docker volume create name_of_volume
$ docker volume create myvol
- To check the list of volume
$ docker volume ls
- 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.
- To remove the volume
docker volume rm name_of_volume
$ docker volume rm myvol
- 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.
- Pull the jenkins image
$ pull the jenkins image
- To start 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
We want all the data of jenkins home to be available in our volume
- Need to copy this initial admin password
- Open localhost:8080 on the browser
Provide the admin password that we copied
- We getting a option to select plugins to install
-> 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'
- Main idea here is to show how can we share the volumes.
- In jenkins create a new job
- Enter an item name
Click on freestyle project
- Add build steps->click on 'execute shell' write command (like: ls) and apply to save it.
- Go to the dash board click on jenkins :- you can see the testjob1 is created
- 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
It will start another instance of Jenkins or a different docker container with Jenkins
- Now we need to check the volume is shared between these two Jenkins or not
Jenkins is fully up an running
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
- 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.
- A file or directory on the host machine is mounted into a container.
- 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.
- Some new file and folder copied in jenkins folder.
Open another terminal and check running containers
$ docker ps
- Stop the running containers
- Remove the container
- Remove all containers
- Now check the volume myvol2 is still there
- We can use same volume with another container to share the data with any other container
$ docker volume inspect myvol2
$docker volume --help
How it works
Our physical jenkins_home is created in our system.