Not initialized when using mysql with docker

Viewed 16

I'm creating a Rest API mini-application using Docker, flask, and mysql.I use docker-compose.yaml to build and run.I use init.sql to initialize MySQL, but after running it several times, it no longer initializes. Furthermore, the data that has been used so far always remains and I cannot refer to the data.

How can I create new data here?

Condition:

  • Volume has been removed
  • To execute, use docker-compose up -d after running docker-compose build.
  • Connect to the db server and check the table contents.

My code:

version: '3'
services: 
  api:
      build: python
      container_name: api_server
      ports:
        - "5000:5000"
      tty: yes
      environment:
        TZ: Asia/Tokyo
        FLASK_APP: app.py
      depends_on:
        - db
      networks:
        - app_net
  db:  
    build: mysql
    container_name: db_server2
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: testdb
      TZ: Asia/Tokyo
    volumes:
      - ./db-data/:/var/lib/mysql
    command: mysqld
    networks:
      - app_net
volumes:
  db-data:
networks:
  app_net:
    driver: bridge

When I change MYSQL_DATABASE: testdb to MYSQL_DATABASE: testdb2 and run it, no data is created. (The previous data is still there.)

In addition, no volume is created.

Any advice would be appreciated.

1 Answers

The initialization script will only run once if there are no DB files at all (empty db-data folder). As you wrote, you do have a testdb database so it won't run again for testdb2.

Related