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
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.
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: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 misinterpretlocalhost
to mean "a Unix socket") andMYSQL_PORT=3306
(with the first number fromports:
, optional that is the default port 3306 and required otherwise).Once you’ve done this, you can run your tests:
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.