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

[Enhancement]: Support Docker desktop on Linux #6426

Closed
sergeda opened this issue Jan 20, 2023 · 12 comments · Fixed by #6907 or #7058
Closed

[Enhancement]: Support Docker desktop on Linux #6426

sergeda opened this issue Jan 20, 2023 · 12 comments · Fixed by #6907 or #7058

Comments

@sergeda
Copy link

sergeda commented Jan 20, 2023

Module

Core

Proposal

Currently, when I try to run Testcontainers on Linux with Docker desktop installed I get this error:

2023-01-20T13:06:37.525GMT+02:00 INFO  o.testcontainers.DockerClientFactory  - 0 0 - Connected to docker: 
  Server Version: 20.10.21
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 4178 MB
2023-01-20T13:06:37.595GMT+02:00 INFO  🐳 [testcontainers/ryuk:0.3.3]  - 0 0 - Creating container for image: testcontainers/ryuk:0.3.3
2023-01-20T13:06:37.613GMT+02:00 INFO  o.t.utility.RegistryAuthLocator  - 0 0 - Credential helper/store (docker-credential-desktop) does not have credentials for https://index.docker.io/v1/
2023-01-20T13:06:37.745GMT+02:00 INFO  🐳 [testcontainers/ryuk:0.3.3]  - 0 0 - Container testcontainers/ryuk:0.3.3 is starting: 71cb2840729392cf9a66d23edfae5dcfcc2b5f36d9bf6e30e99052019455191f
2023-01-20T13:06:38.182GMT+02:00 ERROR 🐳 [testcontainers/ryuk:0.3.3]  - 0 0 - Could not start container
com.github.dockerjava.api.exception.NotFoundException: Status 404: {"message":"No such container: 71cb2840729392cf9a66d23edfae5dcfcc2b5f36d9bf6e30e99052019455191f"}

@kiview
Copy link
Member

kiview commented Jan 20, 2023

I have never tried out using Testcontainers with Docker Desktop on Linux. Can you please share the logs of the Ryuk container that failed to start? Does it work if you disable Ryuk?

@folofsson
Copy link

I've the same issue, overriding docker.host in .testcontainers.properties docker.host=unix:///home/USER/.docker/desktop/docker.sock does not work
15:22:08.245 [main] DEBUG com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager - ep-00000006: connection released [route: {}->**unix://localhost:2375]**[total available: 1; route allocated: 1 of 2147483647; total allocated: 1 of 2147483647] 15:22:08.246 [main] ERROR 🐳 [testcontainers/ryuk:0.3.4] - Could not start container com.github.dockerjava.api.exception.NotFoundException: Status 404: {"message":"No such container: 50141d58385bed1f7409fb6bcb05c699b55d2ba9a0696648a23a65c4751bd7ea"}

@vcvitaly
Copy link
Contributor

vcvitaly commented Feb 1, 2023

I have a Linux PC so let me try investigating this...

@vcvitaly
Copy link
Contributor

vcvitaly commented Feb 1, 2023

@sergeda @folofsson have you been able to resolve this? if not, could you please provide a full log? and also the results of

cat .testcontainers.properties
ls -la /var/run/docker.sock

As a temporary workaround, you can create a symlink like this:

sudo ln -s /home/vcvitaly/.docker/desktop/docker.sock /var/run/

I was able to run a test successfully this way.

@vcvitaly
Copy link
Contributor

vcvitaly commented Feb 1, 2023

@kiview Docker Desktop for Linux creates a socket in /home/$USER/.docker/desktop/docker.sock and neither EnvironmentAndSystemPropertyClientProviderStrategy nor UnixSocketClientProviderStrategy is able to handle this at the moment. Should I raise a PR to fix this?

I wonder why nobody has reported this until now, DD for Linux has been available since last May.

@eddumelendez
Copy link
Member

Thanks for the discovery and provide the workaround, @vcvitaly ! I think that maybe Docker Desktop for Linux is not that popular but of course there will be some users. At least, that's why I understand there has not been many reports.

I would suggest to improve docs instead of adding natively.

@vcvitaly
Copy link
Contributor

vcvitaly commented Feb 2, 2023

Ok, I hit a dead end.

I tried adding the following to the .testcontainers.properties - docker.host=unix:///home/vcvitaly/.docker/desktop/docker.sock

With that mount the ryuk container fails to start with the following error:

2023/02/02 17:21:20 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:39 +0x4e8

However when I do sudo ln -s /home/vcvitaly/.docker/desktop/docker.sock /var/run/ and clean up the properties, it mounts the symlink and is able to connect to the socket. This is something that experienced Linux users might be able to explain...

The workaround remains:

sudo ln -s /home/<YOUR_USERNAME>/.docker/desktop/docker.sock /var/run/

This is a brittle solution however since /var/run must be cleaned up on every restart and besides making this symlink requires Docker Desktop to be running so I'm not even sure that you can run this workaround on start-up.

@sergeda
Copy link
Author

sergeda commented Feb 19, 2023

For me when I try to do
sudo ln -s /home/<YOUR_USERNAME>/.docker/desktop/docker.sock /var/run/ I'm getting

ln: failed to create symbolic link '/var/run/docker.sock': File exists

@lgaida
Copy link

lgaida commented Feb 22, 2023

I ran into the same issue when using docker desktop on linux. I have used podman for quite a while and switched back to docker. Due to podman i was already familiar with "disabling ryuk".

I got testcontainers to work with docker desktop by doing

this

[...] adding the following to the .testcontainers.properties - docker.host=unix:///home/vcvitaly/.docker/desktop/docker.sock

then that

[...] Does it work if you disable Ryuk?

Proof:
export TESTCONTAINERS_RYUK_DISABLED=true && ./mvnw clean install -P runIT
(disabling ryuk on the fly for transparency, ideally export it somewhere else e.g in your bash profile)

02-22 07:46:28.393  INFO [Hidden Private App,,] 11890 --- [           main] l.p.p.r.HiddenPrivateITTest : The following 1 profile is active: "test"
2023-02-22 07:46:30.233  INFO [Hidden Private App,,] 11890 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-02-22 07:46:31.235  INFO [Hidden Private App,,] 11890 --- [           main] o.t.d.DockerClientProviderStrategy       : Found Docker environment with Environment variables, system properties and defaults. Resolved dockerHost=unix:///home/lenard/.docker/desktop/docker.sock
2023-02-22 07:46:31.237  INFO [Hidden Private App,,] 11890 --- [           main] org.testcontainers.DockerClientFactory   : Docker host IP address is localhost
2023-02-22 07:46:31.299  INFO [Hidden Private App,,] 11890 --- [           main] org.testcontainers.DockerClientFactory   : Connected to docker: 
  Server Version: 20.10.22
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 7644 MB
2023-02-22 07:46:31.299  INFO [Hidden Private App,,] 11890 --- [           main] org.testcontainers.DockerClientFactory   : Checking the system...
2023-02-22 07:46:31.301  INFO [Hidden Private App,,] 11890 --- [           main] org.testcontainers.DockerClientFactory   : ✔︎ Docker server version should be at least 1.6.0
2023-02-22 07:46:31.308  INFO [Hidden Private App,,] 11890 --- [           main] o.t.utility.ImageNameSubstitutor         : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
2023-02-22 07:46:31.375  INFO [Hidden Private App,,] 11890 --- [           main] o.t.utility.RegistryAuthLocator          : Credential helper/store (docker-credential-desktop) does not have credentials for index.docker.io
2023-02-22 07:46:33.482  INFO [Hidden Private App,,] 11890 --- [tream--89176971] org.testcontainers.DockerClientFactory   : Starting to pull image
2023-02-22 07:46:33.539  INFO [Hidden Private App,,] 11890 --- [tream--89176971] org.testcontainers.DockerClientFactory   : Pulling image layers:  0 pending,  0 downloaded,  0 extracted, (0 bytes/0 bytes)
2023-02-22 07:46:34.495  INFO [Hidden Private App,,] 11890 --- [tream--89176971] org.testcontainers.DockerClientFactory   : Pulling image layers:  0 pending,  1 downloaded,  0 extracted, (2 MB/2 MB)
2023-02-22 07:46:34.642  INFO [Hidden Private App,,] 11890 --- [tream--89176971] org.testcontainers.DockerClientFactory   : Pulling image layers:  0 pending,  1 downloaded,  1 extracted, (2 MB/2 MB)
2023-02-22 07:46:34.666  INFO [Hidden Private App,,] 11890 --- [tream--89176971] org.testcontainers.DockerClientFactory   : Pull complete. 1 layers, pulled in 1s (downloaded 2 MB at 2 MB/s)
2023-02-22 07:46:35.083  INFO [Hidden Private App,,] 11890 --- [           main] org.testcontainers.DockerClientFactory   : ✔︎ Docker environment should have more than 2GB free disk space
2023-02-22 07:46:35.233  INFO [Hidden Private App,,] 11890 --- [           main] 🐳 [postgres:13-alpine]                  : Pulling docker image: postgres:13-alpine. Please be patient; this may take some time but only needs to be done once.
2023-02-22 07:46:36.909  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Starting to pull image
2023-02-22 07:46:36.910  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  0 pending,  0 downloaded,  0 extracted, (0 bytes/0 bytes)
2023-02-22 07:46:37.708  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  7 pending,  1 downloaded,  0 extracted, (1 KB/? MB)
2023-02-22 07:46:37.714  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  6 pending,  2 downloaded,  0 extracted, (1 KB/? MB)
2023-02-22 07:46:37.792  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  6 pending,  2 downloaded,  1 extracted, (524 KB/? MB)
2023-02-22 07:46:37.847  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  6 pending,  2 downloaded,  2 extracted, (524 KB/? MB)
2023-02-22 07:46:38.256  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  5 pending,  3 downloaded,  2 extracted, (10 MB/? MB)
2023-02-22 07:46:38.745  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  4 pending,  4 downloaded,  2 extracted, (23 MB/? MB)
2023-02-22 07:46:38.757  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  3 pending,  5 downloaded,  2 extracted, (23 MB/? MB)
2023-02-22 07:46:41.596  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  2 extracted, (82 MB/? MB)
2023-02-22 07:46:42.752  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  3 extracted, (84 MB/? MB)
2023-02-22 07:46:42.850  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  4 extracted, (84 MB/? MB)
2023-02-22 07:46:42.956  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  5 extracted, (84 MB/? MB)
2023-02-22 07:46:43.066  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  6 extracted, (84 MB/? MB)
2023-02-22 07:46:43.161  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pulling image layers:  2 pending,  6 downloaded,  7 extracted, (84 MB/? MB)
2023-02-22 07:46:43.195  INFO [Hidden Private App,,] 11890 --- [eam--1618806371] 🐳 [postgres:13-alpine]                  : Pull complete. 8 layers, pulled in 6s (downloaded 84 MB at 14 MB/s)
2023-02-22 07:46:43.231  INFO [Hidden Private App,,] 11890 --- [           main] 🐳 [postgres:13-alpine]                  : Creating container for image: postgres:13-alpine
2023-02-22 07:46:43.778  INFO [Hidden Private App,,] 11890 --- [           main] 🐳 [postgres:13-alpine]                  : Starting container with ID: 7d3714bf9981a07812fec53ef51406e90e8ac645a3d7aed1327e74b50b1122c8
2023-02-22 07:46:44.048  INFO [Hidden Private App,,] 11890 --- [           main] 🐳 [postgres:13-alpine]                  : Container postgres:13-alpine is starting: 7d3714bf9981a07812fec53ef51406e90e8ac645a3d7aed1327e74b50b1122c8
2023-02-22 07:46:45.738  INFO [Hidden Private App,,] 11890 --- [           main] 🐳 [postgres:13-alpine]                  : Container postgres:13-alpine started in PT10.526561S
2023-02-22 07:46:45.982  INFO [Hidden Private App,,] 11890 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

eddumelendez added a commit that referenced this issue Apr 10, 2023
Docker Desktop for Linux installs the socket path at
`/home/username/.docker/desktop/docker.sock`. In Docker Desktop
for Mac 4.18, there is an option to disable the default Docker socket
(/var/run/docker.sock) and rely on `/home/username/.docker/run/docker.sock`
instead.

Fixes #6426
eddumelendez added a commit that referenced this issue Apr 11, 2023
Docker Desktop for Linux installs the socket path at
`/home/username/.docker/desktop/docker.sock`. In Docker Desktop
for Mac 4.18, there is an option to disable the default Docker socket
(/var/run/docker.sock) and rely on `/home/username/.docker/run/docker.sock`
instead.

Fixes #6426
@eddumelendez
Copy link
Member

When working on this for Docker for Mac, we realized this line https://github.com/testcontainers/testcontainers-java/blob/main/core/src/main/java/org/testcontainers/utility/RyukContainer.java#L23

should be /var/run/docker.sock instead of the rootless path. Wonder if that the same for DD for Linux. I guess so. I need some help here :) I've failed setting up DD for Linux in a VM.

@Toparvion
Copy link

As an in-between summary: TestContainers v1.18.1 out-of-the-box does not work with vanilla Docker Desktop for Linux v4.19.0 (checked on Ubuntu 23.04) failing with:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock

The reason is the same difference between Docker Engine and Docker Desktop.
In my case, both suggested workarounds are applicable:

  • either set DOCKER_HOST=unix:///home/<USERNAME>/.docker/desktop/docker.sock & TESTCONTAINERS_RYUK_DISABLED=true environment variables
    (or the same through ~/.testcontainers.properties file but in corresponding notation)
  • or make sudo ln -s /home/<USERNAME>/.docker/desktop/docker.sock /var/run/
    (and not forget to re-create it on the next start)

However, none of them looks scalable for the whole development team with similar local environments.

Maybe a naive suggestion, but what if TestContainers as a last resort could look into current machine's Docker CLI config and pick the Docker endpoint from there:

$ docker context show
desktop-linux

$ docker context ls
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                     KUBERNETES ENDPOINT   ORCHESTRATOR
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                               
desktop-linux *     moby                                                          unix:///home/vladimir/.docker/desktop/docker.sock                         

?
(or directly fetch this data from JSON config files in ~/.docker/ directory)

@HofmeisterAn
Copy link
Contributor

The correct configuration (for Docker Desktop running rootless) is:

  • DOCKER_HOST=unix:///home/<USERNAME>/.docker/desktop/docker.sock
  • TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock

Testcontainers extracts the socket path from the Docker host configuration (e.g. from the DOCKER_HOST environment variable; removes the schema). Since Docker Desktop runs containers inside a VM, we need to pass the socket path from inside the VM, which is /var/run/docker.sock. To support it out of the box, we need to distinguish between runtimes that run in a VM and those that do not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants