When using Colima to start and run docker containers facing issue when creating container from image testcontainers/ryuk

107 Views Asked by At
docker run <ryuk_image_id> 

fails because of the following error:

panic: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I have set up the following in ~/.zprofile (I am using Mac):

export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock
export DOCKER_HOST="unix://${HOME}/.colima/default/docker.sock"
export TESTCONTAINERS_HOST_OVERRIDE=$(colima ls -j | jq -r '.address'))

and started colima:

colima start --network-address

Notice that I cannot use Docker desktop

I am trying to use Colima and I am expecting that Ryuk is pointing to:

${HOME}/.colima/default/docker.sock

Also .testcontainers.properties has the following configuration:

docker.client.strategy=org.testcontainers.dockerclient.UnixSocketClientProviderStrategy

1

There are 1 best solutions below

0
Andrey On

It's not completely clear what's happening from the description, i.e. who is running docker run — is it you or TestContainers during app tests run? Log snippets would be helpful. But the error message you're getting (Cannot connect to the Docker daemon at unix:///var/run/docker.sock) makes me think that your DOCKER_HOST environment variable is not picked up by the process which is interacting with docker.

First I'd check docker context ls output in a brand new terminal window. If DOCKER_HOST is set, you'll see something like this:

NAME        DESCRIPTION                               DOCKER ENDPOINT                                     ERROR
colima      colima                                    unix:///Users/youruser/.colima/default/docker.sock
default *   Current DOCKER_HOST based configuration   unix:///Users/youruser/.colima/default/docker.sock
Warning: DOCKER_HOST environment variable overrides the active context. To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.

If instead you see the default unix:///var/run/docker.sock endpoint under the default profile, then I guess something is wrong with the way you set DOCKER_HOST in your environment.

If that's not it, then maybe something is wrong with the way you launch your process? Are you running it from an IDE? If so, have you restarted your IDE after setting up your exports?

Couple more thoughts based on your question:

  1. Looks like you started colima after setting up the environment variables. Then probably you didn't get TESTCONTAINERS_HOST_OVERRIDE right in that terminal session because colima wasn't started yet and hence there was no address yet. Anyway, try echo $TESTCONTAINERS_HOST_OVERRIDE and see if there's a valid IP in there.

  2. I don't think you need to override docker.client.strategy, at least I didn't have to with the same setup.

Alternative setup

Although the setup you're using is described in the official colima docs, I use a different setup which I consider to be more simple. All you need is to setup a symbolic link like this:

sudo ln -s /Users/youruser/.colima/default/docker.sock /var/run/docker.sock

You don't need to setup the DOCKER_HOST, TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE, TESTCONTAINERS_HOST_OVERRIDE env vars.

Couple of caveats though:

  1. Double check colima docker socket location, as it may be different depending on colima version. You can use colima status for that.
  2. You still need to set TESTCONTAINERS_HOST_OVERRIDE if you run your colima with --network-address flag. Note: if you ever used the --network-address flag, it persists even if you restart colima without it (it is saved in .colima/default/colima.yaml), so you either need to do colima delete or manually unset it if you no longer require network address.