Docker Networking Practical Examples

Docker Networking Practical Examples

Photo by Antoine Petitteville on Unsplash

Quick Overview of Docker Networking

  • bridge: the default network driver,
  • host: for standalone containers,
  • overlay: used in Docker swarm,
  • macvlan: works on MAC addresses assigned to containers,
  • none: networking is disabled,
  • Network plugins: Third-party network plugins with Docker.
  • MacOS,
  • Windows.

Practical Examples

Named Network

  • docker network create --driver=bridge next-net we have to create a new user-defined network. Bear in mind named bridge network is not the same as the default bridge network, it has automatic DNS resolution…
  • docker run -d --name=next-blog-api --net=next-net -p 8888:9000 next-blog-api start the SERVER with mapped port (our service is running on port 9000 inside of the container) and we give it a name, so a random name is not allocated to that container
  • docker run -d --net=next-net -p 3002:3000 next-blog start the client on the same network (service inside of this container should now be pointing to http://next-blog-api:9000)

  • in Dockerfile when you EXPOSE a port, that signals to a hosting service that it needs to bind to this port to access the service running inside. For example, if we do not EXPOSE port, a platform like Azure might assume that application inside is running on default web port 80, which will cause connection failure.
  • and when you use docker run -p ... flag, that makes it accessible outside of Docker network

Host Network (Only for Linux)

  • published ports are discarded when using host network mode (we can’t specify a different port for that container. We can’t map ports as we did in bridge network with -p 9001:9000.
  • it works only on Linux as stated here
  • the container will take the namespace and port of the host
  • docker run -d --network=host next-blog-api
  • docker run -d --net=host next-blog

Links (for Docker Engines Before 1.9 Version)

  • we have to make sure that correct ports are open and nothing is blocking them
  • enable forwarding from Docker containers to the outside world (that is on the Linux host machine)
  • You need the following ports open to traffic to and from each Docker host participating on an overlay network: — TCP port 2377 for cluster management communications — TCP and UDP port 7946 for communication among nodes — UDP port 4789 for overlay network traffic

Troubleshooting

  • docker network ls will display a list of currently defined networks

  • docker network inspects <network name> will give us details (IP’s, gateways, subnets) of the specific network.

  • docker inspect <container id> will give us details of the specific container (including networking details)

echo '
FROM ubuntu:16.04
RUN apt update && apt install -y arp-scan iputils-ping iproute2
CMD ["/bin/bash"]' > Dockerfile
docker build . -t net-tool
docker run -it net-tool
ip addr show #run inside of container
arp-scan --interface=eth0 --localnet #run inside of container