Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exec sed command to a docker container

Tags:

bash

docker

sed

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.

like image 791
Erick Gallani Avatar asked Mar 12 '23 06:03

Erick Gallani


1 Answers

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.

like image 147
nnsense Avatar answered Mar 15 '23 23:03

nnsense