0

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
Zabs
  • 13,852
  • 45
  • 173
  • 297
  • From where backend.local domain should be accessible? From withing docker network or you want to put that domain in your web browser so you can access particular container via that domain? – Jakub Bujny Sep 03 '18 at 10:30
  • Hi Jakub - I want backend.local to be accessible via the browser so it runs the PHP index.php file that is within the var/www directory (this index file is executed when I modify the file above to use ports .eg localhost:8080 (I hope that makes sense) – Zabs Sep 03 '18 at 10:32
  • 2
    I think the easiest way would be to add an entry to your `/etc/hosts/` file of your host. You can find a relative question here: [Access docker container from host using containers name](https://stackoverflow.com/questions/37242217/access-docker-container-from-host-using-containers-name) – tgogos Sep 03 '18 at 10:35

1 Answers1

2

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

Jakub Bujny
  • 4,400
  • 13
  • 27