16

I used a docker image to run a program on our school's server using this command.

 docker run -t -i -v /target/new_directory 990210oliver/mycc.docker:v1 /bin/bash

After I ran it it created a firectory on my account called new_directory. Now I don't have permissions to delete or modify the files.

How do I remove this directory?

mohan08p
  • 5,002
  • 1
  • 28
  • 36
Katie
  • 239
  • 1
  • 2
  • 9

7 Answers7

23

I also had this problem.

After:

docker run --name jenkins -p 8080:8080 -v $HOME/jenkins:/var/jenkins_home jenkins jenkins

I couldn't remove files in $HOME/jenkins.

Ricardo Branco's answer didn't work for me because chown gave me:

chown: changing ownership of '/var/jenkins_home': Operation not permitted

Solution:

exec /bin/bash into container as a root user:

docker exec -it --privileged --user root container_id /bin/bash

then:

cd /var/jenkins_home/ && rm -r * .*
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
siulkilulki
  • 1,054
  • 1
  • 10
  • 18
  • what if the the contrainer, in which I created the folder, has been deleted? – zheyuanWang Jul 27 '21 at 09:53
  • Then run another similar container, and within it, I guess you would have the necessary permissions to delete what was created by the previous one. – siulkilulki Sep 27 '21 at 12:25
5

I made @siulkilulki's answer into one line:

docker exec --privileged --user root <CONTAINER_ID> chown -R "$(id -u):$(id -g)" <TARGET_DIR>

Note that here the CONTAINER must be up.

pcc426
  • 191
  • 2
  • 2
2

Change the owner of all the files on the directory to your used ID within the container running as root, then exit the container and remove the directory.

docker run --rm -v /target/new_directory 990210oliver/mycc.docker:v1 chown -R $(id -un):$(id -un) /target/new_directory
exit
rm -rf $HOME/new_directory
Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
2

I had the same problem. I am using ubuntu 18.04. I ran the following code and then I was able to delete files locally. I have app dir inside docker project dir

  1. cd to your docker project dir
  2. sudo chown -R $(whoami):$(whoami) app/
Aseem
  • 5,848
  • 7
  • 45
  • 69
0

docker run -v {absolute path to dir with the file}:/to_delete -it ubuntu /bin/bash

Then just:

$ cd to_delete

$ rm -rf <file/dir>

DarcliGht
  • 1,399
  • 2
  • 10
  • 14
0

Here is a solution that does not require --privileged.

Game Plan

  1. Determine UIDs of all offending files created by previous docker runs. Use docker to find them, since in-container UID is not the same as host UID. An offending file is any file not owned by container user root which maps to the current user running docker.
  2. Run a container using each discovered UID and delete the offending files (or chown them).

Code

# Assumes that current dir is the volume
# find files owned by docker internal uuids (not root) on the mounted volume:
BAD_FILE_UIDS=$(docker run --rm -v $(pwd):/build alpine sh -c 'find /build -mindepth 1 -not -user root | xargs stat -c "%u" | sort -u')
if [ -n "${BAD_FILE_UIDS}" ] ; then
  for uuid in $BAD_FILE_UIDS ; do
    echo "Cleaning up files owned by $uuid using docker"
    docker run --rm -v $(pwd):/build --user $uuid:0 alpine find /build -mindepth 1 -user $uuid -delete
  done
fi

You can change the -delete to -exec chown SOME_USER {} \; to chown.

The above works well for use in CI as post-build cleanup.

Akom
  • 1,484
  • 19
  • 25
-2

Try this:

docker stop $CONTAINER_NAME
docker rm -v $CONTAINER_NAME

I guess this should remove the mounted dir. If it doesn't, do this explicitly:

sudo rm -rf /target/new_directory
Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64
  • `docker rm -v container_name` don't delete bind mount volumes. OP said that he "don't have permissions to delete or modify the files" so `sudo rm -rf /target/new_directory` is useless – siulkilulki Apr 13 '18 at 18:10