SomethingSomething
SomethingSomething

Reputation: 12178

Making docker container write files that the host machine can delete

I have a docker-based build environment - in order to build my project, I run a docker container with the --volume parameter, so it can access my project directory and build it.

The problem is that the files created by the container cannot be deleted by the host machine. The only workaround I currently have is to start an interactive container with the directory mounted and delete it.

Bottom line question: It is possible to make docker write to the mounted area files with permissions such that the host can later delete them?

Upvotes: 0

Views: 1090

Answers (2)

larsks
larsks

Reputation: 311606

This has less to do with Docker and more to do with basic Unix file permissions. Your docker containers are running as root, which means any files created by the container are owned by root on your host. You fix this the way you fix any other file permission problem, by either (a) ensuring that that the files/directories are created with your user id or (b) ensuring that permissions allow you do delete the files even if they're not owned by you or (c) using elevated privileges (e.g., sudo rm ...) to delete the files.

Depending on what you're doing, option (a) may be easy. If you can run the contanier as a non-root user, e.g:

docker run -u $UID -v $HOME/output:/some/container/path ...

...then everything will Just Work, because the files will be created with your userid.

If the container must run as root initially, you may be able to take care of root actions in your ENTRYPOINT or CMD script, and then switch to another uid to run the main application. To do this, you would need to pass your user id into the container (e.g., as an environment variable), and then later use something like runuser to switch to the new userid:

exec runuser -u $TARGE_UID  /some/command

If neither of the above is an option, then sudo rm -rf mydirectory should work just as well as spinning up an interactive container.

Upvotes: 3

Pavel Agarkov
Pavel Agarkov

Reputation: 3783

If you need your build artifacts just to put them to the docker image on the next stage then it is probably worth to use multi-stage build option.

Upvotes: 0

Related Questions