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=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:

  • Docker-compose failed running a jar that read configuration from a file
  • Docker mysql_connect(): Can't connect
  • Disable certain Docker run options
  • docker on OSX not returning container output through ssh,
  • build Docker images from GitLab
  • NCurses in Docker Container
  • 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! 🙂

  • cant start SonarQube docker container via Marathon with port mapping
  • Docker containers with one public IP
  • Docker communication to hardware on host
  • Hex Cannot Be Found on Dockerized Phoenix App when running on Drone
  • How to config apache (installed in server) to run a web app (django) in docker?
  • Can't execute boot2docker command on TeamCity command line build step
  • 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=127.0.0.1 --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 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.

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