Serket
Serket

Reputation: 4485

Docker container: /bin/sh: cat: No such file or directory

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?

Upvotes: 1

Views: 2684

Answers (3)

Drew MacInnis
Drew MacInnis

Reputation: 8587

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.

Upvotes: 2

danblack
danblack

Reputation: 14736

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

Upvotes: 1

dataplumber
dataplumber

Reputation: 417

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

Upvotes: 0

Related Questions