Home > Back-end >  How to replace path in file in docker container with perl or sed
How to replace path in file in docker container with perl or sed

Time:02-11

I am working on automation script that can set some parameters in docker container. I want to replace path path.repo: /var/backups with path.repo: /var/log/elasticsearch in yaml file in container so I use commands below:

old_path=/var/backups
new_path=/var/log/elasticsearch

docker exec elk sed -i '0,/${old_path}/s//${new_path}/' /etc/elasticsearch/elasticsearch.yml 

or

docker exec elk sed -i -E 's/^([^ ]*\s )(\/var\/backups)(\s.*)/\1\2\/var\/log\/elasticsearch\3/' /etc/elasticsearch/elasticsearch.yml

None of them is working. When I log in docker container and opening the yml file, it still remains same. Am I doing something wrong maybe with docker commands? I also tried:

perl -i -p -e's{"/var/backups"}{/var/log/elasticsearch' /etc/elasticsearch/elasticsearch.yml

Thanks in advance

CodePudding user response:

A problem with your first attempt is that setting variables in your local shell doesn't expose them inside your docker container (and using single quotes around your arguments prevents your local shell from expanding them)...and even if you were to substitute the variable values in that expression, the / characters in your path would muck with your sed command. The simplest solution here is probably something like this:

docker exec elk sed -i 's|/var/backups|/var/log/elasticsearch|' /etc/elasticsearch/elasticsearch.yml 

Note the use of | instead of / as the sed expression delimiter to avoid conflicts with the path separators. That only works with the s command, so if you really need to limit the scope of those changes with a search expression you'll need something like:

docker exec elk sed -i '0,/\/var\/backups/ s|/var/backups|/var/log/elasticsearch|' /etc/elasticsearch/elasticsearch.yml 

That's pretty much the equivalent of your first example.

Having said that, editing content inside a running container is generally considered an anti-pattern: your best course of action is almost always to generate a new image with the changes baked in by using an appropriate Dockerfile.

  • Related