DEV Community

Coding with his cats
Coding with his cats

Posted on

Docker-compose exec using stdin as an input

Sometimes you want to execute command inside docker-compose service using input from the host machine. Instead of copy content from host machine to inside the container and run the command, you can use docker-compose exec with stdin as an input.

Example scenario: Execute external SQL files inside the container

You have backup .sql file on your host machine and want to restore it into your Postgresql container. You can run:

cat /path/to/sql/file \
  | docker-compose exec -T service_name \
  psql -U username -d database_name

Option: -T does the trick. By default, exec command of docker-compose allocates pseudo-tty. With -T, it will not -- which means it expects to do one-time command execution so that we can pipe the content of our SQL file in with |.

Top comments (2)

Collapse
 
asieduernest profile image
malleable

thank you

Collapse
 
enderandpeter profile image
Spencer Williams

Congratulations! You've won!