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:

  • How to clear properly the logs of docker container?
  • Is docker stateless?
  • dumb terminal in non-interactive docker
  • Dockerized .net core app doesn't load on Azure
  • How to set an environment variable in a running docker container
  • Docker random ports & AWS Elastic Load Balancer
  • 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! 🙂

  • Cannot run my docker image
  • how to bind specific interface in docker container?
  • can't access own directory in docker created by ADD/chown/chmod in Dockerfile
  • Docker or Docker for AWS and Deployment on AWS EC2
  • compiling opencv on alpine docker
  • Docker Swarm Mode: Not all VIPs for a service work. Getting timeouts for several VIPs
  • 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.