41

I can't figure out how to read content of a file from a Docker container. I want to execute content of a SQL file into my PGSQL container. I tried:

docker exec -it app_pgsql psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql

My application is mounted in /usr/src/app. But I got an error:

bash: /usr/src/app/migrations/*.sql: No such file or directory

It seems that Bash interprets this path as an host path, not a guest one. Indeed, executing the command in two times works perfectly:

docker exec -it app_pgsql
psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql

I think that's more a Bash issue than a Docker one, but I'm still stuck! :)

1
  • Single quote the entire command string. Commented Jul 15, 2015 at 18:24

4 Answers 4

69

Try and use a shell to execute that command

sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'

The full command would be:

docker exec -it app_pgsql sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'
Sign up to request clarification or add additional context in comments.

11 Comments

Maybe I'm missing something, but to me that is not the full command. Surely the command to run something inside an existing running Docker container starts with docker exec?
@brendan I agree, and I have edited the answer accordingly.
Again I could be wrong, this still seems funny to me. The way the command is written now, would sh, -c and the single quoted text not be parameters to psql, interpreted as the Postgres database name and the -c option of psql?
@brendan Not sure: can you test it?
I wasn't using a Postgres container myself. I can try
|
21

try with sh -c "your long command"

3 Comments

A bit late ;) But I agree. I use that anytime pipe or redirection are involved.
Yes you were faster than me :-)
YOU ARE MY SAVIYAAAAH! +1, I was writing to a file inside a docker container, this technique worked. docker exec -t CONTAINER_ID sh -c 'echo "Hello World" > TestFiles/test_user/test_file.txt'
5

Also working when piping backup to the mysql command:

cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE

Comments

1

You can use the database client in order to connect to you container and redirect the database file, then you can perform the restore.

Here is an example with MySQL: a container running MySQL, using the host network stack. Since that the container is using the host network stack (if you don't have any restriction on your MySQL or whatever database), you can connect via localhost and performing the commands transparently

mysql -h 127.0.0.1 -u user -pyour_passwd database_name < db_backup.sql

You can do the same with PostgresSQL (Restore a postgres backup file using the command line?):

pg_restore --host 127.0.0.1 --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"

Seems like that "docker exec" does not support input redirection.. I will verify this and maybe open an issue for Docker Community at GitHub, if it is applicable.

Comments

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.