0

I have a minimal example mysql library.

seed.sql:

create schema MyDatabase;
use MyDatabase;
create table MyTable(MyNumber int);
insert into MyTable values (70);

Dockerfile:

FROM mysql
COPY *.sql /docker-entrypoint-initdb.d/

Part of my docker-compose.yml:

my-database:
  build: my-database
  environment:
    - MYSQL_ROOT_PASSWORD

Now if I change 70 to 71 in insert into MyTable values (70); and run docker compose up --build -d my-database, I'd expect a new image to be created with the new seed file.

But instead, the old value of 70 is still in the database (I connect directly using mysql cli to check). I need to run docker compose rm my-database first for my seed.sql changes to take effect.

My question is why is that? Shouldn't changes to the seed.sql file cause docker to create a brand new image for my-database, which would force it to completely relaunch the service?

Another similar question - Scripts in the /docker-entrypoint-initdb.d folder are ignored

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    Does this answer your question? [MySQL scripts in docker-entrypoint-initdb are not executed](https://stackoverflow.com/questions/38504257/mysql-scripts-in-docker-entrypoint-initdb-are-not-executed) – Nico Haase May 30 '23 at 07:54
  • Does this answer your question? [How to get docker-compose to always re-create containers from fresh images?](https://stackoverflow.com/questions/32612650/how-to-get-docker-compose-to-always-re-create-containers-from-fresh-images) – Justinas May 30 '23 at 07:55
  • In your output, do you see `COPY *.sql /docker-entrypoint-initdb.d/` being re-run or is it cached and this layer skipped by Docker? – Justinas May 30 '23 at 07:56
  • None of the above 2 questions answer WHY this is the case. WHY doesn't a change to the seed.sql file create a brand new image? – sashoalm May 30 '23 at 07:56
  • @Justinas - if I make changes to it, how can it be skipped? It would invalidate the caching. – sashoalm May 30 '23 at 07:57
  • @sashoalm I have seen when some tools does not update file mtime, so cache depending on that metadata is not cleared – Justinas May 30 '23 at 08:02
  • 2
    The data is in an (anonymous) volume, inserted by the base `mysql` image, and since there's already a database the initialization scripts aren't re-run. For most applications I might prefer using your application framework's database-migration system to doing something Docker-specific. – David Maze May 30 '23 at 09:28
  • @DavidMaze - thank you, that gets closest to answering my actual question so far. So the initdb scripts see the volume and just don't run the sql. But wouldn't the changes to my file build a new docker image? Or do you mean to say that the same anonymous volume gets reused even if the docker image SHA1 has changed? – sashoalm May 30 '23 at 11:18

0 Answers0