5

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?

sargas
  • 5,820
  • 7
  • 50
  • 69

1 Answers1

5

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"
Simone Zabberoni
  • 2,024
  • 1
  • 10
  • 15
  • It works. Thank you for pointing out the documentation. I totally missed the one caveat the command has. – sargas May 11 '18 at 00:50