Docker tutorials 5: sharing a portion of the file system with docker

1) Create a directory that will be shared with the docker machine, and let su create a file in it

mkdir mySharedDirectory
cat > mySharedDirectory/myFile

2a) Let us assume we have an image like the one created in Tutorial 3

docker images
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

FROM ubuntu:latest
MAINTAINER myname <>
RUN apt-get update
RUN apt-get -y upgrade
ADD mySharedDirectory /var/www/site
# By default, simply start a shell
CMD /bin/bash

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.


Docker volumes:

Tagged with:
Posted in Docker

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: