testcontainers / moby-ryuk

Schedule Moby/Docker containers cleanup after specific delay.
https://www.testcontainers.com
MIT License
190 stars 34 forks source link

Cannot connect to the Docker daemon ($DOCKER_HOST - non typical docker.sock location - no root permission ) #161

Closed jpmartins closed 1 month ago

jpmartins commented 1 month ago

Hello I am having error starting the testcontainers/ryuk on my docker daemon

docker run testcontainers/ryuk

2024/09/12 12:10:28 Pinging Docker...
panic: **Cannot connect to the Docker daemon at unix:///var/run/docker.sock**. Is the docker daemon running?

goroutine 1 [running]:
main.main()
/go/src/github.com/testcontainers/moby-ryuk/main.go:36 +0x457
jpmartins commented 1 month ago

On my docker host the dock.sock (DOCKER ENDPOINT) is at $DOCKER_HOST (unix://$HOME/.colima/default/docker.sock).

Anyone can help?

jpmartins commented 1 month ago
ls -ltr $HOME/.colima/default/docker.sock
srw-------  1 P002979  staff  0 Sep 12 10:13 /Users/xxx/.colima/default/docker.sock
colima status
INFO[0000] colima is running using QEMU                 
INFO[0000] arch: x86_64                                 
INFO[0000] runtime: docker                              
INFO[0000] mountType: sshfs                             
INFO[0000] socket: unix:///Users/xxx/.colima/default/docker.sock 
docker ps -a | head -n 3
CONTAINER ID   IMAGE                       COMMAND                  CREATED              STATUS                          PORTS                                                                                                                                           NAMES
07b8730e8c2b   testcontainers/ryuk         "/app"                   About a minute ago   Exited (2) About a minute ago                                                                                                                                                   elated_bassi
9440a2d34563   testcontainers/ryuk         "/app"                   3 minutes ago        Exited (2) 3 minutes ago                                                                                                                                                        musing_hypatia
docker run -v $HOME/.colima/default/docker.sock:/var/run/docker.sock -e RYUK_PORT=8080 -p 8080:8080 testcontainers/ryuk
2024/09/12 13:29:30 Pinging Docker...
panic: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

goroutine 1 [running]:
main.main()
    /go/src/github.com/testcontainers/moby-ryuk/main.go:36 +0x457
mdelapenya commented 1 month ago

Hi @jpmartins I guess you already took a look at https://golang.testcontainers.org/system_requirements/using_colima/

Did you follow the steps in that guide?

jpmartins commented 1 month ago

Thanks for you input mdelapenya.

That guide, on the "other applications that are unaware of Docker context the following workaround" requires something I do not have permission to do, this is a work macbook and on it I do not have a way to do:

sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock

It would be nice to have other alternatives... currently my workaround is using docker desktop instead of colima.

jpmartins-ca commented 1 month ago

Hi @jpmartins I guess you already took a look at https://golang.testcontainers.org/system_requirements/using_colima/

Did you follow the steps in that guide?

By the way, some of the instructions I identified what seem to be issues that I could not figure out how to fix. More specifically the TESTCONTAINERS_HOST_OVERRIDE initialisation is returning null:

export TESTCONTAINERS_HOST_OVERRIDE=$(colima ls -j | jq -r '.address')
 ~ % echo $TESTCONTAINERS_HOST_OVERRIDE
null
 ~ % echo $(colima ls -j | jq -r '.address') 
null
 ~ % jq --version
jq-1.7.1
 ~ % colima --version
colima version 0.7.5
 ~ % echo $(colima ls -j | jq -r '.address')
null
 ~ % echo $(colima ls -j | jq -r)           
{ "name": "default", "status": "Running", "arch": "x86_64", "cpus": 2, "memory": 4294967296, "disk": 64424509440, "runtime": "docker" }
jpmartins-ca commented 1 month ago

Suspect some of the issues might be related with running colima emulating x86_64 under a arm64 cpu.

kiview commented 1 month ago

Hi @jpmartins, why are you running Ryuk manually using the Docker CLI? Testcontainers tries to configure the socket location provided to Ryuk depending on your environment. If this is not working, you should open the issue on the Testcontainers side.

When running Ryuk manually (which is not something Testcontainers users need to do, this is only relevant for Testcontainers developers), you need to mount the socket as per README:

docker run -v /var/run/docker.sock:/var/run/docker.sock -e RYUK_PORT=8080 -p 8080:8080 

So, if this is a Testcontainers issue, let's please close this issue here and instead open it in the corresponding Testcontainers language project.

jpmartins-ca commented 1 month ago

I had a ticket on testcontainers... I closed it and opened this one since the error is on starting the ryuk container... calling it directly has the goal to try to replicate the issue by the command line outside testcontainers. To try to show, that from my perspective there are issues on ryuk side.

Are you saying only a ticket on testcontainers-java side can troubleshoot this?

Since I have limited permissions, it is not possible to have default/standard location of docker.sock on my machine, so /var/run/docker.sock does not exist on my machine, and I do not have admin or sudo rights to create it. That is why I pointed to the existing /Users/_user/.docker/run/docker.sock while starting the ryuk.

For documentation proposes on issues with start ryuk, if your docker-desktop on arm64 suddenly stops working with:

error while creating mount source path '/host_mnt/Users/P002979/.docker/run/docker.sock': mkdir /host_mnt/Users/_user/.docker/run/docker.sock: operation not supported

Following the https://github.com/docker/for-mac/issues/4859#issuecomment-689012097, and changing to "osxfs (Legacy)" in the Advanced Docker Destkop worked for my case.

kiview commented 1 month ago

The important thing here is, that we don;t start to mix Testcontainers features into this issue then if we want to specifically debug running Ryuk (like TESTCONTAINERS_HOST_OVERRIDE or general Colima instruction for Testcontainers usage, which have nothing to do with running Ryuk manually).

In your case, I guess you would need to run Ryuk like this (I didn't see you doing it in your previous posts), although I am entirely not sure how to indicate to Colima, to mount the socket from within the Colima VM instead of mounting through the host (also see https://github.com/abiosoft/colima/discussions/688#discussioncomment-5536729):

docker run -v  /var/run/docker.sock:/var/run/docker.sock -e RYUK_PORT=8080 -p 8080:8080

currently my workaround is using docker desktop instead of colima.

Using Docker Desktop is the officially supported runtime by Testcontainers, we don't officially support other container runtimes and it is up to users configure things in a way to work similar to Docker Desktop.

Are you saying only a ticket on testcontainers-java side can troubleshoot this?

Since it seems your goal is about running testcontainers-java, and not about manually running Ryuk, yes, the correct approach is go through testcontainers-java.

jpmartins commented 1 month ago

You were right, sorry for the inconvenience, hope you have a nice weekend, and thank you for the help on Slack.

As you now know Kevin, with -v /var/run/docker.sock:/var/run/docker.sock alone did not cut it, the issue was also some leftover environment variables from my first attempt with colima, the DOCKER_HOST env var was set to point to docker.sock of Docker-Desktop. DOCKER_HOST should not be set for Docker-Desktop.

Temporary disable of ryuk is an option to continue exploring/learning, but will have the test container not being discarded that way.

Thank you very much.