5

I want to write a shell script that enters into a running docker container, edits a specific file and then exits it.

My initial attempt was this -

  1. Create run.sh file.
  2. Paste the following commands into it

    docker exec -it container1 bash
    sed -i -e 's/false/true/g' /opt/data_dir/gs.xml
    exit
    
  3. Run the script -

    bash ./run.sh
    

However, once the script enters into the container1 it lands to the bash terminal of it. Seems like the whole script breaks as soon as I enter into the container, leaving parent container behind which contains the script.

1
  • When/why do you want to do this? Does your image actually need a custom entry point script that can update config files at startup time based on environment variables or other settings? Commented Jul 18, 2018 at 19:54

2 Answers 2

2

The issue is solved By using the below piece of code

myHostName="$(hostname)"
docker exec -i -e VAR=${myHostName} root_reverse-proxy_1 bash <<'EOF'
sed -i -e "s/ServerName .*/ServerName $VAR/" /etc/httpd/conf.d/vhosts.conf
echo -e "\n Updated /etc/httpd/conf.d/vhosts.conf $VAR \n"
exit
Sign up to request clarification or add additional context in comments.

1 Comment

here root_reverse-proxy_1 is the name of the docker image
1

I think you are close. You can try something like:

docker exec container1 sed -i -e 's/false/true/g' /opt/data_dir/gs.xml

Explanations:

  • -it is for interactive session, so you don't need it here.
  • docker can execute any command (like sed). You don't have to run sed via bash

5 Comments

Thanks, but I got this error : OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"sed\": executable file not found in $PATH": unknown
Is sed installed in the image? Use the full path like "/usr/bin/sed"
Ahh!! May be that's the issue. Sed is not installed. Unfortunately, I'm pulling the docker image from somewhere else and therefore won't be able to pre-install it, or install it through shell (may be!). But I get the clue.
Similar things for easy in place replacement: awk and perl. (maybe one of them is installed...)
Thanks a lot. Will look into it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.