Y
Y
Yurii Diduk2022-03-25 16:29:03
MySQL
Yurii Diduk, 2022-03-25 16:29:03

How can i execute script.sql in a container???

I have a LAMP project and I'm trying to run it in Docker compose. The application is running but has no connection to mysql. Cause: sql file is not executed and the database is not created. When I enter the container and manually run the sql file, everything then works well.

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 a container??? How to write a Dockerfile correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-03-27
@Glowinthedark

Complicated. It makes no sense to customize the image, just mount the script.sql directory in docker-entrypoint-initdb.d:

volumes: 
    - ./init:/docker-entrypoint-initdb.d

If a database is created from scratch in that script, then you must not forget to create a user and give grants to him and switch to this database using the command use <db_name>(where <db_name>the database is freshly created), and then the actual list of requests. And yes, all this does not happen instantly - it is necessary to provide in the application a check that the database has completely risen, some kind of delay to come up with ...
There is no point in using CMD either - these flags can be passed from docker-compose.yml using the command directive
command:
    - --character-set-server=utf8mb4
    - --collation-server=utf8mb4_unicode_ci

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question