How to prevent copied /etc/resolv.conf from being overwritten inside an Execution Environment?

I’m trying to copy my own /etc/resolv.conf inside an Execution Environment but it is always overwritten somehow.
How can we prevent that from happening?

No matter in which section I put RUN chmod 444 /etc/resolv.conf, it always fails with the same eroor message Read-only file system;

Running ansible-builder build -vvv --container-runtime=docker --context="/ee/context" --file="/ee/files/definition/file.yml" --tag="tag:version" -v3 with this definition file:

version: 3

dependencies:
  ansible_core:
    # Require minimum of 2.15 to get ansible-inventory --limit option
    package_pip: ansible-core<2.19
  ansible_runner:
    package_pip: ansible-runner
  galaxy:
    collections:
      - name: ansible.mysql
        ...
  python_interpreter:
    package_system: python311
    python_path: /usr/bin/python3.11
  python:
    - wheel
      ...

additional_build_steps:
  append_base:
    - RUN $PYCMD -m pip install -U pip
    - RUN dnf -y install dnf-plugins-core
    - RUN dnf -y install epel-release epel-next-release
    - RUN dnf -y config-manager --set-enabled crb
    - RUN mkdir -p /etc/ansible
  append_final:
    - COPY --from=quay.io/ansible/receptor:devel /usr/bin/receptor /usr/bin/receptor
    - RUN mkdir -p /var/run/receptor
    - RUN git lfs install --system
    - RUN alternatives --install /usr/bin/python python /usr/bin/python3.11 311
    - COPY ./etc/ansible/ansible.cfg /etc/ansible/
    - COPY ./etc/resolv.conf /etc/
    - COPY ./root/.gitconfig /root/
    - RUN chmod 644 /etc/ansible/ansible.cfg
    - RUN chmod 444 /etc/resolv.conf
    - RUN chmod 600 /root/.gitconfig

I get:

...
#47 [final 17/19] RUN chmod 444 /etc/resolv.conf
#47 0.164 chmod: changing permissions of '/etc/resolv.conf': Read-only file system
#47 ERROR: process "/bin/sh -c chmod 444 /etc/resolv.conf" did not complete successfully: exit code: 1

ansible-builder ee

hello,
2 things : copy —chmod=444 might be more appropriate instead of running an additionnal RUN command.
That being said, i think there’s a mechanism in docker/podman where the resolv.conf file is anyway overriden dynamically at run time (—dns and —dns_search options to customize); i saw somebody using a CMD option where first a custom resolv.conf was copied over /etc/resolv.conf, but it looked dirty , and it wasn’t in the context of an execution environment, might work though :blush:

While chmod or setfacl can help, it rarely does with that file since anything writing to it is doing so with root permissions. I would use the file action and set the the file system attribute to ‘immutable’:

ansible -m file -a 'path=/etc/resolve.conf attributes="+i"' targethost

What @johan said. /etc/hosts and /etc/resolv.conf are files managed by docker/podman and you can’t really enforce the content of these files. You have to use --dns* and --add-host docker command line options.

There is one idea though. You can try mounting your custom file in container using -v /your/resolv.conf:/etc/resolv.conf. I vaguely recall that I tried to do this with /etc/hosts but I can’t recall if it worked or not.

Warm thank to you all for your valuable input.

I believe a bit of context is required.

This ee is designed for AWX inside a docker container which uses podman to run it.
Each time a playbook (template) is run within AWX, this custom ee (if properly setup with the template) is launched as an environment for the playbook, So the playbook uses the DNS servers defined in the ee /etc/resolv.conf.

The ee default resolv.conf uses hardcoded Google’s DNS servers and AFAIK, they are not easily configurable, hence this thread.

My iontent is twofold:

  1. Use encrypted DNS (which with the use of ECH enables fully encrypted communications)
  2. Use privately defined DNS hostnames requiring the use of a private DNS server

@bcoca

So that’s why your proposal cannot be implemented.

@johan

I already tried that, it’s useless as it is not applied. A default 664 is applied no matter what.

Here is what I tried:

...
  append_final:
    ...
    - COPY ./etc/ansible/ansible.cfg /etc/ansible/
    - COPY ./root/.gitconfig /root/
    - ADD ./etc/resolv.conf /etc/resolv.conf.new
    - CMD cp -f /etc/resolv.conf.new /etc/resolv.conf

But it does not change anything. The official Dockerfile reference and Best Practices say that CMD can be overridden by docker run arguments (or in my case podman run) and I suspect it is what’s happening behind the scene.

@bvitnik

It’s an interesting out of the box idea, although a little long to implement in my context. I would have to:

  1. entirely rebuild my awx_devel image to copy a temporary resolv.conf.ee file (the awx container resolv.conf contains a different setup using its own internal resolver at 127.0.0.11)
  2. reinstall AWX with the new awx_devel image and the new ee
  3. change AWX settings to define the exposed path /etc/resolv.conf → resolv.conf.ee

But before I try this, I’ve had my own simpler ideas:

  1. maybe resolv.conf is locked inside the ee resulting in the “Read-only file system” error message. So a solution might be to unlock it first (after installing the proper system package containing chattr), then copy my own and lock it back:
  append_final:
    ...
    - COPY ./etc/ansible/ansible.cfg /etc/ansible/
    - COPY ./root/.gitconfig /root/
    - RUN chattr -i /etc/resolv.conf
    - COPY ./etc/resolv.conf /etc/
    - RUN chattr +i /etc/resolv.conf

leads to:

...
#43 [final 13/17] COPY ./etc/ansible/ansible.cfg /etc/ansible/
#43 DONE 0.0s

#44 [final 14/17] COPY ./root/.gitconfig /root/
#44 DONE 0.0s

#45 [final 15/17] RUN chattr -i /etc/resolv.conf
#45 0.134 chattr: Read-only file system while setting flags on /etc/resolv.conf
#45 ERROR: process "/bin/sh -c chattr -i /etc/resolv.conf" did not complete successfully: exit code: 1
------
 > [final 15/17] RUN chattr -i /etc/resolv.conf:
0.134 chattr: Read-only file system while setting flags on /etc/resolv.conf
  1. pass dns option directly in the CMD
  append_final:
    ...
    - COPY ./etc/ansible/ansible.cfg /etc/ansible/
    - COPY ./root/.gitconfig /root/
    - CMD ["--dns 172.19.0.1","--dns_search example.com"]

The ee build succeeds except for this:

 1 warning found (use docker --debug to expand):
 - MultipleInstructionsDisallowed: Multiple CMD instructions should not be used in the same stage because only the last one will be used (line 103)

I’m already using ansible-builder max verbosity -vvv and I don’t know what it means by docker --debug.
Since there is only one CMD instruction in my definition file, it shows that ansible-builder adds its own and overrides my own, which is confirmed when running a simple ad-hoc setup inside AWX.

So, the last option is to try @bvitnik 's idea.
I’ll let you know when I have time to implement and test this.

I think this should be:

- CMD --dns 172.19.0.1 --dns_search example.com