skip to Main Content

I need to compile some programs using files in a docker container. Once compiled, the container is no longer used.

Therefore I always use the following command. docker run --rm -v my_file:docker_file my_images my_command

But I find that there are always some problems.

  1. For example, take a simple C language program that outputs "hello, world" as an example.
    docker run -it --rm -v /home/cuiyujie/workspace/workGem5/gem5/hello.c:/home/cuiyujie/workspace/workGem5/gem5/hello.c -v /home/cuiyujie/workspace/workGem5/gem5/build:/home/cuiyujie/workspace/workGem5/gem5/build  gerrie/gem5-bare-env

After entering the container, execute gcc hello.c -o hello, cp hello build.

I found outside the container that the hello file belongs to root.

-rwxr-xr-x 1 root root 16696 2月 23 10:23 hello*

I don’t have permission to delete it. what should I do to make it become the permissions of the host user?

2

Answers


  1. You can use chown to change the ownership of a file. You’ll need permission to run it with sudo though.

    $ sudo chown $USER hello
    

    If you also want to change the group of the file to your primary group, you can put a . after the user:

    $ sudo chown $USER. hello
    
    Login or Signup to reply.
  2. If you run your container as your own UID, files created in the host volumes will be owned by your UID. That comes with the disclaimer that your container needs to be designed to run as a user other than root (e.g. not need access to files owned by root inside the container). Here’s an example of running as your uid/gid and full access to your home directory using bash on Linux (the $(id -u) expansion may not work in other environments):

    docker container run 
      -u "$(id -u):$(id -g)" -w "$(pwd)" 
      -v /etc/passwd:/etc/passwd:ro 
      -v /etc/group:/etc/group:ro 
      -v "$HOME:$HOME" 
      <your_image>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search