0

I have a mysql docker container and it is up and running. I can get into it and see the mysql prompt. I do not want to mount external storage on it. Everytime I start this container, I want to execute a test.sql script from my file system to create DB and do few such actions. When I run the script which exists in my current working directory, it complaints. I know this is trivial but I am unable to catch the issue.

/microservices/mysqlcontainer]docker exec 3a21e5e3669d /bin/sh -c 'mysql -u root -ppwd <./test.sql' /bin/sh: ./test.sql: No such file or directory

2
  • Is test.sql inside the image/container? docker exec 3a21e5e3669d ls -l test.sql Commented Jan 30, 2017 at 21:28
  • no it is outside and i also guess it could be the problem but some forums suggested this syntax as working. I have been able to connect to this container and get mysql prompt easily though like this docker exec -it mysqltest mysql -u root -ppwd Commented Jan 30, 2017 at 22:06

1 Answer 1

5

Since your script is outside the container, there's no need to catch the shell input redirection. You should be able to run the following:

docker exec -i 3a21e5e3669d mysql -u root -ppwd <./test.sql
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, i do understand the linux concepts of redirection, pipes, stdout, etc. However, could you elaborate a little bit more on "no need to catch...". Your suggestion did work btw.
The /bin/sh -c ' mysql .... <input.txt' is used to run the redirect inside the container's shell, sending the input to the mysql command. Without quotes, that redirect gets picked up by your shell and input is sent to the docker command which gets sent to the command you're running inside of docker when you have the -i flag (updating my questions now since I forgot that flag).
thank you! Have been trying to docker-ize my microservices that i am prototyping using spring boot+netflix libraries. This helps. I needed sql container to hold authentication info for json web tokens. I can move ahead now as my auth service can talk to the DB and everytime i boot up the mysql container, the db is created with the script. I will probably read on my own how to mount an external file sys but that is not a priority for me right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.