How can you configure DNS resolution for Docker containers?
The DNS resolution of Docker containers can be configured in three ways. By default, Docker sets an internal DNS server for containers in a custom network, so that containers can be parsed with each other through service names; if you need to customize, you can use the --dns flag to specify the DNS server when running the container; for the Docker Compose environment, you can configure the dns and dns_search parameters in the docker-compose.yml file to achieve more granular control; if you need global unified configuration, you can edit the Docker daemon configuration file /etc/docker/daemon.json and restart the Docker service to make the configuration take effect. Each method is suitable for different scenarios and can be used in combination as needed.
Configuring DNS resolution for Docker containers is a common need, especially when you're dealing with microservices or custom networks. The good news is that it's not overly complicated once you understand the moving parts.
How Docker Handles DNS by Default
By default, Docker sets up its own internal DNS server for containers connected to user-defined networks. This allows containers to resolve each other by service name — which is super handy for things like connecting a web app container to a database container without hardcoding IPs.
For example, if you have a service named db , another container can reach it just by using db as the hostname.
But sometimes, the default setup isn't enough — maybe you want to use a specific DNS server, or you need custom domain names for local development.
Option 1: Set Custom DNS in docker run
If you're starting a container manually using docker run , you can specify custom DNS servers using the --dns flag:
docker run --dns=8.8.8.8 --dns=8.8.4.4 my-container
This tells Docker to use Google's public DNS servers instead of whatever is configured by default. You can also set this globally in the Docker daemon config if you want it to apply to all containers.
Note: If your network blocks certain DNS servers (like corporate networks), this might not work as expected. Test and adjust accordingly.
Option 2: Use docker-compose.yml for More Control
Most modern Docker settings use Docker Compose, which gives you a cleaner way to define DNS settings:
services:
app:
image: my-app
dns:
- 192.168.1.10
- 8.8.8.8You can even add search domains so that short hostnames get resolved correctly:
dns_search: - mydomain.com - dev.mydomain.com
This means a container trying to reach api will attempt to resolve api.mydomain.com automatically.
Option 3: Customize the Docker Daemon
If you want consistent DNS behavior across all containers, editing the Docker daemon config ( /etc/docker/daemon.json ) is the way to go:
{
"dns": ["10.0.0.2", "8.8.8.8"]
}After updating, restart Docker:
sudo systemctl restart docker
Now every new container will use these DNS servers unless overridden at runtime or in the compose file.
This is useful in environments where all containers should route through a central DNS resolver — like in production clusters or private networks.
Basically, how you configure DNS depends on your use case: one-off containers, compose-based services, or global defaults. Each approach has its place, and they can be mixed depending on your needs.
The above is the detailed content of How can you configure DNS resolution for Docker containers?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How does Docker differ from traditional virtualization?
Jul 08, 2025 am 12:03 AM
The main difference between Docker and traditional virtualization lies in the processing and resource usage of the operating system layer. 1. Docker containers share the host OS kernel, which is lighter, faster startup, and more resource efficiency; 2. Each instance of a traditional VM runs a full OS, occupying more space and resources; 3. The container usually starts in a few seconds, and the VM may take several minutes; 4. The container depends on namespace and cgroups to achieve isolation, while the VM obtains stronger isolation through hypervisor simulation hardware; 5. Docker has better portability, ensuring that applications run consistently in different environments, suitable for microservices and cloud environment deployment.
How to troubleshoot Docker issues
Jul 07, 2025 am 12:29 AM
When encountering Docker problems, you should first locate the problem, which is problems such as image construction, container operation or network configuration, and then follow the steps to check. 1. Check the container log (dockerlogs or docker-composelogs) to obtain error information; 2. Check the container status (dockerps) and resource usage (dockerstats) to determine whether there is an exception due to insufficient memory or port problems; 3. Enter the inside of the container (dockerexec) to verify the path, permissions and dependencies; 4. Review whether there are configuration errors in the Dockerfile and compose files, such as environment variable spelling or volume mount path problems, and recommend that cleanbuild avoid cache dryness
How to set environment variables in PHP environment Description of adding PHP running environment variables
Jul 25, 2025 pm 08:33 PM
There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en
How do you specify environment variables in a Docker container?
Jun 28, 2025 am 12:22 AM
There are three common ways to set environment variables in a Docker container: use the -e flag, define ENV instructions in a Dockerfile, or manage them through DockerCompose. 1. Adding the -e flag when using dockerrun can directly pass variables, which is suitable for temporary testing or CI/CD integration; 2. Using ENV in Dockerfile to set default values, which is suitable for fixed variables that are not often changed, but is not suitable for distinguishing different environment configurations; 3. DockerCompose can define variables through environment blocks or .env files, which is more conducive to development collaboration and configuration separation, and supports variable replacement. Choose the right method according to project needs or use multiple methods in combination
How do you expose a port from a Docker container to the host machine?
Jul 12, 2025 am 01:33 AM
To expose Docker container ports, the host needs to access the container service through port mapping. 1. Use the dockerrun-p[host_port]:[container_port] command to run the container, such as dockerrun-p8080:3000my-web-app; 2. Use the EXPOSE instruction to mark the purpose in the Dockerfile, such as EXPOSE3000, but the port will not be automatically published; 3. Configure the ports segment of the yml file in DockerCompose, such as ports:-"8080:3000"; 4. Use dockerps to check whether the port map is generated after running.
How do you create a Docker volume?
Jun 28, 2025 am 12:51 AM
A common way to create a Docker volume is to use the dockervolumecreate command and specify the volume name. The steps include: 1. Create a named volume using dockervolume-createmy-volume; 2. Mount the volume to the container through dockerrun-vmy-volume:/path/in/container; 3. Verify the volume using dockervolumels and clean useless volumes with dockervolumeprune. In addition, anonymous volume or binding mount can be selected. The former automatically generates an ID by Docker, and the latter maps the host directory directly to the container. Note that volumes are only valid locally, and external storage solutions are required across nodes.
How to run PHP in Docker?
Jun 27, 2025 am 12:09 AM
When running PHP, you need to pay attention to the environment configuration and container stability when running Docker. First, prepare a PHP project with a clear structure, ensure that there are dependent files such as composer.json, and place the code in a separate directory for mounting; second, use the official PHP image to quickly start container testing, such as using the CLI image to execute simple scripts; then write a custom Dockerfile image, copy the code, install the extensions, and enable the necessary modules; finally handle debugging and common problems, including permissions, missing dependencies, Apache operation and log viewing. It is recommended to build a custom image and optimize the configuration when deploying and launching it online.
How to debug inside a Docker container with VSCode?
Jul 10, 2025 pm 12:40 PM
The key to debugging code with VSCode in Docker containers is to configure the development environment and connection methods. 1. Prepare a mirror with development tools, install necessary dependencies such as debugpy or node, and use the official devcontainers image to simplify configuration; 2. Mount the source code and enable the Remote-Containers plug-in, create .devcontainer folders and configuration files, and realize in-container development; 3. Configure the debugger, add debug settings for the corresponding language in launch.json, and enable the listening port in the code; 4. Solve common problems, such as exposing the debug port, ensuring the host is 0.0.0.0, and use postCreateC


