Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker containers serving different subdomains on port 80

Is it possible to have a 2 docker containers serve on port 80 but different subdomains or hostnames?

Something like:

api.example.com goes to a node application

app.example.com goes to a Java application

like image 821
Your Friend Ken Avatar asked Jan 30 '23 12:01

Your Friend Ken


2 Answers

Yes you can. using a proxy.

There is a project by jwilder/nginx-proxy which allows you to give your hostname via an enviroment variable which will than route your request to the appropriate container.

A good example of this implemented is given here: https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/

like image 51
Yonah Dissen Avatar answered Feb 06 '23 09:02

Yonah Dissen


No. The first container you start will have exclusive access to the port, and if you try and start a second container on the same port it will fail.

Instead, use a load balancer such as Nginx or Traefik to handle the incoming traffic to port 80 and proxy it on to your two app containers based on host headers.

like image 28
Trondh Avatar answered Feb 06 '23 08:02

Trondh