Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash / Docker exec: file redirection from inside a container

Tags:

bash

docker

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

like image 437
Jonathan Petitcolas Avatar asked Jul 15 '15 18:07

Jonathan Petitcolas


People also ask

Which command can exec commands inside docker container?

Description. The docker exec command runs a new command in a running container. The command started using docker exec only runs while the container's primary process ( PID 1 ) is running, and it is not restarted if the container is restarted. COMMAND will run in the default directory of the container.

How do I run a command in a docker container?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.

How do you exec into a container?

Follow these steps: Use docker ps to get the name of the existing container. Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container. Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.


2 Answers

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' 
like image 137
VonC Avatar answered Oct 05 '22 13:10

VonC


try with sh -c "your long command"

like image 31
user2915097 Avatar answered Oct 05 '22 14:10

user2915097