Sobes.tech
Junior — Middle

Explain how network interaction occurs inside a Docker container and how it connects to external networks.

sobes.tech AI

Answer from AI

In a Docker container, network interaction is organized through virtual networks that Docker creates and manages.

Key points:

  • Each container gets a virtual network interface (usually eth0) with an IP address within the Docker network.
  • By default, Docker creates a bridge network to which containers are connected.
  • Containers within the same network can communicate with each other via IP or container name.
  • To access external networks (the internet), the container uses NAT through the host machine.
  • Custom networks (bridge, overlay) can be created for isolation and configuration.
  • Container ports can be forwarded to the host so that external applications can access services inside the container.

Example of running a container with port forwarding:

docker run -d -p 8080:80 nginx

Here, port 80 inside the container is accessible on port 8080 of the host.