Search code examples
shelldockergraphitecd

How to cd, then append to a file from inside a shell script?


Here is a portion of my shell script:

...
docker exec -it graphite bash
cd /opt/graphite/conf
echo >> storage-schemas.conf
echo "[atlas]" >> storage-schemas.conf
echo "pattern = test.atlas" >> storage-schemas.conf
echo "retentions = 1s:15d,10s:45d" >> storage-schemas.conf
...

I want to bash into a running docker container (called graphite), then cd into /opt/graphite/conf then append a few lines of text to a file called storage-schemas.conf. But when I run the above shell script, it creates a new file storage-schemas.conf on my desktop (which is my working directory) and appends to that!

I know I'm not using cd command correctly as this post here explains. But I really need my shell script to not have any dependency, as the end goal is to provide a one-click solution across many teams. Is there a way?


Solution

  • So it turns out that the work-around for this problem was fairly simple. Instead of entering inside the docker container and then trying to append a file, I'm now copying the file from host and pasting(overwriting) it inside the docker container. I can achieve this easily using the docker cp command and I don't have to leave my main bash script (Thanks again Marc for pointing this out).