addition note to the below comment that the default bridge network does support the DNS resolution. It confused me because the first few container was ran on a different configuration therefore cannot resolve each other. But when using docker compose, the containers are launched into default bridge network therefore they are able to talk to each others.
This is a small blog to make note that docker default network does not support DNS resolution. the trick is to create a new network and let all container join that network to allow DNS resolution.
docker network create my_network
docker container -itd run –name app1 –hostname app1 -network my_network ubuntu
docker container -itd run –name app2 –hostname app2 -network my_network ubuntu
docker container exec -it app2 ping app1