Bash / Docker exec: file redirection from inside a container

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= --username=foo foo < /usr/src/app/migrations/*.sql

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

  • Persistent Data when Scaling Postgres with Citus and Docker Compose
  • Why do kubelet failed to get stats from docker (error 500)?
  • Docker - Does the container OS need to be same as Host OS
  • Which application container is better for Docker container?
  • Importing a MySQL database with a Python script fails when the same command works on the command line, what gives?
  • Running GUI apps on docker container with a MacBookPro host
  • 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= --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! 🙂

  • Golang DNS without CGO on a Kubernetes Cluster
  • Docker Nginx stopped: [emerg] 1#1: host not found in upstream
  • How to make Docker application public accessible
  • Docker set up - cannot run hello-world
  • Can Docker Engine start containers in parallel
  • Configure Marathon-LB from docker-compose
  • 3 Solutions collect form web for “Bash / Docker exec: file redirection from inside a container”

    Try and use a shell to execute that command

    sh -c 'foo < /usr/src/app/migrations/*.sql'

    The full command would be:

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

    try with sh -c "your long command"

    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 -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 --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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.