How to Access Docker Containers? IP Address or Container Name?
Introduction
This post covers various scenarios for accessing Docker containers, exploring whether to use the container’s IP address or name in each case. While this information might be trivial for seasoned Docker users, I often find myself forgetting these basic concepts, wasting a lot of time. Hence, I decided to write a blog post in the form of a cheat sheet. Most of the content here is generated by ChatGPT.
In general, just remember these two points:
- Although we prefer to access containers by their names, this DNS service is provided by Docker and can only be used by containers.
- For port mapping
-p 8080:80
: Use the container’s port80
when accessing the container via container name/IP, and use the host’s port8080
when accessing from the host.