Open keneanung opened 6 months ago
can you confirm this approach:
mkdir test-docker-rootless-detection ; cd $_ ; python -m venv .venv && . $_/bin/activate
pip install docker
cat > detect_rootless.py <<EOF
from docker import from_env
from docker.client import DockerClient
from docker.models.containers import Container, ContainerCollection
def is_rootless(client: DockerClient):
info = client.info()
sec_opts = info.get('SecurityOptions') or tuple()
return any('rootless' in s for s in sec_opts)
if __name__ == "__main__":
print(is_rootless(from_env()))
EOF
python detect_rootless.py
and then i guess we will need to tweak the Reaper class a bit in core
can you confirm this approach:
mkdir test-docker-rootless-detection ; cd $_ ; python -m venv .venv && . $_/bin/activate pip install docker cat > detect_rootless.py <<EOF from docker import from_env from docker.client import DockerClient from docker.models.containers import Container, ContainerCollection def is_rootless(client: DockerClient): info = client.info() sec_opts = info.get('SecurityOptions') or tuple() return any('rootless' in s for s in sec_opts) if __name__ == "__main__": print(is_rootless(from_env())) EOF python detect_rootless.py
This returns the following:
$ python detect_rootless.py
True
I also use rootless Docker and I can confirm this issue.
I can also confirm that setting TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/run/user/$(id -u)/docker.sock
fixes the issue. Alternatively, I have tried disabling Ryuk through TESTCONTAINERS_RYUK_DISABLED=true
, which makes the tests run flawlessly as well.
Would it be possible to fix this so that testcontainers
just works with rootless docker? @alexanderankin
TESTCONTAINERS_RYUK_DISABLED=true pytest
works, but it's a pain to use, and we can't even just os.environ['TESTCONTAINERS_RYUK_DISABLED'] = 'true'
inside the python test file
Nowadays any dev slightly conscious should be using rootless docker (because it is safer, and because it reduce friction with file ownerships when mounting volumes inside an image that use the root user. So not only it is safer, but it is also easier to use...). It's a bit the future of containers, but it's already there and working. So testcontainers
should be available for rootless docker without the need for complex setup
you can import the config module and set it on the dataclass there
Ah, we have confirmed that the logic i provided above works. Then all that remains is plugging it in I suppose. I'll accept PR that fixes the issue. yes.
I am not forced to use docker rootless neither, it's just that it's better than the root option, especially when you use containers a lot for development, try it you'll see :) Rootless docker can be easily enabled following these docs: https://docs.docker.com/engine/security/rootless/ usually 3 commands:
dockerd-rootless-setuptool.sh install
systemctl --user enable docker
loginctl enable-linger $UID
Unfortunately I just realized that ryuk
is needed to stop the containers after running the tests. So disabling it is not a sustainable solution
But TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/run/user/$(id -u)/docker.sock
works fine
An easy solution would be to just change the default RYUK_DOCKER_SOCKET
depending on if rootless is detected or not here: https://github.com/testcontainers/testcontainers-python/blob/main/core/testcontainers/core/config.py#L15
I might look into this if I find sometimes and will send a PR
Describe the bug
When Docker is run in rootless mode, the
ryuk
fails to start as the docker socket mounted as a volume has the wrong permissions (nobody:nobody
). This can be fixed be mounting the correct socket at/run/user/$(id -u)/docker.sock
.The overwrite can be done by setting the environment variable
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE
to the above mentioned socket.Ideally, this would be detected and configured automatically (ie. using
Server.Security Options.rootless
andEndpoints.docker.Host
of the active context or theDOCKER_HOST
environment variable).If this is not an option, please consider documenting this case more in detail.
To Reproduce
Configure Docker to run in rootless mode (see https://docs.docker.com/engine/security/rootless/) and run the following snippet:
Runtime environment
Provide a summary of your runtime environment. Which operating system, python version, and docker version are you using? What is the version of
testcontainers-python
you are using? You can run the following commands to get the relevant information.