Search code examples
networkingdockercontainersdocker-engine

Create multiple Docker network connections


Monday when I got to work I realized that Docker was something that I had to use to fix some server issues in the company at the moment. So since this week all my work has been studying Docker and try to make it work as soon as possible.

So far I understood the containers / swarm / etc, but I am still stuck with the network. Basically I need to run 3 different networks under Docker with different containers on it.

Check image example here please

I need to run 3 different networks which will be assigned to 3 public IPs provided by the hoster (OVH) (I don't even know if it will work since only tomorrow I'll get the VPS to work).

So let's say over the network 1 there will be 3 containers to be used as production, network 2 will be used for development and 3rd network to be used as test.

Is this possible to make with Docker?

ATM I'm running tests on a raspbian (jessie) using Docker engine but like I said, I am still stuck with the whole Docker network interface.


Solution

    • Create the networks

      docker network create net1
      docker network create net2
      docker network create net3
      
    • Attach the containers to the desired network

      docker run --net=net1 --name=container1 [opts] [image]
      

    or, if the container already exists:

    docker network connect net1 container1
    

    if you to attach a host IP to the container you can just bind a port to it. Let's say a container runs o port 80:

    docker run --name=container1 --net=net1 -p YOU_IP_ADDR:80:80 [image]