Docker Community Forums

Share and learn in the Docker community.

Docker ignores container hosts file and use host file!


#1

Hello,

I’m trying to use Docker for the first time Yii2 based project, the project will have 2 site, yii2.devi and admin.yii2.devi, my docker composer file:

###############################################################################
# .  Generated on phpdocker.io                          #
###############################################################################
version: "3.1"
services:

memcached:
  image: memcached:alpine
  container_name: yii2a-memcached
  ports:
    - "11211:11211"
mysql:
  image: mysql:8.0
  container_name: yii2a-mysql
  working_dir: /src/html
  volumes:
    - ./src:/src
  environment:
    - MYSQL_ROOT_PASSWORD=rootPassword
    - MYSQL_DATABASE=yii2
    - MYSQL_USER=root
    - MYSQL_PASSWORD=root
  ports:
    - "3306:3306"

webserver:
  image: nginx:alpine
  container_name: yii2a-webserver
  working_dir: /src/html
  extra_hosts:
    - "yii2.devi:127.0.0.1"
    - "admin.yii2.devi:127.0.0.1"
  volumes:
      - ./src:/src
#          - ./phpdocker/nginx/sites/default.conf:/etc/nginx/conf.d/default.conf
        - ./phpdocker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./phpdocker/nginx/logs:/var/log/nginx
#          - ./phpdocker/nginx/hosts:/etc/hosts
      - ./phpdocker/nginx/sites:/etc/nginx/conf.d
  ports:
   - "80:80"
   - "81:81"
   - "8080:8080"
php-fpm:
  build: phpdocker/php-fpm
  container_name: yii2a-php-fpm
  working_dir: /src/html
  volumes:
    - ./src:/src
    - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini

but, none of the domains resolved, and I have another old local project with local domain yii.devi, which is resolved with the content of yii2.devi!!

The nginx sites config file:

File 1:

server {
listen 80 ;
client_max_body_size 108M;
access_log /var/log/nginx/admin.yii2.access.log;
error_log /var/log/nginx/admin.yii2.error.log;

server_name admin.yii2.devi;

root /src/html/backend/web;
index index.html index.php;

if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
}

location ~ \.php$ {
    fastcgi_pass php-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "error_log=/var/log/nginx/admin.yii2_php_errors.log";
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    include fastcgi_params;
} }

File 2

server {
listen 80 default;

client_max_body_size 108M;

access_log /var/log/nginx/yii2.access.log;
error_log /var/log/nginx/yii2.error.log;

server_name yii2.devi;

root /src/html/frontend/web;
index index.html index.php;

if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
}

location ~ \.php$ {
    fastcgi_pass php-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "error_log=/var/log/nginx/yii2_php_errors.log";
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    include fastcgi_params;
}
}

So, again, the nginx container ignores the added domains, and load the site content on the host machine hosts file.
I ensured the Mojave httpd is killed, and the docker composer domains are added.

Would please help with this. …

Thanks,


(Metin Y.) #2

nginx does not require the extra_hosts setting unless you want to override a hostname outside your container environment.

The client machine needs to have the /etc/hosts overrides, not nginx. Though it needs to point to you docker hosts ip and not to localhost. nginx will pick up the hostname from the url without any problems.


#3

Thanks a lot.
But is this the same case when I move to live host (AWS or digital O.)?


(Metin Y.) #4

This is how a reverse proxy scenario with nginx is, it does not need to be able to resolve the hostname for the server_name it is listinging on. The required information is part of the tcp packets that reach the nginx port.


#5

you right … Thanks a lot