I am trying to build a mysql container using a docker file and an SQL file to create the tables but I keep getting this error in my terminal:> [3/3] RUN /docker-entrypoint-initdb.d/books.sql: #7 0.686 /docker-entrypoint-initdb.d/books.sql: line 1: syntax error near unexpected token
`('
'7 0.686 /docker-entrypoint-initdb.d/books.sql: line 1: ``CREATE TABLE "articulos" (`
where it says I have a "`" in from of CREATE and I don't why that's showing up.
here is my docker file
FROM mysql:latest
ENV MYSQL_DATABASE books
COPY ./sqlscript /docker-entrypoint-initdb.d/
RUN /docker-entrypoint-initdb.d/books.sql
and this is my SQL file
CREATE TABLE "articulos" (
"id" int NOT NULL AUTO_INCREMENT,
"titulo" text,
"contenido" text,
"fechacreacion" date DEFAULT NULL,
"categoria" text,
PRIMARY KEY ("id")
) ENGINE=InnoDB AUTO_INCREMENT=143 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Thank you in advance.
CodePudding user response:
The RUN
keyword is to execute a command. The SQL schema file isn't an executable. If you look at the documentation for the MySQL container, it should automatically load all schema files located in /docker-entrypoint-initdb.d
when it first starts. So you really just need the following:
FROM mysql:latest
ENV MYSQL_DATABASE books
COPY ./books.sql /docker-entrypoint-initdb.d/
...and make sure your SQL file is named books.sql
in the current working directory. (Not sure what sqlscript
is in your original question).
CodePudding user response:
You could be facing two problems:
- Docker is trying to run your
.sql
file as if it was abash
script - And anyway, your MySQL server might not be running when building
Try removing your last Dockerfile line (RUN /docker-entrypoint-initdb.d/books.sql
).
It might work like you want it to since I presume that .sql
files placed in the /docker-entrypoint-initdb.d/
directory are executed on startup.