Reputation: 27038
I have a basic docker setup. The problem is that the ports are conflicting with already used ports from other projects.
For example, this docker-compose.yml
uses port 3306:3306
for mysql, but I'm already using this port.
I can change the port to a different one, but I don't want to commit this changes by mistake and I don't want to ignore this file from git, in case i do need to make some permanent changes in the feature.
Is there a global docker-compose.yml
file that will overwrite the current one? Either for individual projects or for all..
Any ideas?
Upvotes: 0
Views: 440
Reputation: 10111
When running multiple Docker applications on the same host machine, in order to avoid port conflicts you might want to consider not using fixed ports at all.
As written in the documentation:
Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).
So, if you want to run two Docker containers which internally use port 3306
, this way both containers will get this port mapped to a random available port on the host machine (e.g. 32775
for the first container and 32776
for the second one).
To find out which external port got used, simply look it up via docker ps -a
. For automated usage in an automation shell script, run something like
container_id=docker-compose ps -q <service name as in docker-compose.yml file>
echo $(docker port "${container_id}" | cut -d: -f2)
Alternatively, if you know the internal port and it is fixed, you can also run
docker-compose port <service name as in docker-compose.yml file> <private port>
The random port will change each time you remove and re-create your services. If this is not an option, e.g. because you need fixed URLs to communicate to your users, the next step could be to use a reverse-proxy in front of the containers. This way, you can access them via addresses like my-service.my-host.some-domain
or my-host/my-service
, no matter which random port got exposed.
I had some good experience using traefik for this, the nginx docker reverse proxy project also works nicely.
If you are interested in going this way, there are plenty of tutorials available on the internet.
Upvotes: 1
Reputation: 9136
I think you can use docker-compose.override.yml
.
For example, if you have two files like the below.
# docker-compose.yml
web:
ports:
- 8883:80
# docker-compose.override.yml
web:
prots:
- 9999:80
When you run docker-compose up
, it will automatically read docker-compose.override.yml
along with docker-compose.yml
and then it will use port 9999:80
overwriting docker-compose.yml
config.
(It will only overwrite what docker-compose.override.yml
specifies, then use docker-compose.yml
for the rest intactly)
Thus, by creating docker-compose.override.yml
and ignoring it in git
, you can achieve what you want.
For more detail refer the document.
Upvotes: 1