Docker Community Forums

Share and learn in the Docker community.

Unable to map nginx container to a hostname however works with ports (e.g localhost:8080)

docker

(Gav) #1

For github I am using the following repo to get a working local PHP development environment with Docker on my Windows machine - which uses nginx, PHP and MySQL

When I run docker-compose up -d it prepares the containers and I can open up http://localhost:8080 in the browser and see my PHP application. I want to be able to access this from the browser without using the ports, so rather than http://localhost:8080 I want to be able to access this via a custom hostname e.g http://docker.backend - my nginx conf file looks like following -

// note I have tried to amend the conf file below and include the server_name to docker.backend but still have no joy

server {

# Set the port to listen on and the server name
listen 80 default_server;

# Set the document root of the project
root /var/www/public;

# Set the directory index files
index index.php;

# Specify the default character set
charset utf-8;

# Setup the default location configuration
location / {
    try_files $uri $uri/ /index.php;
}

# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

sendfile off;

client_max_body_size 100m;

# Specify what happens when PHP files are requested
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param APPLICATION_ENV development;
    fastcgi_intercept_errors off;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 4 16k;
}

# Specify what happens what .ht files are requested
location ~ /\.ht {
    deny all;
 }
}

I have the following docker-compose.yml below :-

version: '2.2'
volumes:
  database_data:
    driver: local
services:
  nginx:
    image: 'nginx:latest'
    container_name: nginx-container
    ports:
      - '8080:80'
    volumes:
      - './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf'
    volumes_from:
      - php
  php:
    build: ./docker/php/
    container_name: php-container
    expose:
      - 9000
    volumes:
      - '.:/var/www'
  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: root
      MYSQL_DATABASE: dev
      MYSQL_USER: dev
      MYSQL_PASSWORD: root