santi6291
santi6291

Reputation: 164

Docker run on sub domains

My goal is to deploy multiple webapps and access them through sub-domains, I'm currently running them on different ports, I have nginx on the server and the containers are running apache.

docker run -p 8001:80 -d apache-test1
docker run -p 8002:80 -d apache-test2

and I'm able to access them by going to

http://example.com:8001

or http://example.com:8002

But I like to access them through sub-domains instead

http://example.com:8001 -> http://test1.example.com
http://example.com:8002 -> http://test2.example.com

I have nginx running on the server with the following server settings

server {
    server_name test1.anomamedia.com;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host ;
        proxy_set_header X-Real-IP $remote_addr ;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
        proxy_pass http://localhost:8001;
    }
}

server {
    server_name test2.anomamedia.com;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host ;
        proxy_set_header X-Real-IP $remote_addr ;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
        proxy_pass http://localhost:8002;
    }
}

If its of any help this is my Dockerfile

FROM ubuntu

RUN apt-get update
RUN apt-get -y upgrade

RUN sudo apt-get -y install apache2 php5 libapache2-mod-php5

# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur

# Enable apache mods.
RUN a2enmod php5
RUN a2enmod rewrite

EXPOSE 80

# Copy site into place.
ADD html /var/www/html

# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf

# By default, simply start apache.
CMD /usr/sbin/apache2ctl -D FOREGROUND

Upvotes: 4

Views: 4646

Answers (2)

Florian Lopes
Florian Lopes

Reputation: 1289

Have a look at the nginx-proxy project (https://github.com/jwilder/nginx-proxy). I wrote a tutorial in my blog which demonstrates exactly what you want to achieve using it: https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker

This tool automatically forwards requests to the appropriate container (based on subdomain via the VIRTUAL_HOST container environment variable).

For example, if you want to access your container through test1.example.com, simply set the VIRTUAL_HOST container environment variable to test1.example.com.

Upvotes: 0

MadDocNC
MadDocNC

Reputation: 820

I have similar problems. Also I often need to add and remove containers, so I don't whant to edit nginx conf everytime. My solution was using jwilder/nginx-proxy.

then you just start containers with exposed port (--expose 80, not -p 80:80) and add env variables:

-e VIRTUAL_HOST=foo.bar.com

Don't forget to transfer with your main nginx traffic with right headers:

server {
    listen       80;# default_server;
    #send all subdomains to nginx-proxy
    server_name  *.bar.com;

    #proxy to docker nginx reverse proxy
    location / {
        proxy_set_header   X-Real-IP $remote_addr; #for some reason nginx 
        proxy_set_header   Host      $http_host;   #doesn't pass these by default
        proxy_pass         http://127.0.0.1:5100;
    }

}                    

Upvotes: 3

Related Questions