testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

todo - run tests against docker desktop on linux

Open alexanderankin opened this issue 1 year ago • 4 comments

from discussion in #610

alexanderankin avatar Jun 28 '24 03:06 alexanderankin

Let me know if I can assist you. Ain't got too much time to spare but I'll do my best ;)

black-snow avatar Jun 28 '24 18:06 black-snow

My little research.

Introductory:

  1. Fedora Workstation 42 x86_64
  2. Docker Desktop

Let's look at running a basic example from the official website

from testcontainers.postgres import PostgresContainer
import sqlalchemy

with PostgresContainer("postgres:16") as postgres:
    psql_url = postgres.get_connection_url()
    engine = sqlalchemy.create_engine(psql_url)
    with engine.begin() as connection:
        version, = connection.execute(sqlalchemy.text("SELECT version()")).fetchone()

print(version)

When DockerContainer is initialized, DockerClient is called, which gets the docker_host from tc.host in the ~/.testcontainers.properties file or from the DOCKER_HOST environment variable.

Because of this, attempting to start fails with the error docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')).

This problem can be solved in ~/.bashrc by setting the environment variable export DOCKER_HOST=unix://${HOME}/.docker/desktop/docker.sock or in ~/.testcontainers.properties by setting tc.host==unix://home/username/.docker/desktop/docker.sock. By the way, TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE environment does not solve the problem.

Then we start to crash with the error docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.51/containers/c57d89b2acb9f9fd4997576bdf62204daacbdfcf01006555cd0cda9e7f3624f7/start: Internal Server Error ("mounts denied: The path /socket_mnt/home/alexelizard/.docker/desktop/docker.sock is not shared from the host and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing. See https://docs.docker.com/ for more info.")

This is an error on startup Ryuk. I couldn't figure out where Ryuk gets the /socket_mnt. Using export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=${HOME}/.docker/desktop/docker.sock doesn't fix the issue with /socket_mnt.

The solution was found in testcontainers-java #7036 - you need to set the environment variable export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=host.docker.internal.

In summary: to run testcontainers via Docker Desktop in Fedora Workstation, you need to set the following environment variables in ~/.bashrc:

export DOCKER_HOST=unix://${HOME}/.docker/desktop/docker.sock
export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=host.docker.internal

Hope this helps.

AlexElizard avatar Oct 06 '25 11:10 AlexElizard

Btw., it's totally ok to explicitly document, that Docker Desktop for Linux is not officially supported by tc-python (we have a similar situation for the other languages, also since having DD4L on CI is very hard).

kiview avatar Oct 06 '25 12:10 kiview

I'm not sure this is correct. Docker Desktop is quite widely used. Moreover, according to the documentation the Docker host should be detected automatically (item 6), but this doesn't happen.

AlexElizard avatar Oct 06 '25 23:10 AlexElizard