简体   繁体   中英

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?

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 (eg scripts are run from the docker-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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM