Docker-compose exec using stdin as an input

codewithcats

Coding with his cats

Posted on June 22, 2020

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 |.

💖 💪 🙅 🚩
codewithcats
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.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024