r/ProgrammerTIL • u/cdrini • Apr 06 '22
Other Language [Docker] TIL How to run multi-line/piped commands in a docker container without entering the container!
I would regularly need to run commands like:
docker run --rm -it postgres:13 bash
#-- inside the container --
apt-get update && apt-get install wget
wget 'SOME_URL' -O - | tar xf -
Well, I just learned, thanks to copilot, that I can do this!
docker run --rm postgres:13 bash -c "
apt-get update && apt-get install wget
wget 'SOME_URL' -O - | tar xf -
"
That's going to make writing documentation soooo much simpler! This isn't really a docker feature, it's just a bash argument I forgot about, but it's going to be super helpful in the docker context!
Also useful because I can now use piping like normal, and do things like:
docker-compose exec web bash -c "echo 'hello' > /some_file.txt"
83
Upvotes
8
u/GreenFox1505 Apr 06 '22 edited Apr 07 '22
If you think that's neat, wait till you see "here documents"
https://tldp.org/LDP/abs/html/here-docs.html
4
u/wineblood Apr 06 '22
Nice. Although I'd expect to see something like
apt-get updateto be in your dockerfile.11
u/NoStepOnDingus Apr 06 '22
This is super useful, TIL!
5
u/thelamestofall Apr 06 '22
That's not really a docker thing, just a bash thing