Docker Community Forums

Share and learn in the Docker community.

Docker network subnet

Hi everyone
i want to run docker-compose up for many times for different environments .
i knew that i can run it specifying different names with this command (docker-compose -p name up -d)
i want that every time new containers run in different subnet and get other ip addresses that i can use the same ports for applications.
How can i do that?

This is ambigous. If you use -p to set the project name, every deployed set of containers will live in their own “namespace”. Every container, network, volume created will have the project name as prefix, followed by an underscore, followed by the name you set for the object, e.g. {project name}{network name}, {project name}{volume name}. Though inside the compose,yml you configure the object without the project_name.

If by same ports for application you mean the host port of a publised port: this is impossible - a host port can only be bound from one process. Regardless whether it’s running on the host or is a published port for a container.

If by same ports for application you mean the container port. This is the normal behavior, unless you run your containers with network_mode: host (which will directly bind the container port to the host network’s interface, the point from above applies here as well).

Whatever you do, never ever use a containers ip in container to container communication OR when you access the container from the host… actualy NEVER use it: the ip is unrealiable and will eventualy change. If you want to access a container from the host, you need to publish the container port to a host port and use {host_ip_or_name}:{publised_host_port} to access the container.

1 Like

Creates a new network. The DRIVER accepts bridge or overlay which are the built-in network drivers. If you have installed a third party or your own custom network driver you can specify that DRIVER here also. If you don’t specify the --driver option, the command automatically creates a bridge network for you. When you install Docker Engine it creates a bridge network automatically. This network corresponds to the docker0 bridge that Engine has traditionally relied on. When you launch a new container with docker run it automatically connects to this bridge network. You cannot remove this default bridge network, but you can create new ones using the network create command.

$ docker network create -d bridge my-bridge-network
Bridge networks are isolated networks on a single Engine installation. If you want to create a network that spans multiple Docker hosts each running an Engine, you must create an overlay network. Unlike bridge networks, overlay networks require some pre-existing conditions before you can create one. These conditions are:

Access to a key-value store. Engine supports Consul, Etcd, and ZooKeeper (Distributed store) key-value stores.
A cluster of hosts with connectivity to the key-value store.
A properly configured Engine daemon on each host in the cluster.
The dockerd options that support the overlay network are:

To read more about these options and how to configure them, see “Get started with multi-host network”.

While not required, it is a good idea to install Docker Swarm to manage the cluster that makes up your network. Swarm provides sophisticated discovery and server management tools that can assist your implementation.

Once you have prepared the overlay network prerequisites you simply choose a Docker host in the cluster and issue the following to create the network:

$ docker network create -d overlay my-multihost-network