Need help?

Our experts will login to your server within 30 minutes to fix urgent issues.

Customer support or server support, make your own solution using our support configuration wizard.

Want multiple Docker containers on the same port? Here’s how

Docker helps web developers get their apps online without bugs.

While it is intuitive and easy to use Docker for development, using Docker in production servers is riddled with problems.

Here at Bobcares.com, we help web developers and digital marketers to setup and maintain Docker based web hosting servers.

And a common challenge we resolve is : “How to use One server to run several hundred sites running on Docker containers.

 

Why can’t multiple containers bind to a the same HTTP port?

Websites listen to port 80 to receive requests and serve pages.

So each website Docker container has its port 80 open.

However, the Docker host has only one port 80, and can receive requests for only one of these containers.

docker multiple containers same port - problem

Docker can be configured to bind each container to separate server ports. For eg. bind Docker 1’s port 80 to servers port 8080, Docker 2’s to 8081, etc.

But since browsers will by default connect to port 80, and not 8080 or 8081, these sites will not be visible.

 

Using a Reverse Proxy to run multiple containers on one server

A reverse proxy is a program that accepts connections on port 80, and sends it to one or more servers in the back end.

So, if we can put a Reverse Proxy to listen at 80, and get it to split traffic based on domain names, we can host multiple domains with just one port 80.

There are many reverse proxies such as HAProxy, Nginx and Varnish.

For most Docker based web hosting purposes, we’ve found Nginx containers to be a good fit.

docker multiple containers same port - Nginx solution

How to setup a HTTP gateway using an Nginx container

There are 3 parts to setting up an Nginx gateway:

  • Run a Nginx container
  • Make the Nginx config files editable through a volume
  • Configure Nginx to connect to website’s internal IP

 

1. Bind port 80 and 443 of the Docker host to the Nignx container

For Nginx to act as the gateway, it must have control over the Docker host server’s HTTP and HTTPS ports.

To set it, setup a docker compose file with the “ports” directive like this:

http-gateway:
container_name: http-rev-prox
image: jwilder/nginx-proxy
ports:
- '80:80'
- '443:443'

Depending on your website needs and traffic patterns you may need additional settings such as “CLIENT_MAX_BODY_SIZE” and “PROXY_READ_TIMEOUT” set in the “environment” directive.

 

2. Make the Nginx configuration file editable from outside the container

Some of our customers have Docker servers that contain hundreds of sites.

In these servers, we add, modify and remove websites on a fairly regular basis.

For this, it is important to be able to edit the Nginx configuration file without having to recreate the Nginx image.

We use Docker Volumes for this, where the Nginx configuration folder “/etc/nginx/conf.d/” is mounted on an external directory such as “/usr/docker/nginx/conf.d“.

The Docker composer settings look like this:

volumes:
- /usr/docker/nginx/conf.d:/etc/nginx/conf.d/:rw

Note that this is only a part of the big compose file. For a fully functional gateway, you’ll need to add additional settings like SSL directory, Docker socket location, etc.

Click here for expert Docker assistance.

 

3. Configure Nginx to connect requests to a site to the site container’s private IP

Now that we have Nginx config files in an editable location, create configuration files for each site, with a “proxy_pass” directive to link the site’s private IP to Nginx.

For eg. a domain’s (say mydomain.com) configuration file may look something like this:

location / {
proxy_pass http://10.1.2.3:80;
proxy_set_header Host $host;
}

At this point, Nginx listens to port 80, and forwards any requests coming to mydomain.com to it’s internal IP 10.1.2.3.

 

Getting started with Multi-domain hosting in Docker servers

The steps we’ve mentioned here is only an outline that touches the core factors to make this work.

The fine details are omitted to keep the subject focused.

But in an actual production environment, there can be a hundred small issues that can affect quality of hosting.

It can range from setting up resource limits & SSL to bugs like config leaks & protocol mixup.

So, if you are trying to set this up from scratch, we’d recommend you talk to a Docker expert to get it setup exactly matching your requirements.

Our Docker experts are online 24/7 and can come to your help within minutes. Click here to open a support request.

 

Conclusion

Docker allows only a 1 to 1 port binding, which prevents multiple Docker containers to use the same port of the main server (Docker host). Here we’ve discussed how to use a reverse proxy to divert requests to many domains through one HTTP port in the Docker host.


PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

Submit a Comment

Your email address will not be published. Required fields are marked *

Bobcares
Bobcares is a server management company that helps businesses deliver uninterrupted and secure online services. Our engineers manage close to 51,500 servers that include virtualized servers, cloud infrastructure, physical server clusters, and more.
MORE ABOUT BOBCARES