You can use docker run or docker container run command with -v option to mount a host directory in a docker container.
Syntax:
1 2 3 4 5 6 7 8 9 |
## Old docker versions docker run -dt --privileged=true \ -v /some/path/in/host/:/some/path/in/docker/container \ --name ## New docker versions docker container run -dt --privileged=true \ -v /some/path/in/host/:/some/path/in/docker/container \ --name |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## Create a directory in host machine mkdir ~/mydir && echo "hello world" > ~/mydir/myfile.txt ## Create a new docker container with host directory as mount docker container run -dt --privileged=true \ -v ~/mydir:/mydir \ --name mycontainer ubuntu ## Check if the directory is mounted docker container exec -it mycontainer ls /mydir ## Returns myfile.txt ## Create a new file in the host directory echo "welcome to cloudaffaire" > ~/mydir/mynewfile.txt ## Check if the new file is available inside the container docker container exec -it mycontainer ls /mydir ## Returns myfile.txt mynewfile.txt ## Remove the docker container docker container stop mycontainer docker container prune -f |