1

I'm using the mysql/mysql-server image to create a mysql server in docker. Since I want to setup my database (add users, create tables) automatically, I've created a SQL file that does that for me. In order to automatically run that script, I extended the image with this dockerfile:

FROM mysql/mysql-server:latest

RUN mkdir /scripts
WORKDIR /scripts

COPY ./db_setup.sql .
RUN mysql -u root -p password < cat db_setup.sql

but for some reason, this happens:

/bin/sh: cat: No such file or directory
ERROR: Service 'db' failed to build : The command '/bin/sh -c mysql -u root -p password < cat db_setup.sql' returned a non-zero code: 1

How do I fix this?

Serket
  • 3,785
  • 3
  • 14
  • 45
  • 1
    The `<` redirection operation takes a filename as an argument, not a command. You wouldn't be able to do the redirection above even without using docker. So thinking of this as a docker issue is a red herring. – Bill Karwin Feb 14 '21 at 20:11
  • 1
    You can't `RUN mysql` in any case, because the database server won't be running during the `docker build` phase. Are you looking for something like the `/docker-entrypoint-initdb.d` setup script directory? – David Maze Feb 14 '21 at 22:27
  • @DavidMaze I know... I fixed it by using CMD but I know it’s not the best solution. Could you provide your solution? – Serket Feb 14 '21 at 22:28
  • 1
    The answers to [How can I initialize a MySQL database with schema in a Docker container?](https://stackoverflow.com/questions/29145370/how-can-i-initialize-a-mysql-database-with-schema-in-a-docker-container) (but not the accepted answer) describe the setup pretty well. Anything in that directory when the database container starts up the very first time will be executed, via `mysql` if it's a `*.sql` file. – David Maze Feb 14 '21 at 22:31
  • I updated my answer to incorporate the good info in these comments that address how to run initialization scripts for the mysql image and not just mentioning the source of the original `cat: No such file or directory` error. – Drew MacInnis Feb 15 '21 at 21:12

3 Answers3

2

You can just remove the cat command from your RUN command:

RUN mysql -u root -p password < db_setup.sql

No such file or directory is returned since cat cannot be found in the current directory set by WORKDIR. You can just redirect the stdin of mysql to be from the db_setup.sql file. Edited to clarify < sh redirection is expecting the file name to use for input.

EDIT 2: Keep in mind your example is a RUN command that is attempting to run mysql and creating a layer at docker image build time. You may want to have this run during the mysql entrypoint script at runtime instead (e.g. scripts are run from thedocker-entrypoint-initdb.d/ directory by the docker-entrypoint.sh script of the official mysql image) or using other features that are documented for the official image.

Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18
  • 2
    The cat command isn't necessarily missing from the image, but it's not an SQL file in the current working directory. So it can't be used as the input for the `<` redirection. – Bill Karwin Feb 14 '21 at 20:12
  • Ah, yes, correct, `<` would just be expecting the file name of the file to use for stdin. The `cat` is unnecessary, I'll clarify my answer. – Drew MacInnis Feb 14 '21 at 20:30
1

RUN is a build time command. MySQL isn't running at this point.

If you where/are using a standard image there is a location for database initialization:

FROM mysql:8.0
COPY db_setup.sql /docker-entrypoint-initdb.d
danblack
  • 12,130
  • 2
  • 22
  • 41
0

Command cat is not present in mysql/mysql-server:latest image.

Moreover, you would only need to provide filename afetr redirection.

RUN mysql -u root -p password < db_setup.sql

dataplumber
  • 375
  • 3
  • 16
  • First statement is not established. OP would get the same error even if the command did exist in the image, unless their current working directory is `/use/bin` – Charles Duffy Feb 14 '21 at 20:44
  • Even if the current working directory is not /use/bin, but the location of cat should be added to path – dataplumber Mar 14 '21 at 20:24
  • `< cat` does not search the PATH, it looks for a file named `cat` in the current directory only; so whether it's in the path is irrelevant: The user would get a "file not found" for `< cat` no matter whether or not `/usr/bin/cat` exists (and no matter whether or not `/usr/bin` is in the PATH) as long as `/usr/bin` is not their current working directory. – Charles Duffy Mar 14 '21 at 21:39