Docker and Volumes nocopy

16,856

Solution 1

There might be some way you can hide the file but with Docker volumes what you describe is not possible as the directory on the container is a mount of the directory on the host and not files being copied.

The nocopy modifier is for when you are creating a volume and data already exists in the container's path, you can specify if you want that data copied when the volume is created.

You probably need to design a volume structure so the file you are creating isn't on the shared volume.

Solution 2

I know this is an old question, but it might help people.

If you want to reflect modifications from the host to the container, but not the other way around, it will be hard.

If you want to get a "snapshot" of the code, you can get the code inside your container using COPY or ADD when building your image, which means that your code must be in the building context. For example :

.
├── content_to_mount
|   ├── stuff1.txt
|   └── stuff2.txt
├── docker_stuff
|   ├── docker.conf
|   └── Dockerfile
└── other_stuff

If you have that kind of file tree, you will need to build your docker image using something like the following command in the root directory (where you can see docker_stuff):

docker build -f docker_stuff/Dockerfile .

and in you Dockerfile, you would have something like :

COPY content_to_mount app

That can be "dangerous" if the code for your app is really in your home, because you shouldn't have too big a context for your docker build.

Another solution, but that would be a bit weird in the process, would be to mount stuff like so :

$ docker run -d --name webserver01 -v /home/guest/app:/ref/app

and have a specific entrypoint in your container that does a :

cp -R /ref/app /app

Then, you would have your reference code, the one mounted from the host, in /ref/app and the modifications you would run in /app wouldn't be reflected on the host.

Share:
16,856
mrbit01
Author by

mrbit01

Updated on June 09, 2022

Comments

  • mrbit01
    mrbit01 about 2 years

    I want to mount a directory from my host to a Docker container, and I want to make some modifications inside the container. But, when I modify the content on the mounted point, the changes are also reflected on the host.

    For example:

    $ docker run -d --name webserver01 -v /home/guest/app:/app
    

    After which I made some change inside the container in /app

    $ docker exec -ti webserver01 'touch /app/test.txt'
    

    The file test.txt is created on my host and inside the container (the idea is to only create the file inside the container)

    I tried to use the flag nocopy but I have this error:

    $ docker run -d --name webserver01 -v /home/guest/app:/app:nocopy
    docker: Error response from daemon: Invalid bind mount spec "/home/guest/app:/app:nocopy": invalid mode: nocopy.
    

    Docker version

    $ docker --version
    Docker version 1.11.1, build 5604cbe/1.11.1
    

    Thanks for the help!

  • mrbit01
    mrbit01 almost 8 years
    I try another approach copying the files with the ADD command on the Dockerfile but the files are in different place than the Dockerfile and Docker don't allow this :(
  • Baruch
    Baruch almost 5 years
    Link to the nocopy official docs: docs.docker.com/compose/compose-file/#long-syntax-3 for clarity.
  • Wes Turner
    Wes Turner over 3 years
    Updated link to the nocopy docs in the compose specification: github.com/compose-spec/compose-spec/blob/master/…