Docker, mount all user directories to container

Viewed 124

add -v option can mount directories to the container, for example, mounting /home/me/my_code to the container, and when in the container, we can see the directory.

Currently, in my Dockerfile, the user is docker and the workspace is /home/docker, and how can I mount all my directories in /home/me to /home/docker? So that when I enter into the container, it would be very convenient to run my task and explore files like in /home/me.

1 Answers

While building a image through dockerfile, COPY or ADD is used to copy a file with necessary content in the process of building the image example, installing npm binaries and all.

Since you are looking to have the flexibility of having a same local FS as inside the conatiner, you can try out "Bind Mounts".

bash-3.2$ docker run \
>       -it \
>       --name devtest \
>       --mount type=bind,source=/Users/anku/,target=/app \
>       nginx:latest \
>       bash
root@c072896c7bb2:/# 
root@c072896c7bb2:/# pwd
/
root@c072896c7bb2:/# cd app
root@c072896c7bb2:/app# ls
 Applications   Documents   Library   Music  Projects   PycharmProjects   anaconda3  'iCloud Drive (Archive)'  'pCloud Drive'   testrun.bash
 Desktop        Downloads   Movies    Pictures   Public    'VirtualBox VMs'   gitlab      minikube-linux-amd64      starup.sh
root@c072896c7bb2:/app# 

There are two kinds of mechanism to mange persisting data.

  1. Volumes are completely managed by Docker.
  2. Bind Mount, mounts a file or directory on the host machine into container. Any changes made from the host machine or from inside the container are synced.

Suggest to go through Differences between --volume and --mount behavior

Choose what best work for you.

Related