1) Create a directory that will be shared with the docker machine, and let su create a file in it
cat > mySharedDirectory/myFile
2a) Let us assume we have an image like the one created in Tutorial 3
REPOSITORY TAG IMAGE ID CREATED SIZE
myname myLinux2 50ee9e4f504d 13 minutes ago 215.9 MB
We can now run that image, mounting mySharedDirectory on /containerDir on the container
docker run --name myLinuxBox -v /full/path/to/mySharedDirectory:/containerDir -it myname:myLinux2
We can now find myFile in the docker machine at /containerDir/myFile. If we change it, the changes apply to myFile in mySharedDirectory in the host machine.
2b) In alternative. we can embed the mounting in the image creation process. We add to the Dockerfile we discussed in Tutorial 3 the “ADD” line
MAINTAINER myname <firstname.lastname@example.org>
RUN apt-get update
RUN apt-get -y upgrade
ADD mySharedDirectory /var/www/site
# By default, simply start a shell
Now, whenever we run this image, the /var/www/site directory will be mapped into the mySharedDirectory located in the place where we run the build.