24 January 2018

Networking in docker

Docker works on the principal of running containers as a service, when you run a container it has its
own attributes like namespace ip-address port etc. This attributes are allocated to containers by docker daemon at run time. There are ways to control this behavior like creating namespaces of your choice at the time of launching them.

Same way when it comes to ip-addresses you can create your own docker network which can give a static ip to your container or its underline service. 

docker comes with 5 kind of networking drivers:

bridge : when you want to communicate between standalone containers.
overlay : to connect multiple Docker daemons together and enable swarm services to communicate with each other.
host : For standalone containers, remove network isolation between the container and the Docker host. 
macvlan : Allow you to assign a MAC address to  container, making it appear as a physical device on your network.
none : disables all networking.

by default bridge is the default driver that got created when you launch any of the container as service.

How one can create its own docker network as per requirement 

the syntax to create a network is : 

$ docker network create --options networkname

few widely used options are:

--driver drivername
--subnet=subnetrange/x
--gateway=anIPfromdefinedsubnet

for example assigning static IP out of your your CIDR block


 $ docker network create --driver overlay --subnet=192.168.0.0/26 --gateway=192.168.0.1 my-network


additionally you can use this created network for your container at the the time of its launch

for example:


 $ docker run --publish 61000:1414 --publish 61001:9443 --net my-network --ip 192.168.0.3 --detach --env
  MY_APP_PASSWORD=password punitporwal07/apache:2.2.29


this way your container will be allocated with an static IP within your defined subnet range.

k/r
P

No comments:

Post a comment