5

I'm trying to change a config file that is inside a docker container.

docker exec container_name sed -ire '/URL_BASE = /c\api.myapiurl' tmp/config.ini 

Executing this sed command locally works just fine, but when I try to execute this in the container I receive the following error message.

sed: cannot rename tmp/config.ini: Operation not permitted

What I need to do is replace the 'URL_BASE =' from the 'config.ini' before deploy the container to my server.

I don't know why the sed command is trying to rename the file when its not suppose to.

Any ideas?

What I've tried

I tried to execute with the --privileged flag, but didn't worked. I tried to change the file permissions with chmod but I couldn't for the same reason of permission.

docker exec --privileged container_name sed -ire '/URL_BASE = /c\api.myapiurl' tmp/config.ini 

Result: sed: cannot rename tmp/config.ini: Operation not permitted

Chmod

docker exec --privileged  container_name chmod 755 tmp/config.ini 

Result: chmod: changing permissions of 'tmp/config.ini': Operation not permitted

I also have tried execute with sudo before docker but didn't work either.

2
  • Is this the correct command to replace your string? You can verify it when manually executing it: docker run -it <image> bash and then execute sed <...>
    – n2o
    Aug 30, 2016 at 21:10
  • Sed inplace works by creating a copy of the file and then replacing it with the original. Your problem is that you created the file inside the container with admin privileges, but in the later part shifted to another user. So, whenever you try to perform exec, you are typing those command as non-admin user. Show us your Dockerfile. You can verify this by typing: docker exec container_name whoami and docker exec container_name ls -l tmp/config.ini Aug 31, 2016 at 5:09

2 Answers 2

7

Nehal is absolutely right, sed works creating a local file so you just need a different approach, which is commonly used on Linux: heredocs.

Taking just the first lines from the documentation, a here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program.

It can help us with docker exec as follows:

docker exec -i container_name bash <<EOF
sed -ire '/URL_BASE = /c\api.myapiurl' /tmp/config.ini
grep URL_BASE /tmp/config.ini
# any other command you like
EOF

Be aware of the -t, which is commonly used running bash, because it allocates a pseudo-TTY, and we don't really need that. Also, to be safe always use absolute paths like /tmp/config.ini.

0
docker exec -i <container name> sed -i 's/xxx/${yyy}/g' path/filename.yaml

This is working for me.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.