Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 207
Shukri Adams Avatar asked Oct 30 '17 09:10

Shukri Adams


People also ask

Can we run multiple Docker containers in a single host?

With Docker compose, you can configure and start multiple containers with a single yaml file. This is really helpful if you are working on a technology stack with multiple technologies.

Can I run multiple Docker containers on same port?

So there is no conflict if multiple containers are using the same port ( :80 in this case). You can access one container from another using its container-name or service-name or ip-address, whereas ip-address is not a good idea because this might change every time you (re)start the container.

How many Docker containers can I run per host?

Using this simple calculation, we can estimate that we can run about 1,000 containers on a single host with 10GB of available disk space.

Can a server run multiple Docker containers?

You can connect multiple containers using user-defined networks and shared volumes. The container's main process is responsible for managing all processes that it starts. In some cases, the main process isn't well-designed, and doesn't handle “reaping” (stopping) child processes gracefully when the container exits.


1 Answers

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 allow forward addresses from the main port (such as 80 and 433) to web-server to other ports or addresses. All your containers should have Nginx 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 container listening on 80 and 433 that forwards the requests using reverse proxies you create to another port.

Good Luck!

like image 52
ChosenQuill Avatar answered Oct 11 '22 02:10

ChosenQuill