39

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! :)

Jonathan Petitcolas
  • 4,254
  • 4
  • 31
  • 42

4 Answers4

65

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'
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 Sep 28 '18 at 16:53
  • @brendan I agree, and I have edited the answer accordingly. – VonC Sep 28 '18 at 19:39
  • 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](https://www.postgresql.org/docs/9.3/static/app-psql.html)? – brendan Sep 30 '18 at 13:10
  • @brendan Not sure: can you test it? – VonC Sep 30 '18 at 15:29
  • I wasn't using a Postgres container myself. I can try – brendan Oct 01 '18 at 11:19
  • Behaviour is as I expected: `docker exec -it 43 psql --host=127.0.0.1 --username=postgres sh -c 'foo < /usr/src/app/migrations/*.sql'` `psql: FATAL: database "sh" does not exist` – brendan Oct 04 '18 at 20:57
  • @brendan OK. And without the sh -c? As in https://stackoverflow.com/a/40935552/6309? – VonC Oct 04 '18 at 21:41
  • `docker exec -i 85 psql -U postgres < app_development.back` with `app_development.back` on host machine system containing `\l` lists the databases. https://pastebin.com/JR0wp1RH Not sure what policy is for comments linking to a paste bin. I can delete if required. – brendan Oct 04 '18 at 23:38
  • @brendan I realize the goal of this answer was to call a command with parameters within a docker exec command: so the `sh -c` part should include said command and all its parameters: `sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql` – VonC Oct 05 '18 at 06:24
  • Your latest answer edit makes sense and looks correct to me. Thanks @VonC – brendan Oct 06 '18 at 10:51
  • @brendan Thank you for helping me to fix this answer. – VonC Oct 06 '18 at 12:02
19

try with sh -c "your long command"

user2915097
  • 30,758
  • 6
  • 57
  • 59
3

Also working when piping backup to the mysql command:

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

dahe
  • 806
  • 8
  • 13
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.

Community
  • 1
  • 1
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49