atdatabases icon indicating copy to clipboard operation
atdatabases copied to clipboard

Support external docker

Open twavv opened this issue 5 years ago • 4 comments

I'm running on GitLab and trying to use Docker to run a PG database for testing.

In GitLab, CI pipelines are executed inside of Docker containers (rather than within dedicated VMs as is the case with Travis-CI) so you can't start a nested docker daemon. However, they do support launching a "sidecar" docker service at tcp://docker:2375. This means that all started containers aren't available at localhost, but rather, at the docker host.

twavv avatar May 18 '20 21:05 twavv

@databases/with-container (1.0.0 → 1.1.0)

New Features

  • Support specifying the host

    In some CI environments, servers started by docker are not available at localhost, but are accessible from a different host. This allows manually overriding the host for testing the connection.

Packages With No Changes

The following packages have no user facing changes, so won't be released:

  • @databases/expo
  • @databases/mysql
  • @databases/mysql-config
  • @databases/mysql-test
  • @databases/pg
  • @databases/pg-config
  • @databases/pg-create
  • @databases/pg-data-type-id
  • @databases/pg-errors
  • @databases/pg-migrations
  • @databases/pg-schema
  • @databases/pg-test
  • @databases/push-to-async-iterable
  • @databases/sql
  • @databases/sqlite
  • @databases/websql
  • @databases/websql-core

Edit changelogs

rollingversions[bot] avatar May 18 '20 21:05 rollingversions[bot]

Another thing to do might be to take the URL automatically (if not provided) from the DOCKER_HOST environment variable. If it looks like tcp://<ip address>:2375 then we should use <ip address> to connect to the database (and preferably return postgres://user@<ip address>/db from other places like in pg-test).

twavv avatar May 18 '20 21:05 twavv

Thanks @travigd I think the changes you're suggesting sound good. Would you be willing to add those to the PR? The logic would need duplicating between pg-test and mysql-test for now, but that should be ok as it'll only be a few lines to:

  1. check if DOCKER_HOST is specified
  2. parse it with new URL(...)
  3. extract and pass through the .host

One other question, do you know if there are any environments where DOCKER_HOST would be specified but we should still connect to localhost? I'm not aware of any myself.

ForbesLindesay avatar May 18 '20 23:05 ForbesLindesay

I believe this should also fix https://github.com/ForbesLindesay/atdatabases/issues/89 . Since there is only one docker daemon in a docker-in-docker setup, if a test is running in a container, pg-test will start another container on the host machine. The only way to connect to the container from the test is if the host is configurable to be set as host.docker.internal instead of localhost.

I think this line should also be updated https://github.com/ForbesLindesay/atdatabases/blob/9dc5a1d59f38f53c98b810949a3db01fc5783f23/packages/with-container/src/index.ts#L159 to

rawOptions.externalPort || (await detectPort({
  host: options.host,
  port: defaultExternalPort
})); 

do you know if there are any environments where DOCKER_HOST would be specified but we should still connect to localhost?

@ForbesLindesay I think it would be best to handle parsing of DOCKER_HOST externally and not couple it with this project. Simply making the host configurable should make this library flexible enough.

addnab avatar Sep 25 '20 07:09 addnab