1

for this question im working with prisma's dev container: https://github.com/prisma/prisma/tree/main/.devcontainer

once i open that repo inside of a container using the remote container plugin in visual studio and run some Jest Tests that rely on docker services defined in the https://github.com/prisma/prisma/tree/main/docker folder, i get the error of "cant connect to database" for all databases...

it's like if the dev container had no idea those services exist... on my pc, looking at docker desktop i see the services up and running but the devcontainer can't... why?

i find it weird that i had to change any type of setting since this files are from the prisma repo, they are suposed to be ready for action once downloaded... right?

Bandinopla
  • 33
  • 1
  • 1
  • 7
  • How are you specifying what database host you're connecting to? Typically when using non-docker setups you'll use localhost, which you'll need to change to the name of the database container when using a docker setup. – Nick ODell Aug 16 '22 at 19:22
  • from enviroment variables: https://github.com/prisma/prisma/blob/main/packages/migrate/src/__tests__/MigrateDev.test.ts#L1340 – Bandinopla Aug 16 '22 at 20:30
  • Are you using the docker compose file they supply? – Nick ODell Aug 16 '22 at 21:07
  • this is the devcontainer file: https://github.com/prisma/prisma/blob/main/.devcontainer/devcontainer.json Visual Studio Remote plugin picks that up and opens the entire project inside of a container. That works fine. Before that, i went to my local copy of prisma's source folder and manually docker-compose up this https://github.com/prisma/prisma/blob/main/docker/docker-compose.yml again that works fine, i can see all the database services running on my machine... BUT when i try to run a test, inside the Visual Studio Contained project, i get the error "can't connect to XXXX" – Bandinopla Aug 16 '22 at 21:13

1 Answers1

1

Assumming the docker network driver is bridge (Default).

If the script is runing this line to get env in your devcontainer as below.

const connectionString = (
    process.env.TEST_MYSQL_URI_MIGRATE || 'mysql://root:root@localhost:3306/tests-migrate'
  ).replace('tests-migrate', 'tests-migrate-dev')
`

The localhost in the connection string means the localhost in your devcontainer but not your host machine.

You should access the localhost of your host machine instead.

The fix is set the TEST_MYSQL_URI_MIGRATE environment variable instead like

TEST_MYSQL_URI_MIGRATE=mysql://root:root@host.docker.internal:3306/tests-migrate

For the details how to access the localhost of host machine, please read this question

ikhvjs
  • 5,316
  • 2
  • 13
  • 36