I have 2 websites that clients need to connect to on port 80. Each website runs in its own container. I want to run both containers on the same Docker host.
I understand that port 80 can only be exposed on the Host one time. What solutions exist that have minimal overhead/administration that will allow me to simply run both containers on the same host (while still allowing clients to reach each container on port 80) ?
Both website 1 and website 2 should appear to client the web browser on port 80 and have friendly URL's (ie: www.web1.com, www.web2.com)
Use an nginx reverse proxy:
apt-get install nginx
. Note: I've assumed you don't have apache or some other web server on the host already.../etc/nginx/sites-available
that redirects that site to a docker container's http that will run on some other prearranged port (for example, 2001, 2002, ...). Each site gets its own file, like the one below, but with different prearranged ports for each site. External users will access these at port 80 of the same IP address but with different web site names, and from these names nginx will handle the necessary internal connections invisibly./etc/nginx/sites-enabled
and restartnginx
. Later, you can remove one of these links and restart nginx if you need to temporarily disable access to a site.docker run
commands to/etc/rc.local
, and redirecting the prearranged host port (localhost:2001) to container port 80 e.g.docker run -d -p localhost:2001:80 imageA
If a container is down you will get a gateway error from nginx. This could be customized to show a custom HTML page. For more robustness, it might be better to manage the containers in
supervisord
or some other process manager that respawns dead processes.Here is an example nginx site file for a redirection to port 2001:
In this example, although a local disk directory for HTML files is set up for site A, it is not used. Instead all requests are sent to the upstream. I haven't tested whether the
root
andindex
lines can be safely omitted.A more proper way to host multiple websites on a single host using docker, docker-compose and nginx-proxy: https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/