Is it possible to use Docker to separate web sites for users?
Solution 1:
Yes, it is possible. What you need to do is providing several 80 ports. one for each URLs. You can do this using, e.g. Virtual Host of Apache running on Docker host server.
- Set DNS CNAME.
- Run docker instances and map their port 80 to port, say, 12345~12347 of the docker host.
- Run Apache server on docker host and set a Virtual Host for each URL and set ProxyPass and ProxyPassReverse to localhost:12345 which is one of your docker instances.
Apache config file will look like this:
<VirtualHost *:80>
ServerName www.somewebsite.com
<Proxy *>
Allow from localhost
</Proxy>
ProxyPass / http://local.hostname.ofDockerHost:12345/
ProxyPassReverse / http://local.hostname.ofDockerHost:12345/
</VirtualHost>
Solution 2:
It is possible. You may use apache (or better yet, haproxy, nginx or varnish, that may be more efficient than apache for just that redirection task) in the main server, to redirect to the apache ports of each container.
But, depending on the sites you run there (and their apache configurations), it may require far more memory than using a single central apache with virtualhosts, specially if you have modules (i.e. php) that require a lot of RAM.
Solution 3:
I know this has already been answered however I wanted to take it one step further and show you an example of how this could be done, to provide a more complete answer.
Please see my docker image here with instructions on how to use it, this will show you how to configure two siteshttps://hub.docker.com/r/vect0r/httpd-proxy/
As jihun said you will have to make sure you have your vhost configuration set. My example uses port 80 to display a test site example.com and 81 to display test site example2.com. Also important to note that you will need to specify your content and expose required ports in your Dockerfile, like such;
FROM centos:latest
Maintainer vect0r
LABEL Vendor="CentOS"
RUN yum -y update && yum clean all
RUN yum -y install httpd && yum clean all
EXPOSE 80 81
#Simple startup script to aviod some issues observed with container restart
ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh
#Copy config file across
COPY ./httpd.conf /etc/httpd/conf/httpd.conf
COPY ./example.com /var/www/example.com
COPY ./example2.com /var/www/example2.com
COPY ./sites-available /etc/httpd/sites-available
COPY ./sites-enabled /etc/httpd/sites-enabled
CMD ["/run-httpd.sh"]
Hope this helps explain the process a little be more. Please feel free to ask me any further questions on this, happy to help.
Regards,
V