How to have domain names for applications inside different containers?

Hi.
With this default config

version: '3.2'

services:

  frontend:
    build: frontend
    ports:
      - 20080:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app

  backend:
    build: backend
    ports:
      - 21080:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app

and frontend\Dockerfile:

FROM yiisoftware/yii2-php:8.1-apache

# Change document root for Apache
RUN sed -i -e 's|/app/web|/app/frontend/web|g' /etc/apache2/sites-available/000-default.conf

I can access these sites only like
http:/localhost:20080

But I would like to use user friendly domainnames

My try is this:

FROM yiisoftware/yii2-php:8.1-apache

# Change document root for Apache
RUN sed -i -e 's|/app/web|/app/frontend/web|g' /etc/apache2/sites-available/000-default.conf

RUN sed -i -e 's|#ServerName www.example.com|ServerName yii2ware.front|g' /etc/apache2/sites-available/000-default.conf

hosts

127.0.0.1       yii2ware.front
127.0.0.1       yii2ware.back
services:

  frontend:
    build: frontend
    ports:
      - 80:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app

  backend:
    build: backend
    ports:
      - 80:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app

http:/yii2ware.front works.
But backend does not work - port is already allocated

I could not use ports like 20080 in docker-compose because hosts file does not use them.

What is the right way to do this?
I need two applications , both in their own container, and I want domain names for each of them.
Thanks

You need a reverse proxy, check nginx-proxy with TLS companion or simple Traefik example.

1 Like

Any working example?

Those docs are too complicated when you are not linux admin or devops or whatever

Check the detailed nginx-proxy acme-companion example using ENV vars or simple Traefik example using labels for automatic configuration.

it seems a too complicated task for such problem, all those proxy.

Currently I solved task by dropping two applications to one.
Both frontend and backend are called from one container

No one said IT would be easy :wink:

Reverse proxy is best practice to send requests by (sub-)domain to different services.

And it doesn’t get easier than starting 1 or two additional containers and add ENV or Label to the target service/container with the domain name to use.