raquelhortab
raquelhortab

Reputation: 556

docker exec -> permission denied when executing through ssh

I am trying to execute a command on a docker container that is running in a remote server. I am running most commands through ssh and they all work correctly. However, this command modifies the file /etc/environment and I get a "permission denied" error.

The command in question is docker exec container_id echo 'WDS_SOCKET_PORT=XXXXX' >> /etc/environment

I tried adding the option -u 0 to the docker exec command with no luck.

I don't mind making changes to the Dockerfile since I can kill, remove or recreate this container with no problem.

Upvotes: 1

Views: 1713

Answers (1)

BMitch
BMitch

Reputation: 263569

The error isn't coming from docker or ssh, it's coming from your shell that parses the command you want to run. You are trying to modify the file on your host. To do io redirection inside the container, you need to run a shell there and parse the command with that shell.

ssh user@ip "docker exec  container_id /bin/sh -c 'echo \"WDS_SOCKET_PORT=XXXXX\" >> /etc/environment'"

EDIT: Note that the whole docker command should be surrounded by quotes. I believe this is because ssh might otherwise parse different parts of the command as parameters of the docker command. This way, each sub-command is clearly delimited.

Upvotes: 1

Related Questions