Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a SQL script in CockroachDB inside a Docker container?

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?

like image 954
sargas Avatar asked May 08 '18 21:05

sargas


1 Answers

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, but docker 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"
like image 80
Simone Zabberoni Avatar answered Oct 22 '22 20:10

Simone Zabberoni