I started a CockroachDB container with the following:
docker run -d --name=node1 --hostname=node1 \
--network=db -p 26257:26257 -p 8080:8080 \
-v "${PWD}/sql:/cockroach/sql" \
-v "${PWD}/cockroach-data/node1:/cockroach/cockroach-data" \
cockroachdb/cockroach:v2.0.1 start --insecure
The /sql
directory has init.sql
, which is a SQL script I want the database to run, this is the command I'm trying to run:
docker exec node1 \
"/cockroach/cockroach sql --insecure < /cockroach/sql/init.sql"
And this is the error I'm getting:
OCI runtime exec failed: exec failed: container_linux.go:348:
starting container process caused
"exec: \"/cockroach/cockroach sql --insecure < /cockroach/sql/init.sql\":
stat /cockroach/cockroach sql --insecure < /cockroach/sql/init.sql:
no such file or directory": unknown
However, if I docker exec -ti node1 /bin/bash
and run the same command manually, it works.
How can I run cockroach sql --insecure < [my SQL script]
from outside the container?
According to the exec documentation:
COMMAND should be an executable, a chained or a quoted command will not work.
Example:
docker exec -ti my_container "echo a && echo b"
will not work, butdocker exec -ti my_container sh -c "echo a && echo b"
will.
You should try:
docker exec -ti node1 \
sh -c "/cockroach/cockroach sql --insecure < /cockroach/sql/init.sql"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With