'Is it possible to run multiple nginx docker containers on the same host?

Is is possible, using only docker-compose, to mount multiple nginx containers on the same host machine, with each container sharing port 80?

I want to tidy up a system that runs multiple applications on the same host. Each application consists of several linked docker containers tied together with a docker-compose file, and each application is exposed to the world using the host system's nginx as a reverse proxy. So far, so good.

Each time I add a new docker application, I have to add a new nginx.conf file for that application to the host nginx, but I'd prefer bundling the nginx config with the app's docker-compose file as an nginx container, and thus have each app cleanly maintain everything it needs in containers. However, each nginx container needs to listen on port 80, so only the first one can bind. The host nginx can listen for several web applications on port 80, but can multiple nginx instances do the same?

UPDATED :

So it seems this isn't strictly possible. The goal is to have as much application-specific nginx config bundled with the application, so I'm trying a solution where an app still spins up its own nginx container with that logic, while the host nginx handles only url routing to the app nginx. Not sure about performance, but this greatly reduces app entanglement with the host.



Solution 1:[1]

Even though it has been a few years, I thought I might as well answer it for others who come across this issue.

Unfortunately, no. It's not possible to have multiple containers on the same port. This is due to the nature of the operating system. But I think I can understand what you are trying to do. You want to have multiple custom config web applications.

The best way to do this is through a reverse proxy. What a reverse proxy does is that it allows you to forward requests from the main port (such as 80 and 433) to web-servers on other ports or addresses. Assuming that all of your containers have their own instance of Nginx (or any other web server) in them with your config and code. There are two ways to do this.

In docker, the easiest way to do this is through the nginx-proxy project. Just by adding a variable to the environment in your docker-compose, a managed nginx container will automatically forward your requests to the set site. This seems like what you would want to do since it requires only a docker-compose.

You can also manage this yourself. Keep all your sites listening on other ports in nginx, and have one main nginx container listening on 80 and 433 that forwards the requests using reverse proxies you create to another port.

Good Luck!

Solution 2:[2]

It is still there.

Click on the three dots and you should see it. If not, restart your browser or PC.

enter image description here

Solution 3:[3]

I had this happen as well. The reason was the indicator was read only. There is a solid black lock on the code.

I had to make a copy. This unlocked it and I could convert.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 vitruvius
Solution 3