Docker networking is CRAZY!! (you NEED to learn it)
Why Stack Overflow recommends it
Stack Overflow users link it when answering container connectivity questions, such as reaching the host's localhost or connecting an app container to a database container, and want to explain how Docker networks actually work.
Recommended by 4 people across 4 different months, 2023 to 2024.
What it is
A 39-minute NetworkChuck walkthrough of Docker networking, covering the default bridge network, host mode, the "none" driver, and how containers reach each other. It is a hands-on tutorial aimed at beginners, with a sponsor segment for Bitdefender and a written guide linked from the description.
What people said
Recommends Paul Schwarz · 2023-02 · Stack Overflow · on “From inside of a Docker container, how do I connect to the localhost of the machine?”
I found this video very useful to understand Docker networking and not just copy/paste brute force until it works [this video]
Recommends D Arjona · 2023-03 · Stack Overflow · on “Is it possible to build a virtual network with running docker containers in ONOS SDN?”
Something that needs to be considered is that by default each Docker container has its own IP address, which may be used by the host machine to reach each container and is obtained using the following command: sudo docker inspect <container_name> | grep -i ipaddress Also consider that Docker supports different network configurations. There is a [this video] that explains them.
Recommends Guy · 2023-10 · Stack Overflow · on “How to make express application container connect to mongodb container”
The problem is that the Express is trying to connect MongoDB on the host 127.0.0.1 which refers to the same container. You have several options to solve this, two of them are Change the network to host This will make the containers run on the same network interface as the host, so all the ports are exposed then when the express requests 127.0.0.1 it'll reach MongoDB. sudo docker run -d --network=host --name mongodb mongo sudo docker run -d --network=host --name backend yolobackend:1.0.0 Use docker network DNS service Because both of containers are running in the same network (mynetwork) they can communicate. Docker provides a DNS service the resolves the container's IP based on the container
Recommends zedfoxus · 2024-10 · Stack Overflow · on “How do I install postgis on a postgres container running on docker?”
If you have 2 docker containers (one for app and one for PG), make sure they are in the same network, even if the network is the default network. You can even ping each other easily: [this video] is a great video to watch about docker networking.