-->

  • How to mount any local machine file or folder with docker container volume

     

    How to mount any local machine file or folder with docker container

     

    Docker Volumes



    What is a Docker Volume?

    A Docker Volume is used to store data permanently, even if the container is deleted.


    Without a volume:

    Container Deleted
          ↓
    Data Deleted
      



    With a volume:

     

    Container Deleted
          ↓
    Data Remains Safe  
    

     

     

    Why Do We Need Volumes?


    Imagine a MySQL container:

    • docker run mysql


    All database data is inside the container.


    If the container is removed:

    • docker rm mysql-container


    Your database is gone. 

     

    Docker local file to docker container


    How to use docker volume inside the docker ?


    First we will create a file or file with code in local machine and then mount with docker and run nodejs code with nodemon and we will see live code's output in docker.

    In our local machine --> go to desktop --> creating new folder --> then open in vs code --> then mount it

    • cd Desktop
    • mkdir app
    • code app



    Mount it 

    • docker run -it node:latest


     

    How to mount any local machine file or folder with docker container volume

     

     Now we will mount from in our local machine's folder that we have created a folder name is app wtih node folder. so for this you can check our folder is there - docker container ls -a 

     

     

    How to mount any local machine file or folder with docker container volume

     

     use this command - docker run -it -v 

     

    • -it -> interactive mode
    • -v -> volume (with which you can attach any volume from your own machine to our docker container).
    • copy of address of my folder 
    • :   -> then mount with my container's address 

     

     

    How to mount any local machine file or folder with docker container volume

     

    then you can see the above image and if we type uname there is linux and we will go docker container's directory cd home then ls for list directory.

     cd app

    ls

    there is a file called index.js  

    index.js file is coming from our local machine. 

     

     

    How to mount any local machine file or folder with docker container volumeHow to mount any local machine file or folder with docker container volume

     you can check here cat index.js 

     

     

    How to mount any local machine file or folder with docker container volume

     

    Now we will nodemon in our container and will run our node js file and if we try to change from our local machine then it will effect in our container where as we did run nodemon index.js file.

    so first we will install nodemon as a globally.

    npm install -g nodemon

      

     

    How to mount any local machine file or folder with docker container volume

     

    then we will run nodemon index.js  

     

     

    How to mount any local machine file or folder with docker container volume

     

     if we change anything in our file index.js in our local machine then it will changes effect here because both are pointing same memory location and so if you are unable to changes anything as a output so run this command.

    nodemon -L index.js 

     

     

    How to mount any local machine file or folder with docker container volume



    Second way to create docker volume

     

    This is the way to mount any file to docker container and now we will try to create a docker volume so for this  type this command and as well as open docker desktop that we have installed in previous blog article - check out Here - 

    CLICK HERE

    docker volume create data 

     

    data -> is the name of volume 

     

      

    How to mount any local machine file or folder with docker container volume


    Now you can check here if we type this command - docker volume ls

    here it is showing  local as a our Driver local machine 

    and

    Volume name as a data that we have created just now 

    Now we will attach our data volume to our docker container 

    use this command - 

    docker run -it -v data:/data node bash 

    Here you can check here is a folder called as data so go to this data folder and again type ls command and nothing is there 


    How to mount any local machine file or folder with docker container volume

    so we will create a file or folder atul.js for data volume and you can use this command mkdir atul.js and type again ls atul.js



    How to mount any local machine file or folder with docker container volume


    so now we can say that what we have created a data volume that its attached with our local file. so for this you see this

    open another terminal and type this command - 

    docke run -it -v data:/myapp ubuntu 

    go to this this directory

    cd myapp

    ls

    and you can see that there is same atul.js is there   



    How to mount any local machine file or folder with docker container volume


    lets say if we create a another file or folder in previous container terminal so we are able to see in another same file in our data volume because we have create a volume called as data and each and every container file pushed in volume data.

     

     

    How to mount any local machine file or folder with docker container volume


    So we have created a two different container and attached with data volume and now you can see the result.


     

     

    How to mount any local machine file or folder with docker container volume


    Conclusion



    Understanding Dockerfiles, custom image creation, and environment variables is essential for anyone learning Docker, DevOps, Cloud Computing, or System Administration.

    By mastering these concepts, you'll be able to create reusable Docker images, automate deployments, and configure applications efficiently. These skills are widely used in real-world production environments and are frequently asked about in DevOps and System Engineer interviews.



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.