Setup subdomains in docker environment

2019-04-14 06:36发布

问题:

I am running different services in Docker Containers on my Ubuntu server (14.04 Trusty Tahr) at home. My currently Setup at the moment is as following:

  • Jenkins jenkins.slarti
  • Gitlab and leanlabs.io under gitlab.slarti
  • Java Servlet Webserver under servlet.slarti (apache tomcat)
  • "Normal" Webserver under web.slarti (nginx)

These domains are virtualhost's and I have already setup an nginx proxy.

Nginx-proxy docker run:

docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

Jenkins docker run:

docker run -d --name jenkinsci --expose 8080 -e VIRTUAL_HOST=jenkins.slarti -e VIRTUAL_PORT=8080 jenkins

Normal Webserver docker run:

sudo docker run --name nginx1 -P -d --expose 80 -e VIRTUAL_HOST=web.slarti nginx

Gitlab & leanlabs.io docker run:

docker run -d  --link kanban_redis:kanban_redis -p 80:80 -e KANBAN_SERVER_HOSTNAME="http://localhost"  -e KANBAN_GITLAB_URL="http://mygitlab.com"  -e  KANBAN_REDIS_ADDR="kanban_redis:6379" --expose 80 -e VIRTUAL_HOST=gitlab.slarti leanlabs/kanban:1.4.0

Apache Tomcat docker run:

docker run -it --rm -p 8888:8090 -e VIRTUAL_HOST=servlet.slarti tomcat:8.0

I did the setup according to this guide http://blog.florianlopes.io/host-multiple-websites-on-single-host-docker

Currently I can't ping web.slarti, servlet.slarti etc. Neither can i reach these domains if i type their url in the browser.

I can reach Apache Tomcat via slarti:8888 but I want to reach it through servlet.slarti and that fails since i don't have a DNS setup.

What do I have to do to reach the subdomains web.slarti, servlet.slarti etc? How do I setup the reverse proxy correctly so I can reach the domains? And how do I have to configure the DNS?

回答1:

I am not expert on networks. But after reading your post, it appears that you do need a DNS config like this:

A -> slarti (or IP)
CNAME jenkins.slarti -> slarti
CNAME gitlab.slarti -> slarti
CNAME servlet.slarti -> slarti
CNAME web.slarti -> slarti

This is what I have done to redirect my subdomains to my Docker containers.

Without proper DNS configuration, your subdomains can't be reached.

With this DNS config, everytime you request gitlab.slarti, you actually reach Nginx, which in turn redirects you to the proper Docker container (if the VIRTUAL_HOST environment variable is configured to gitlab.slarti).



回答2:

  1. Add an A record for *.example.com for the root IP Address of the machine (Your Ubuntu Server).
  2. Then start binding as much sub domains you want to bind with multiple docker containers those are running different services.

For example:

docker run -d --name jenkinsci --expose 8080 -e VIRTUAL_HOST=jenkins.slarti -e VIRTUAL_PORT=8080 jenkins