,

Running multiple containers mapped in different domains – the discourse example

 

“Do you want to have multiple docker containers in one server running different services, all mapped in different domain names and discourse to be one of them?”

The idea

This is another blog post coming directly from TechMinistry, Thessaloniki’s local hackerspace.

It’s the time of the year where we are brainstorming about the redesign of our website. We want to add some visibility in our hackerspace’s activities and promote participation within the city’s online community.

So we decided to give Discourse a try to in order to explore all of its features and especially its API.

Unfortunately we didn’t have a spare server to host it so we had to install it in a new docker container since our website already exist in another one.

 

The problem

Our problem was how to test discourse while there was another docker container listening to the same port, 80.

Hint: “nginx reverse proxy”.

After a lot of “ducking” we found that you can set up an nginx reverse proxy server outside the 2 containers in order to handle the requests.

Configuring NGINX

In your server (NOT in any of the containers) you need to create a configuration file and put it inside /etc/nginx/conf.d .

sudo nano /etc/nginx/conf.d/default.conf

The file name doesn’t really matter but inside the it you need to paste the contents of this file.

As you can see in the configuration file, there are two different “apps” (you can change their names if you like) which each one is pointing to a different port in the localhost.

upstream app-a {
server 127.0.0.1:2121;
}

upstream app-b {
server 127.0.0.1:2020;
}

Nginx will forward traffic to these ports, depending on which app you are trying to access.

server {
server_name techministry.gr www.techministry.gr;

location / {
proxy_pass         http://app-a;
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_set_header   X-Forwarded-Host $server_name;

}
}

At this part we are making clear to nginx that the requests for the techministry.gr URL belong to the app-a and should be forwarded to the relevant port. The same for the other app.

Now we only need to update the nginx process and start the containers. In order for these changes to take affect without restarting nginx you can use the following command:

sudo nginx -s reload

 

Configuring a simple docker container

Let’s run the website container:

docker run –name website -p 2121:80 -t -i -d techministry

Now the website container will forward the traffic that receives from external port 2121 to internal port 80.

Configuring Discourse for the nginx reverse proxy

For making a similar configuration for the discourse container we will need to edit it’s configuration file. We are assuming that you have already followed the Discourse installation guide and you are ready to launch it. Before launching it you need to go back to the app.yml file and change the EXPOSE port to 2020.

nano containers/app.yml

and change the “## which TCP/IP ports should this container expose?” line to:

expose:
– “2020:80″   # fwd host port 80   to container port 80 (http)
– “2222:22″ # fwd host port 2222 to container port 22 (ssh)

Then you follow the discourse installation guide for building the image and running it.

When the installation is complete and the container is up and running you can visit your fresh discourse installation by simply accessing the domain name you reserved in the nginx configuration file which in that case is http://betatechministry.gr.

You can also access the container using your server’s IP followed by the container port – in that case 78.155.69.90:2020.

Conclusion

Following this technique you can have multiple docker containers running different services, all mapped in different domain names.
Pretty awesome, right?

I want to give a special credit  to my friend and TechMinistry’s member Alexandros who dedicated a lot of his free time for this.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.