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:

  • After using Docker-machine to reconfigure docker client how do I set it back to Docker for Windows hyperv
  • Symfony app deployment with docker
  • How to do host-specific customization on docker images
  • How to solve “shell request failed on channel 0” on RHEL container?
  • Communication between two containers on the same host
  • Externalize to docker env
  • 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! 🙂

  • Understanding how the packet flows in the standard docker networking model
  • How do get GoCD server to run persistently in Docker to run in a VM?
  • How to determine bridge IP of docker swarm container
  • How do I mount a virtual machine folder on a docker container? [closed]
  • Bind to docker socket on Windows
  • Docker virtual host cached
  • 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.