Bobcares

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

by | Sep 21, 2018

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

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. tehmilcho

    Hi,

    im pretty new in this Docker Topic. I found Docker because im in the “DIY Smarthome Hobby stuff”. I had 2 Raspberrys with multiple Service then i got a lattepanda delta and want to switch all over to one System.

    But now i have the problem that the Services (Pimatic, IoBroker, Pihole) are in each other’s way, because Pimaitc – Alexa Echo Plugin needs port 80 and Pihole needs port 80 and so stuff.

    I know a ready a littlebit about Nignx Reverse Proxys with Multiple Webservices but i dont know i should work with Services that are not a Website / Domain because Pihole is a DNS-Server and “Pimatic – Alexa Echo Plugin” builds a “virtual Philipps Hue Bridge”.

    I think i need a separate IP-Adress for Pihole and Pimatic. What you think?

    Reply
    • Sijin George

      Hello,
      Our Engineers need to take a closer look at the Docker settings. We’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply

Submit a Comment

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

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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF