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.