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

Containers not spinning up inside VSCode Container with latest Docker for Mac #283

Closed
zsiegel opened this issue Oct 11, 2021 · 22 comments
Closed
Labels
bug Something isn't working

Comments

@zsiegel
Copy link

zsiegel commented Oct 11, 2021

Hello

We have a number of projects that utilize VS Code running in containers. We mount the host docker socket into the container so it can access the Docker for Mac instance that is running on the host machine for our tests.

Up until the most recent version of Docker for Mac everything was going well.

Currently this no longer works on Docker for Mac 4.1.0 (69386) - Docker version 20.10.8, build 3967b7d. I am not exactly sure what the issue is so this might need to be reported elsewhere but I figured I would start here.

I have created a reproducible project here for testing if it helps.

From what I can see the ryuk container spins up but then none of the actual test containers specified run. Here is the log output with all the debug output turned on.

root@d8c15eedf186:/home/node/workspace# DEBUG=testcontainers* npm test 

> testcontainers-node-example@1.0.0 test
> jest --coverage

  testcontainers DEBUG Creating new Docker instance +0ms
  testcontainers DEBUG No registry auth locator found for registry: "https://index.docker.io/v1/" +319ms
  testcontainers DEBUG Not pulling image as it already exists: postgres:12 +15ms
  testcontainers DEBUG Creating new Reaper for session: eda8e4abc7c9c5ccd8e2dc1761ce3e51 +0ms
  testcontainers DEBUG Re-using cached auth for registry https://index.docker.io/v1/ +0ms
  testcontainers INFO  Docker host strategy INSIDE_CONTAINER_IPAM: 172.17.0.1 +3ms
  testcontainers WARN  Unable to detect docker-compose version, is it installed? Error: spawn docker-compose ENOENT +17ms
  testcontainers DEBUG System diagnostics: {
  testcontainers   "node": {
  testcontainers     "version": "v16.10.0",
  testcontainers     "architecture": "arm64",
  testcontainers     "platform": "linux"
  testcontainers   },
  testcontainers   "docker": {
  testcontainers     "serverVersion": "20.10.8",
  testcontainers     "operatingSystem": "Docker Desktop",
  testcontainers     "operatingSystemType": "linux",
  testcontainers     "architecture": "aarch64",
  testcontainers     "cpus": 4,
  testcontainers     "memory": 4124495872
  testcontainers   }
  testcontainers } +0ms
  testcontainers DEBUG Not pulling image as it already exists: testcontainers/ryuk:0.3.2 +1ms
  testcontainers INFO  Creating container for image: testcontainers/ryuk:0.3.2 +2ms
  testcontainers INFO  Starting container testcontainers/ryuk:0.3.2 with ID: 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +103ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Pinging Docker... +0ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Docker daemon is available! +0ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Starting on port 8080... +0ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Started! +0ms
  testcontainers DEBUG Waiting for container to be ready: 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +246ms
  testcontainers DEBUG Waiting for host port 44248 for 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +3ms
  testcontainers DEBUG Waiting for internal port 8080 for 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +1ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 New client connected: 127.0.0.1:40055 +161ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 EOF +1ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Client disconnected: 127.0.0.1:40055 +0ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:42 Received the first connection +1ms
  testcontainers DEBUG Internal port 8080 ready for 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +211ms
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:52 Timed out waiting for re-connection +10s
  testcontainers:containers TRACE 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: 2021/10/11 19:41:52 Removed 0 container(s), 0 network(s), 0 volume(s) 0 image(s) +1ms
 FAIL  tests/postgres-container.test.ts (25.726 s)
  ● PostgresContainer › postgres container started

    thrown: "Exceeded timeout of 15000 ms for a hook.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      4 |     let container: StartedTestContainer;
      5 |   
    > 6 |     beforeAll(async () => {
        |     ^
      7 |         container = await new GenericContainer("postgres:12").start();
      8 |     }, 15000);
      9 |   

      at tests/postgres-container.test.ts:6:5
      at Object.<anonymous> (tests/postgres-container.test.ts:3:1)


  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'stop')

      10 |     afterAll(async () => {
      11 |         await new Promise(resolve => setTimeout(resolve, 5000));
    > 12 |         await container.stop();
         |                         ^
      13 |     }, 15000);
      14 |
      15 |     it("postgres container started", async () => {

      at tests/postgres-container.test.ts:12:25
      at fulfilled (tests/postgres-container.test.ts:5:58)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        26.464 s
Ran all test suites.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
  testcontainers ERROR Container failed to be ready: Error: Port 44248 not bound after 60000ms for 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5 +60s
  testcontainers ERROR Failed to stop container 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5: Error: (HTTP code 404) no such container - No such container: 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5  +52ms
  testcontainers ERROR Failed to stop container after it failed to be ready: Error: (HTTP code 404) no such container - No such container: 5a5f8c31c8d5434b62d80959b804b38f4c59c06417843e04d529abd25a097ab5  +0ms

Please let me know if there is anything else I can provide or if maybe you have some more answers as to where this would be best reported? I feel like I have not been able to get to the root of whatever is causing this issue. Thanks!

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Oct 12, 2021

Hi @zsiegel, thanks for raising such a detailed issue! I confirm I can reproduce the issue. I upgraded to Docker for Mac 4.0.1, Docker version 20.10.8 and had no issues. But upgrading from Docker Desktop 4.0.1 to 4.1.1 I have an issue.

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Oct 12, 2021

Can also be reproduced by cloning this repo and running:

docker run -it \
  -v $(pwd):/opt/testcontainers-node \
  -v /var/run/docker.sock:/var/run/docker.sock \
  node:16-bullseye sh -c "cd /opt/testcontainers-node && npm test -- generic-container -t 'should stop the container when the host port check wait strategy times out'"

First thing I notice is that the host port isn't being bound.

@cristianrgreco
Copy link
Collaborator

Seems the Java project has also run into the issue: testcontainers/testcontainers-java#4395

@cristianrgreco
Copy link
Collaborator

@zsiegel As a temporary workaround, I suspect that using a different wait strategy would work.

@zsiegel
Copy link
Author

zsiegel commented Oct 14, 2021

I tried a few different wait strategies and none of them worked. From watching the docker logs it appears these containers don't even start :(

I will keep an eye on the issue in docker/for-mac as I suspect that needs fixed.

@cristianrgreco
Copy link
Collaborator

@zsiegel you won't be able to change the wait strategy of the ryuk container, so you'd need to disable it by setting this env variable: TESTCONTAINERS_RYUK_DISABLED=true, then set the wait strategy on your postgres container

@zsiegel
Copy link
Author

zsiegel commented Oct 15, 2021

hmmm so I gave this a try as well and I still get nothing. If I watch the docker logs and do watch -n 2 docker ps I never see any new containers spin up. Is there any more info on what disabling this RYUK container does?

From what I can see when running in this wormhole situation test containers cant spin anything up at all.

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Dec 9, 2021

@zsiegel FYI, this PR changes the way we map host ports, I believe it will fix this issue so stay tuned 🙂

@zsiegel
Copy link
Author

zsiegel commented Dec 10, 2021

@cristianrgreco amazing thank you. I will keep an eye out so I can test this as soon as it drops.

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Dec 10, 2021

@zsiegel could you please try v8.1.0?

@zsiegel
Copy link
Author

zsiegel commented Dec 11, 2021

I gave this another try and sadly no luck!

I am now on Docker for Mac 4.3.0.

Output of test run on sample project within devcontainer setup is.

root@5271bba281dd:/home/node/workspace# npm test

> testcontainers-node-example@1.0.0 test
> jest --coverage

 FAIL  tests/postgres-container.test.ts (23.067 s)
  ● PostgresContainer › postgres container started

    thrown: "Exceeded timeout of 15000 ms for a hook.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      4 |     let container: StartedTestContainer;
      5 |   
    > 6 |     beforeAll(async () => {
        |     ^
      7 |         container = await new GenericContainer("postgres:12")
      8 |           .start();
      9 |           await new Promise(resolve => setTimeout(resolve, 5000));

      at tests/postgres-container.test.ts:6:5
      at Object.<anonymous> (tests/postgres-container.test.ts:3:1)


  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'stop')

      12 |     afterAll(async () => {
      13 |         await new Promise(resolve => setTimeout(resolve, 5000));
    > 14 |         await container.stop();
         |                         ^
      15 |     }, 15000);
      16 |
      17 |     it("postgres container started", async () => {

      at tests/postgres-container.test.ts:14:25
      at fulfilled (tests/postgres-container.test.ts:5:58)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        23.594 s, estimated 24 s
Ran all test suites.

When I run this locally it works fine.

@cristianrgreco
Copy link
Collaborator

@zsiegel thanks for trying. Could you please run the test with DEBUG=testcontainers*? The test timeout is only 15s, maybe it timed out pulling the image?

@zsiegel
Copy link
Author

zsiegel commented Dec 11, 2021

Here are the logs.

root@fe7ce51cdd1f:/home/node/workspace# DEBUG=testcontainers* npm test

> testcontainers-node-example@1.0.0 test
> jest --coverage

  testcontainers DEBUG Found applicable Docker client strategy: UnixSocketStrategy +0ms
  testcontainers DEBUG Testing Docker client strategy URI: unix:///var/run/docker.sock +0ms
  testcontainers DEBUG No registry auth locator found for registry: "https://index.docker.io/v1/" +119ms
  testcontainers INFO  Using Docker client strategy: UnixSocketStrategy, Docker host: 172.17.0.1 +10ms
  testcontainers DEBUG Not pulling image as it already exists: postgres:12 +5ms
  testcontainers DEBUG Creating new Reaper for session: 3dc69756fb2d9967446b8ddf10d33cc7 +0ms
  testcontainers DEBUG Re-using cached auth for registry https://index.docker.io/v1/ +0ms
  testcontainers DEBUG Not pulling image as it already exists: testcontainers/ryuk:0.3.2 +4ms
  testcontainers INFO  Creating container for image: testcontainers/ryuk:0.3.2 +1ms
  testcontainers WARN  Unable to detect docker-compose version, is it installed? Error: spawn docker-compose ENOENT +9ms
  testcontainers DEBUG System diagnostics: {
  testcontainers   "node": {
  testcontainers     "version": "v16.13.1",
  testcontainers     "architecture": "arm64",
  testcontainers     "platform": "linux"
  testcontainers   },
  testcontainers   "docker": {
  testcontainers     "serverVersion": "20.10.11",
  testcontainers     "operatingSystem": "Docker Desktop",
  testcontainers     "operatingSystemType": "linux",
  testcontainers     "architecture": "aarch64",
  testcontainers     "cpus": 5,
  testcontainers     "memory": 10418831360
  testcontainers   }
  testcontainers } +0ms
  testcontainers INFO  Starting container testcontainers/ryuk:0.3.2 with ID: 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962 +479ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:42 Pinging Docker... +0ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:42 Docker daemon is available! +1ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:42 Starting on port 8080... +0ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:42 Started! +0ms
  testcontainers DEBUG Waiting for container to be ready: 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962 +981ms
  testcontainers DEBUG Waiting for host port 55054 for 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962 +0ms
  testcontainers DEBUG Waiting for internal port 8080 for 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962 +1ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:43 New client connected: 127.0.0.1:42531 +149ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:43 EOF +0ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:43 Client disconnected: 127.0.0.1:42531 +0ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:43 Received the first connection +1ms
  testcontainers DEBUG Internal port 8080 ready for 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962 +157ms
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:53 Timed out waiting for re-connection +10s
  testcontainers:containers TRACE 6503a055bcdea8a3a2a1811f429a22e81409f65ec5935a22afcc7307a8042962: 2021/12/11 16:27:53 Removed 0 container(s), 0 network(s), 0 volume(s) 0 image(s) +0ms
 FAIL  tests/postgres-container.test.ts (31.793 s)
  ● PostgresContainer › postgres container started

    thrown: "Exceeded timeout of 30000 ms for a hook.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

       9 |     })
      10 |
    > 11 |     beforeAll(async () => {
         |     ^
      12 |         container = await new GenericContainer("postgres:12")
      13 |           .start();
      14 |     }, 30000);

      at tests/postgres-container.test.ts:11:5
      at Object.<anonymous> (tests/postgres-container.test.ts:4:1)


  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'stop')

      15 |   
      16 |     afterAll(async () => {
    > 17 |         await container.stop();
         |                         ^
      18 |     }, 30000);
      19 |
      20 |     it("postgres container started", async () => {

      at tests/postgres-container.test.ts:17:25
      at tests/postgres-container.test.ts:8:71
      at Object.<anonymous>.__awaiter (tests/postgres-container.test.ts:4:12)
      at tests/postgres-container.test.ts:16:25

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        32.13 s, estimated 34 s
Ran all test suites.

I also captured the logs from the Ryuk container.

~ ❯ docker logs 6503a055bcde --follow
2021/12/11 16:27:42 Pinging Docker...
2021/12/11 16:27:42 Docker daemon is available!
2021/12/11 16:27:42 Starting on port 8080...
2021/12/11 16:27:42 Started!
2021/12/11 16:27:43 New client connected: 127.0.0.1:42531
2021/12/11 16:27:43 EOF
2021/12/11 16:27:43 Client disconnected: 127.0.0.1:42531
2021/12/11 16:27:43 Received the first connection
2021/12/11 16:27:53 Timed out waiting for re-connection
2021/12/11 16:27:53 Removed 0 container(s), 0 network(s), 0 volume(s) 0 image(s)

@cristianrgreco cristianrgreco added the bug Something isn't working label Jan 19, 2022
@cristianrgreco
Copy link
Collaborator

Hi @zsiegel, apologies for the delay!

I've tried this again using testcontainers 8.6.1, Docker Desktop for Mac 4.6.1, and with disabling ryuk it works:

root@cfb02120cc9f:/home/node/workspace# TESTCONTAINERS_RYUK_DISABLED=true DEBUG=testcontainers* npm test

> testcontainers-node-example@1.0.0 test
> jest --coverage

  testcontainers DEBUG Found applicable Docker client strategy: UnixSocketStrategy +0ms
  testcontainers DEBUG Testing Docker client strategy URI: unix:///var/run/docker.sock +206ms
  testcontainers DEBUG No registry auth locator found for registry: "https://index.docker.io/v1/" +4ms
  testcontainers INFO  Using Docker client strategy: UnixSocketStrategy, Docker host: 172.17.0.1 +13ms
  testcontainers DEBUG Not pulling image as it already exists: postgres:12 +11ms
  testcontainers DEBUG Not creating new Reaper for session: c3f4b713c20e008383c608e4e73c9b00 +0ms
  testcontainers INFO  Creating container for image: postgres:12 +1ms
  testcontainers WARN  Unable to detect docker-compose version, is it installed? Error: spawn docker-compose ENOENT +15ms
  testcontainers DEBUG System diagnostics: {"node":{"version":"v16.13.1","architecture":"arm64","platform":"linux"},"docker":{"serverVersion":"20.10.13","operatingSystem":"Docker Desktop","operatingSystemType":"linux","architecture":"aarch64","cpus":4,"memory":4124368896}} +1ms
  testcontainers INFO  Starting container postgres:12 with ID: 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0 +32ms
  testcontainers DEBUG Waiting for container to be ready: 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0 +193ms
  testcontainers INFO  Container is ready +0ms
  testcontainers INFO  Stopping container with ID: 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0 +5ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: Error: Database is uninitialized and superuser password is not specified. +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: You must specify POSTGRES_PASSWORD to a non-empty value for the +1ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0:  +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all +1ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: connections without a password. This is *not* recommended. +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0:  +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: See PostgreSQL documentation about "trust": +0ms
  testcontainers:containers TRACE 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0: https://www.postgresql.org/docs/current/auth-trust.html +0ms
  testcontainers INFO  Container has already been stopped: 47553733e22208137c0900c630118629a6c6293dab87b0d5c623ea936f2739b0 +96ms
 PASS  tests/postgres-container.test.ts
  PostgresContainer
    ✓ postgres container started (2 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.642 s, estimated 34 s
Ran all test suites.

I will look into why ryuk is not working in this setup, but in the meantime this is a workaround.

@cristianrgreco
Copy link
Collaborator

cristianrgreco commented Apr 4, 2022

@zsiegel So this one took me several hours to find the root cause, thanks also for your help @kiview!

When you run docker-compose, it by default creates a new bridge network. Testcontainers doesn't know about this and creates containers in the default bridge network. Containers cannot talk to each other across different networks, so for example when your VS code container tries to connect via TCP to a container in the default bridge network (this is how the resource reaper works), it times out.

You can simply tell Docker to use the default bridge network, update your docker-compose.yml as follows, note that you need the network_mode statement for every service:

services:
  app:
    ...
    network_mode: bridge

@zsiegel
Copy link
Author

zsiegel commented Apr 4, 2022

@cristianrgreco this makes a ton of sense. I assume their might be an option to specify the network test containers uses?

or does the usage of test containers require this bridge network implicitly?

thanks!

@ivandotv
Copy link

Could docker-compose define a network and then testcontainers and ryk attach to that network?

@sae-artem
Copy link

@cristianrgreco

Adding network_mode: bridge to services in docker-compose stops docker-compose from creating new bridge network, instead it places the containers to default bridge network. While it helps with testcontainers, it may break DNS resolution of services inside the docker-compose itself, which was there before without network_mode: bridge

From docker documentation:

Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.

What would be nice is to be able to attach testcontainers to an existing network by it's name. So flow would be like this:

  1. Define a new bridge network in docker-compose.yaml
  2. Define in testcontainers setup something like .withNetwork(getNetworkByName(networkName))

@cristianrgreco
Copy link
Collaborator

Define in testcontainers setup something like .withNetwork(getNetworkByName(networkName))

@sae-artem This is already possible is it not? .withNetworkMode(networkName)

@sae-artem
Copy link

sae-artem commented Jul 11, 2023

I tried it like that.

I have a docker-compose.yaml where I have something like this:

services:
  serviceA:
    ...
    networks:
      - local_bridge
networks:
  local_bridge:
    driver: bridge

Then in my testcontainers'setup

const startMocks: () => Promise<MockContainer[]> = async (): Promise<MockContainer[]> => {
  return Promise.all(
    mockContainers.map(async (mockContainer: MockContainer) => {
      mockContainer.container = await new ConfigurableGenericContainer('...-utility/mocks')
        .withExposedPorts(mocksPort)
        .withStartupTimeout(startupTimeout)
        .withCommand([mockContainer.yaml])
        .withNetworkMode("local_bridge")
        .start()

      return mockContainer
    })
  )
}

In my setup, I do docker-compose up first (outside testcontainer) and then starting testcontainers' code above. I'm getting
...globalSetup.ts, reason: (HTTP code 404) no such container - network local_bridge not found

Maybe I didn't get how the .withNetworkMode(networkName) is supposed to work, @cristianrgreco

@sae-artem
Copy link

The error above was caused by the fact, that Docker Compose created the local_bridge network with the name docker_local_bridge network (because yml is located in .docker folder). Once I changed the testcontainers setup code to .withNetworkMode("docker_local_bridge") - the error about network not found dissapeared. But I got the next error:

Error: Jest: Got error running globalSetup - /home/node/app/__integration-tests__/globalSetup.ts, reason: Port 33086 not bound after 60000ms
    at /home/node/app/node_modules/testcontainers/dist/src/wait-strategy/host-port-wait-strategy.js:53:23
    at IntervalRetryStrategy.<anonymous> (/home/node/app/node_modules/testcontainers/dist/src/retry-strategy.js:37:28)
    at Generator.next (<anonymous>)
    at fulfilled (/home/node/app/node_modules/testcontainers/dist/src/retry-strategy.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

So it seems like again testcontainers can't connect to my service from my docker-compose anyway.

@sae-artem
Copy link

My bad, it was the other testcontainer giving this error, for which I didn't specify the network. Now it all works.

So to summarize, you can join an existing network .withNetworkMode(nameOfTheNetwork)

Thanks for help @cristianrgreco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants