Multiple instances of docker-compose on a single server that listen on different ports

I am using an application which runs 3 different docker images:

  • The first one is the server nginx with port 2000 mapped to 8080 on the host
  • The second one is database
  • The third one is a worker

The whole application is launched with docker compose up.

I want multiple users to bring up the same application on a single host/node. Since the docker-compose.yaml instructs to map port host’s 8080 port to 2000 for the first instance, is there a way to pass a different port (say 8090) for the 2nd user’s instance? Or is my only option to have a different docker-compose.yaml file for each user? Wondering if there are other options with newer docker compose.

You can pass the port number as an environment variable:

  ports:
    - ${APP_HOST_PORT:-8080}:2000

and tell the users to set the environment variable in their .bashrc with different ports.

But you also need to deal with the project name as well and possibly with bind mounts. Anything that would be the same for all users. If you use the same project name, they would overwrite eachother’s containers. I don’t remember if the name parameter in Docker Compose v2 compose files support environment variables, but if they do, you can use the same solution I recommended for ports.

On the other hand, I would rather make a script and ask the users to run the script instead of docker compose directly. Then you can handle everything in the script depending on the userid instead o relying on the suers properly set the environment variables.

Making multiple compose files are not a bad idea either, but it is enough to create an override file. compose.override.yml is read by Docker Compose v2 by default and merged with the default compose file. Of course that works only if the files in the same folder and you csn’t have multiple files with the same name for different users so you would need to set the file names

docker compose -f compose.yml -f compose.user1.yml up -d
1 Like