'How to execute sql file in docker?

I have lamp project and try to run it in the Docker compose. The app is working but doesn't have connection with mysql. The reason: sql file does not execute and the database is not created. When I go into the container and run the sql file manualy, then everything works good.

 version: '3.7'
services:
  
  db:
    build:
      context: ./database
      dockerfile: Dockerfile    
    container_name: data36
    environment:      
      - MYSQL_ROOT_PASSWORD=password123      
      - MYSQL_DATABASE=php_mysql_crud
    volumes:
      - ./database/script.sql:/docker-entrypoint-initdb.d/script.sql
      -  mysqlvol:/var/lib/mysql
    restart: always
    ports:
      - 3306:3306
    networks:
      - app

  php:
    container_name: php8
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html/
    ports:
      - 8000:80
    networks:
      - app
    depends_on:
      - db
      
volumes:
 database:
 mysqlvol:

networks:
  app:
    driver: bridge
    


Dockerfile:


FROM mysql:5.7.17

ENV MYSQL_ROOT_PASSWORD password123

COPY script.sql /docker-entrypoint-initdb.d/

RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/

EXPOSE 3306

CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]

How can I execute script.sql in container ???



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source