Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path is not mounted correctly when running Docker hooks from Docker #1387

Closed
okainov opened this issue Apr 6, 2020 · 6 comments · Fixed by #1888
Closed

Path is not mounted correctly when running Docker hooks from Docker #1387

okainov opened this issue Apr 6, 2020 · 6 comments · Fixed by #1888

Comments

@okainov
Copy link
Contributor

okainov commented Apr 6, 2020

Situation:

  • In our CI we want to run pre-commit inside Docker.
  • Some of our hooks are docker_image

Problem
This line mostly

'-v', f'{os.getcwd()}:/src:rw,Z',

Currently pre-commit mounts the current directory to /src and uses current directory name as mount base.
However this does not work when pre-commit is run inside the container on some mounted path already, because mount points are relative to the host, not to the container.

Example:

/opt/my_code      <- host, mounts /opt/my_code:/project
/project              <- in Docker running pre-commit, pre-commit is doing mount /project:/src
/src                   <-  (in Dockerized hook)

Currently pre-commit will try to mount it as -v /project:/src,rw,Z. Expected - to mount it as -v /opt/my_code:/src

Possible solution:

When I replaced os.getcwd() from the code above to translate_path(os.getcwd()) where translate_path is taken from https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a, it worked perfectly. It does add extra docker pip-dependency though.

See also: https://forums.docker.com/t/mounting-a-volume-not-working-with-running-docker-in-docker/25775/2

@asottile
Copy link
Member

asottile commented Apr 6, 2020

the docker* hook type assume you're on the host -- changing that breaks some of our assumptions but it might be possible to implement your suggestion

the call into docker-py can be replaced with subprocess.check_call(('docker', 'inspect', hostname)) -- would you like to take a stab at fixing it?

@asottile
Copy link
Member

@okainov would you be interested in working on this?

@okainov
Copy link
Contributor Author

okainov commented Apr 23, 2020

@asottile I'd really like to and I have also the update for not using docker dependency, but there might be some bureaucracy issues from my side, so I'm not sure whether I could publish it soon...

@djh82
Copy link

djh82 commented Oct 3, 2020

Is there any appetite for mounting in a similar way to jenkins, where the docker container essentially becomes transparent since it would be mounted like so:

-v os.getcwd():os.getcwd()

@asottile
Copy link
Member

asottile commented Oct 4, 2020

that would be a breaking change from the current set of guarantees

okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

See https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a
Fixes pre-commit#1387
@okainov
Copy link
Contributor Author

okainov commented Apr 21, 2021

@asottile it took me some time :D But here is PR #1885, feel free to take a look and provide feedback.

okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

See https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a
Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

See https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a
Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

See https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a
Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

See https://gist.github.com/dpfoose/f96d4e4b76c2e01265619d545b77987a
Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 21, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 22, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 23, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
okainov added a commit to okainov/pre-commit that referenced this issue Apr 27, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
asottile pushed a commit to okainov/pre-commit that referenced this issue Apr 29, 2021
Currently pre-commit mounts the current directory to /src and uses
current directory name as mount base.
However this does not work when pre-commit is run inside the container
on some mounted path already, because mount points are relative to the
host, not to the container.

Fixes pre-commit#1387
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants