1

I have a docker-compose.yml file with the following:

 populated_db:
        container_name: populated_db
        restart: always
        image: mysql
        volumes:
            - ./test_database.sql:/docker-entrypoint-initdb.d/a.sql
            - populated_test_db:/var/lib/mysql
        ports:
            - "33061:3306"
        environment:
            MYSQL_RANDOM_ROOT_PASSWORD: "true"
            MYSQL_DATABASE: aaaa
            MYSQL_USER: aaaa
            MYSQL_PASSWORD: aaaa
db:
    container_name: db
    image: mysql
    ports:
        - "33060:3306"
    volumes:
        - type: volume
          source: populated_test_db
          target: /var/lib/mysql
          volume:
            nocopy: false
    restart: always
    environment:
        MYSQL_RANDOM_ROOT_PASSWORD: "true"
        MYSQL_DATABASE: aaaa
        MYSQL_USER: aaaa
        MYSQL_PASSWORD: aaaa
    depends_on:
        - populated_db

volumes:
    populated_test_db:

The populated_db container successfully starts and populates the database with the contents of test_database.sql. However, once it has done that, the db container starts and tries to use populated_test_db as its /var/lib/mysql, causing the following error:

Unable to lock ./ibdata1 error: 11
Check that you do not already have another mysqld process using the same InnoDB data or log files.

Presumably this is because the populated_db container is still running. How can I stop it running once it has populated the volume, so the db container can use the populated volume?

Birdie
  • 274
  • 2
  • 14
  • This might help https://stackoverflow.com/questions/29145370/how-can-i-initialize-a-mysql-database-with-schema-in-a-docker-container – ThatGuyInIT Sep 30 '18 at 23:19
  • @ThatGuyInIT that explains how to populate a Docker MySQL container, but I can already do that. – Birdie Sep 30 '18 at 23:39
  • You could add an exit to your populate script which would cause the populate container to stop; however, you would probably also need to check for the pid file from the other container before attempting to start MySQL as both containers are going to be started at the same time. Seems like a lot of kludge though and anytime I am facing a lot of kludge to make something work, I usually take a second look at my approach and see if there might be something I am missing. – ThatGuyInIT Oct 01 '18 at 00:07
  • 2
    How about this? It populates mysql during the container build. https://github.com/lindycoder/prepopulated-mysql-container-example – ThatGuyInIT Oct 01 '18 at 01:01
  • @ThatGuyInIT that looks exactly like what I was looking for, thanks! – Birdie Oct 01 '18 at 01:04

0 Answers0