moby: Unable to modify /etc/hosts file in a container

As part of my test setup, the setup script needs to append a line to /etc/hosts

Doing sudo echo "127.0.0.1 foo.bar" >> /etc/hosts results in the following error when executed inside a container

/bin/sh: /etc/hosts: Read-only file system

Googled the error and based on a suggestion from superuser.com issued the command sudo -- sh -c "echo test >> /etc/hosts". But it results in the same error

vi /etc/hosts opens the file in read-only mode. Changing permission doesn’t work as well.

bash-3.2# chmod a+w /etc/hosts
chmod: changing permissions of `/etc/hosts': Read-only file system

Any suggestions?

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 9
  • Comments: 20 (7 by maintainers)

Commits related to this issue

Most upvoted comments

You can do that using in your docker-compose.yml using extra_hosts. For instance:

version: '2'
services:
  daemon:
    build: ./
    extra_hosts:
      - "example.com:127.0.0.1"

–add-host “example.com:127.0.0.1” $ sudo docker run -i -t --add-host "example.com:127.0.0.1" testing/centos /bin/bash

If one wants to modify the default hosts file (not just add a new entry with --add-host docker run flag), it can be done dynamically when for example CMD is executed:

ADD hosts tmp/
CMD cat /tmp/hosts >> /etc/hosts

0.8.1 breaks the dnsmasq workaround because of this commit: https://github.com/alexlarsson/docker/commit/02fddffd51da782f912e2709ea814c330269515b

You will get:

Starting dnsmasq: 
dnsmasq: setting capabilities failed: Operation not permitted
                                                           [FAILED]

To fix add user=root to /etc/dnsmasq.conf See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514214

You can do that using in your docker-compose.yml using extra_hosts. For instance:

version: '2'
services:
  daemon:
    build: ./
    extra_hosts:
      - "example.com:127.0.0.1"

Thanks this worked

I had the same issue and solved it using vi in ex mode.

ex -sc '%s/foo/bar/g|x' /etc/hosts

Hi @creack

I tried to umount and mount /etc/hosts but I’m still getting a Read-only file system error:

$ sudo docker run -i -t -privileged ubuntu /bin/bash

root@27cbc645ba2d:/# mount
none on / type aufs (rw,relatime,si=f3d90fadadd06f3b)
/dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 on /etc/hostname type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 on /etc/hosts type ext4 (ro,relatime,errors=remount-ro,data=ordered)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
/dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 on /.dockerinit type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 on /etc/resolv.conf type ext4 (ro,relatime,errors=remount-ro,data=ordered)
devpts on /dev/tty1 type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=666)
devpts on /dev/ptmx type devpts (rw,relatime,mode=600,ptmxmode=666)
root@27cbc645ba2d:/# 
root@27cbc645ba2d:/# umount /dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092
root@27cbc645ba2d:/# mount -t ext4 -o rw,remount -force /dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 /etc/hosts
root@27cbc645ba2d:/# echo testing >> /etc/hosts
bash: /etc/hosts: Read-only file system
root@27cbc645ba2d:/# mount
root@27cbc645ba2d:/# 

If I just unmount:

root@9a8c52e9ab77:/# umount /dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092
root@9a8c52e9ab77:/etc# echo testing >> /etc/hosts 
bash: /etc/hosts: Read-only file system
root@9a8c52e9ab77:/# mount
root@9a8c52e9ab77:/#

A mount variation:

root@837cf29cb7b8:/# umount /dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092
root@837cf29cb7b8:/# mount -t ext4 -o rw -force /dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 /etc/hosts
root@837cf29cb7b8:/# echo testing >> /etc/hosts
bash: /etc/hosts: Read-only file system
root@837cf29cb7b8:/# 
root@837cf29cb7b8:/# mount
/dev/disk/by-uuid/06091d80-7a94-4104-9bc5-58c21b343092 on /etc/hosts type ext4 (rw,rce)
root@837cf29cb7b8:/# 

Any idea how to modify /etc/hosts?