Docker-compose exec using stdin as an input
Coding with his cats
Posted on June 22, 2020
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 |
.
💖 💪 🙅 🚩
Coding with his cats
Posted on June 22, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.