Docker Compose | Virtual Hosts

Roberto Godoy picture Roberto Godoy · May 4, 2018 · Viewed 9.2k times · Source

Whats wrong in my code? thanks in advance!

I'm trying to set up a virtual host for my docker container. On localhost: 8000 works perfectly, but when I try to access through http: //borgesmelo.local/ the error ERR_NAME_NOT_RESOLVED appears, what can be missing?

This is my -> docker-compose.yml

version: '3.3'

services:

      borgesmelo_db:
            image: mariadb:latest
            container_name: borgesmelo_db
            restart: always
            volumes:
                  -  ./mariadb/:/var/lib/mysql

            restart: always
            environment:
                  MYSQL_ROOT_PASSWORD: My@159#Sql
                  MYSQL_PASSWORD: My@159#Sql

      borgesmelo_ws:
            image: richarvey/nginx-php-fpm:latest
            container_name: borgesmelo_ws
            restart: always
            volumes:
                  -  ./public/:/var/www/html
            ports:
                  - "8000:80"

      borgesmelo_wp:
            image: wordpress:latest
            container_name: borgesmelo_wp
            volumes:
                  -  ./public/:/var/www/html
            restart: always
            environment:
                  VIRTUAL_HOST: borgesmelo.local
                  WORDPRESS_DB_HOST: borgesmelo_db:3306
                  WORDPRESS_DB_PASSWORD: My@159#Sql
            depends_on:
                  - borgesmelo_db
                  - borgesmelo_ws

      borgesmelo_phpmyadmin:
            image: phpmyadmin/phpmyadmin:latest
            container_name: borgesmelo_phpmyadmin
            links:
                  - borgesmelo_db
            ports:
                  - "8001:80"
            environment:
                  - PMA_ARBITRARY=1

      borgesmelo_vh:
            image: jwilder/nginx-proxy
            container_name: nginx-proxy
            ports:
                  - "8002:80"
            volumes:
                  - /var/run/docker.sock:/tmp/docker.sock:ro
networks:
  default:
    external:
      name: nginx-proxy

This is my hosts file (/etc/hosts) [macOS]

#DOCKER
127.0.0.1:8000          borgesmelo.local

Answer

Rickkwa picture Rickkwa · May 4, 2018

Hosts file doesn't support ports as it is for name lookup only. So you would have to set your hosts file to:

127.0.0.1          borgesmelo.local

Then access your application with http://borgesmelo.local:8000.

If you are listening on port 8000 because you already have something else on port 80, then consider using nginx as a reverse proxy and then you can route to different applications based on the server_name. That way, you can access multiple applications through port 80. If you're dealing with docker containers, then consider looking into Traefik as a reverse proxy.