Sobes.tech
Middle+

Is it possible to change the DNS settings of a container without restarting via the command line?

sobes.tech AI

Answer from AI

Yes. To make changes to DNS settings without restarting the container, you can edit the /etc/resolv.conf file inside the running container.

It is important to understand that direct editing of this file inside the container is not persistently saved when the container is recreated (for example, when stopped and restarted). For permanent DNS changes, you should use the settings at container or orchestrator startup.

However, for temporary testing or debugging, this is possible.

# Find the ID or name of the required container
docker ps

# Start an interactive shell inside the container
docker exec -it <container-id-or-name> /bin/bash

# Inside the container, edit resolv.conf
# Use a text editor installed inside the container (such as vi, nano, etc.)
vi /etc/resolv.conf

# Example changes: add a new nameserver
# nameserver 8.8.8.8
# nameserver 8.8.4.4

# Save and exit the editor

# Exit the container shell
exit

After these actions, the container will use the new DNS servers for its queries without needing to restart the container.