DEV Community

Coding with his cats
Coding with his cats

Posted on

7 3

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
asieduernest profile image
malleable

thank you

Collapse
 
enderandpeter profile image
Spencer Williams

Congratulations! You've won!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay