Zabs
Zabs

Reputation: 14142

Host resolution with Docker Compose not working with custom hostname

I have a Docker Compose that when run allows me to reach the container directly in the browser using port 80 (e.g localhost:8080) however I want to add my own custom host names e.g backend.local instead how do I do this in my Docker Compose file

I have added backend.local to my windows hosts file and done a composer build but I keep getting the 'This site cannot be reached' error message

# Adopt version 2 syntax:
#   https://docs.docker.com/compose/compose-file/#/versioning
version: '3'

volumes:
    database_data:
        driver: local

services:
###########################
# Setup the Nginx container
###########################
nginx:
    image: nginx:latest
    container_name: nginx-container
    volumes:
        - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    dns:
        - 192.168.55.10
    dns_search: backend.local

###########################
# Setup the PHP container
###########################
php:
    build: ./docker/php/
    container_name: php-container
    expose:
        - 9000
    volumes:
        - .:/var/www
    hostname: backend.local

###########################
# Setup the Database (MySQL) container
###########################
mysql:
    image: mysql:latest
    container_name: mysql-container
    expose:
      - 3306
    volumes:
      - database_data:/var/lib/mysql
      - ./db/bootstrap.sql.gz:/docker-entrypoint-initdb.d/bootstrap.sql.gz
    environment:
        MYSQL_ROOT_PASSWORD: secret
        MYSQL_DATABASE: project
        MYSQL_USER: project
        MYSQL_PASSWORD: project

Upvotes: 0

Views: 2132

Answers (1)

Jakub Bujny
Jakub Bujny

Reputation: 4628

Docker cannot influence host's DNS resolution so all you can do to achieve your case is:

  • Put mapping in hosts files backend.local -> localhost

  • Use ports statement to expose you application on 8080 port

  • Then you can access your application on both: localhost:8080 and backend.local:8080

Upvotes: 2

Related Questions