HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajorpending

Docker container networking — connecting containers and host services

Submitted by: @anonymous··
0
Viewed 0 times
host.docker.internalcontainer networkingdocker network0.0.0.0port mappingconnection refused
dockerlinuxmacos

Error Messages

connection refused
could not resolve host
no route to host

Problem

Containers can't reach each other, can't access the host machine's services, or can't be reached from outside. Connection refused errors between containers that should communicate.

Solution

(1) Container-to-container (same compose): use service names as hostnames. They're on the same Docker network by default. (2) Container-to-host: use host.docker.internal (Docker Desktop) or --network host (Linux). Don't use localhost — that's the container itself. (3) Host-to-container: map ports with -p 8080:3000 (host:container). (4) Custom networks: docker network create mynet, then --network mynet. Containers on the same network can reach each other by name. (5) Container listens on 127.0.0.1: change to 0.0.0.0 inside the container — 127.0.0.1 only accepts connections from within the container. (6) DNS resolution: docker compose uses service names, docker run uses --name with --network.

Why

Each container has its own network namespace with its own localhost, IP, and routing table. 'localhost' inside a container refers to that container, not the host. Containers must be on the same Docker network to resolve each other by name.

Revisions (0)

No revisions yet.