‹ All Docker Interview Questions questions

Docker Interview Question 6: Container DNS User Network

Docker Interview Questions · Test 1 · question 6 of 10

Networkingmedium

You start two containers on one Linux host with docker run -d --name db postgres:16 and docker run -d --name api myapi, with no --network flag. The API is configured to connect to host db on port 5432, but its logs show the name db cannot be resolved. Both containers are running. What is the recommended fix so that api reaches the database by the name db?

Select one answer
Show answer and explanation

Answer:

  • A. Create a network with docker network create appnet and run both containers with --network appnet

Without --network, containers join the default bridge network, where they can reach each other only by IP address (unless you use the legacy --link option). User-defined bridge networks provide automatic DNS resolution between containers: on appnet, api can resolve db by its container name.

Option B publishes the port to the host. That makes it reachable from outside the host's network, but it adds no name resolution between containers; db still does not resolve.

Option C is documentation: EXPOSE does not publish anything and does not create DNS entries.

Option D takes api out of Docker's container networking altogether. With host networking it uses the host's resolver, which knows nothing about the container name db, and the database is still on the default bridge.

Reference: Bridge network driver

Practice Test 1 ›

Study guides for Docker Interview Questions