0

I cannot figure out how to fully start ubuntu/mysql, prepare the db, execute tests, and exit the container. Here is my docker file:

FROM ubuntu/mysql:latest
COPY my_db_scripts.sql /docker-entrypoint-initdb.d/
COPY my_test.sh /tests
WORKDIR /tests

I am running it with the following command:

docker run --rm -it -p 3306:3306 -e MYSQL_ROOT_PASSWORD=cool my_tests:1.0.0

The my_db_scripts.sql completes successfully. However, if I add my_tests.sh as an ENTRYPOINT or CMD at the end of the Dockerfile, the test scripts execute before mysql listener starts. If I add my_tests.sh to /docker-entrypoint-initdb.d/, the script executes after the temp mysql instance stops, as quoted in the documentation:

All of this is done before the MySQL service is started. Keep in mind if your database directory is not empty (contains pre-existing database) they will be left untouched.

How can I wait for mysql to start, execute my_tests.sh, and stop the container? ( i.e my test scripts will return error or no error based on results )

Links I used to try different things:

Executing SQL scripts on docker container

How do I know when my docker mysql container is up and mysql is ready for taking queries?

How to add a startup script to a mysql docker container?

angryip
  • 2,140
  • 5
  • 33
  • 67
  • Is there a reason you need to run the tests from inside the database? Can you start an unmodified database container, then run the tests from outside the container? – David Maze Nov 03 '22 at 10:31
  • the container will be configured with many other utilities that i would rather not put on the host. ( the tests depend on them ) i look at the container as a replicated dev env that is decoupled from the one executing it ( me, jenkins , etc ) – angryip Nov 03 '22 at 11:10
  • I'd still set this up as two containers, one running the database proper (an unmodified `mysql` container) and a second running the tests (from your CI pipeline, with the image-of-tools). – David Maze Nov 03 '22 at 11:20
  • gotcha, thanks for the suggestion. for the time being, i'd like to experiment with this approach. – angryip Nov 03 '22 at 12:01

0 Answers0