skip to Main Content

I have this docker compose file:

version: "2.4"

services:
  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=mypasswd
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - 8080:80
    environment:
      - PMA_HOST=mysql
    depends_on:
      mysql:
        condition: service_healthy

  app:
    stdin_open: true
    tty: true
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - ./src:/usr/app/src
    depends_on:
      mysql:
        condition: service_healthy

The app service is just a node image running some tests with jest. The CMD of that image is jest --watchAll

I would like it to be interactive and respond to my key presses, but I cannot get it to work. This is the output I get when I spin up the containers with docker-compose up:

 PASS  src/test.test.ts
  Can connect to the database
    ✓ Can connect to the database (1 ms)
app_1         | 
Test Suites: 1 passed, 1 total
app_1         | Tests:       1 passed, 1 total
app_1         | Snapshots:   0 total
app_1         | Time:        0.314 s
app_1         | Ran all test suites.
app_1         | 
app_1         | Watch Usage
app_1         |  › Press f to run only failed tests.
app_1         |  › Press o to only run tests related to changed files.
app_1         |  › Press p to filter by a filename regex pattern.
app_1         |  › Press t to filter by a test name regex pattern.
app_1         |  › Press q to quit watch mode.
app_1         |  › Press Enter to trigger a test run.
aaaaaaaaaaaaaaaffffffffooooooo





ppppp
p

As you can see, it’s ignoring my key presses, and just appends the letters to the output.

2

Answers


  1. what you expect is nonsense. this output is just for showing you the logs of your services and by docker-compose up: -d command this no longer showing for you.
    for interact with you service you must dive into your container.

    docker exec -it [CONTAINER_NAME] bash
    
    Login or Signup to reply.
  2. You can run your test suite from the host, connecting to a database running in Docker.

    You need to add ports: to your database container to make it accessible from outside Docker:

    services:
      mysql:
        ports:
          # The first number can be any unused port on your host.
          # The second number MUST be the standard MySQL port 3306.
          - '3306:3306'
    

    You don’t show how you configure your application to connect to the database, but you will need to set something like MYSQL_HOST=127.0.0.1 (required; the standard MySQL libraries misinterpret localhost to mean "a Unix socket") and MYSQL_PORT=3306 (with the first number from ports:, optional that is the default port 3306 and required otherwise).

    Once you’ve done this, you can run your tests:

    # Start the database, but not the application
    docker-compose up -d mysql
    
    # Run the tests from the host, outside of Docker
    npx jest --watchAll
    

    This last command is a totally normal test-runner invocation. You do not need to do anything to cause source code to sync with the test runner or to pass keypresses through, because you are actually running your local source code with a local process.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search