Moby: docker exec not able to update the /etc/hosts file in a running container

Created on 16 Aug 2015  路  3Comments  路  Source: moby/moby

docker version is 1.7.0, build 0baf609

I am trying to edit the /etc/hosts file of a running container by using the command:
docker exec <container-id> echo <ip> <server> >> /etc/hosts

And, I get the permission denied error message:
bash: /etc/hosts: Permission denied

However, when I run the following command:
docker exec -it <container-id> /bin/sh

and then manually edit the /etc/hosts file after attaching into that container, it works fine.

Any idea what I am missing here?

Most helpful comment

Apologies, I found the answer
docker exec <> /bin/sh -c "echo <> <> >> /etc/hosts"

All 3 comments

docker info
Containers: 2
Images: 105
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 118
Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.18.0-kali3-amd64
Operating System: Kali GNU/Linux 1.1.0 (moto)
CPUs: 4
Total Memory: 9.79 GiB
Name: kali
ID: URLU:WG5F:RJAB:4WHX:JFPW:N3UR:G644:K7EI:QPER:J66Z:3XGY:FW6Q
WARNING: No memory limit support
WARNING: No swap limit support

uname -a
Linux kali 3.18.0-kali3-amd64 #1 SMP Debian 3.18.6-1~kali2 (2015-03-02) x86_64 GNU/Linux

docker version
Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 0baf609
OS/Arch (client): linux/amd64
Server version: 1.7.0
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 0baf609
OS/Arch (server): linux/amd64

Apologies, I found the answer
docker exec <> /bin/sh -c "echo <> <> >> /etc/hosts"

Yup. Also see https://github.com/docker/docker/issues/15604#issuecomment-131360708 for those looking for an explanation :smile:

Was this page helpful?
0 / 5 - 0 ratings