Docker
Beginner
1 min read
Container DNS and Network Aliases
Example
# Create a network and start three app instances with the same alias
docker network create lb-net
for i in 1 2 3; do
docker run -d \
--name app$i \
--network lb-net \
--network-alias app \
-e INSTANCE=$i \
myapp:latest
done
# Requests to "app:3000" round-robin across the three containers
docker run --rm --network lb-net curlimages/curl \
sh -c 'for i in 1 2 3 4 5 6; do curl -s app:3000/instance; echo; done'
# Inspect DNS — shows multiple A records
docker run --rm --network lb-net alpine nslookup app
# Custom hostname and extra hosts
docker run -d \
--name myapi \
--network app-net \
--hostname api-server \
--add-host external.svc:1.2.3.4 \
myapp:latest
docker exec myapi hostname
docker exec myapi cat /etc/hosts
# Clean up
docker stop $(docker ps -q)
docker network rm lb-net
Related Resources
Docker Reference
Complete tag & property list
Docker How-To Guides
Step-by-step practical guides
Docker Exercises
Practice what you've learned
More in Docker