enderland
enderland

Reputation: 14175

How to mount current directory as read-only but still allow changes inside the container?

I have a situation where:

Basically I want to be able to edit the files in the mounted volume but not have those changes persisted.

My current workflow is to create a dummy container using a dockerfile:

ADD . /mycode

and then execute that container.

However as the repository grows, this step takes longer and longer to perform, because the only way I can think is to make a complete copy of ~/tmp/mycode in order to be able to manipulate the files in the container.

I've also thought about mounting the directory and copying it inside the container and committing that container, but that has the same issue.

Is there a way to run a docker container to allow file edits without persisting them on the host short of copying the whole directory?

I am using the latest docker for mac, currently Version 17.03.1-ce-mac5 (16048).

Upvotes: 5

Views: 2790

Answers (1)

Jiri Klouda
Jiri Klouda

Reputation: 1370

This is fairly trivial to do with docker and overlay:

docker run --name myenv --privileged -v /my/local/path/tmp/mycode:/mnt/rocode:ro -it ubuntu /bin/bash
docker exec -d myenv /sbin/mount -t overlay overlay -o lowerdir=/mnt/rocode,upperdir=/mycode,workdir=/mnt/code-workdir /mycode

This should mount the code from your directory read only and create the overlay inside the container so that /mnt/rocode is read only, but /mycode is writable.

Make sure that your kernel is 3.18+ and that you have overlay in your /proc/filesystems.

Upvotes: 1

Related Questions