Cannot curl by domain name, just by IP!

Hello,

I have 3 containers (PHP, MySql, Nginx) when I do curl -I example.com I get error message curl: (7) Failed to connect to localhost port 80: Connection refused, but with Nginx IP it works fine, how to fix this?

Thanks,

Is the Nginx IP = 127.0.0.1?
Do you have a host entry that point example.com to the Nginx IP?

I’m new to this. the Nginx IP 172.19.0.5, and the PHP IP 172.19.0.2

Here is Nginx entry:
#!/bin/bash
truncate -s 0 /var/log/nginx/*.log
exec nginx -g ‘daemon off;’

and here is my Nginx Dockerfile:

 FROM nginx:alpine
 ARG DEV_DOMAIN
 ARG NGINX_SSL
 ENV DEV_DOMAIN $DEV_DOMAIN
 RUN apk update && apk add \
openssl \
bash
 EXPOSE 80
 EXPOSE 443
 COPY entrypoint/entrypoint.sh /entrypoint.sh
 RUN chmod +x /entrypoint.sh
 ENTRYPOINT ["/entrypoint.sh"]

The docker-composer.yml:

version: "3.5"
services:

######### REDIS
###############
redis:      
  build: 
    context: ./phpdocker/redis
  image: 'yii2:redis5'
  container_name: ${PROJECT_NAME}-redis
  ports:
    - "6379:6379"
  volumes:
    - ./phpdocker/redis/conf/enabled:/sys/kernel/mm/transparent_hugepage/enabled
    - ./phpdocker/redis/conf/defrag:/sys/kernel/mm/transparent_hugepage/defrag
    - ./phpdocker/redis/conf/redis.conf:/etc/redis.conf
    - ./phpdocker/redis/logs:/var/log/redis
  sysctls:
    net.core.somaxconn: 511
    net.ipv4.tcp_syncookies: 0        
  
####### MYSQL
###############
mysql:
  image: "yii2:mysql8.0"
  build:
    context: ./phpdocker/mysql
  
  container_name: ${PROJECT_NAME}-mysql
  working_dir: /app
  environment:
    MYSQL_USER: ${MYSQL_USER}
    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    MYSQL_DATABASE: ${MYSQL_DATABASE}
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}   
  tty: true    
  ports:
    - "3306:3306"
  restart: always
  volumes:
    - ./app:/app
    - ./phpdocker/mysql/data:/var/lib/mysql        
    - ./phpdocker/mysql/log:/var/log/mysql
    - ./phpdocker/mysql/config:/etc/mysql/conf.d
  
####### Nginx
###############
nginx:
  image: 'yii2:nginx-alpine'
  build:
        context: ./phpdocker/nginx
        args:
            DEV_DOMAIN: ${DEV_DOMAIN}
            NGINX_SSL: ${NGINX_SSL}
  container_name: ${PROJECT_NAME}-nginx
  working_dir: /app
  volumes:
      - ./app:/app
      - ./phpdocker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./phpdocker/nginx/logs:/var/log/nginx
      - ./phpdocker/nginx/sites:/etc/nginx/conf.d          
  ports:
   - "80:80"
  restart: always
  depends_on:
    - php-fpm
  links:
    - php-fpm
    - mysql
    - redis

####### PHP
###############    
php-fpm:
  image: "yii2:php-fpm"
  build: 
    context: ./phpdocker/php-fpm
    args:
        XDEBUG_REMOTE_HOST: ${XDEBUG_REMOTE_HOST}
        XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
        XDEBUG_PORT: ${XDEBUG_PORT}
  container_name: ${PROJECT_NAME}-php
  working_dir: /app
  ports:
    - "9000:9000"
  volumes:
    - ./app:/app
    - ./phpdocker/php-fpm/conf/php-${PHP_FPM_CONF}.ini:/usr/local/etc/php/php.ini

so you have some host, running 3 containers…

where are u issuing the curl command? host?

where do you THINK example.com is? off your host? or in one of the containers?

if in one of the containers, how did the containers ip (only accessible from inside the docker host) get mapped to that dns entry? docker does not do that… docker compose does not do that… compose creates dynamic entries for THAT COMPOSE FILE only…the hose does not know

from PHP container curl localhost as example.com, I can ping but not curl it!
have a look to docker-compose https://pastebin.com/fa7d3YVY

What Sam trying to get you to understand is that Docker Compose only has an INTERNAL name server that uses the service/container name and aliases to resolve links and ip addresses within docker. Any other DNS entries has to be mapped. It is possible to add the example.com as an alias on the container you want to reach from it. But when doing local development, you most often creates entries in your local host file that points to localhost of your docker ip.

Maybe I did not address my issue in the right way.
if you check my docker-compose, you will see the aliases. Now from php.dock I can ping nginx.dock, the nslookup can resovles the name and returns the IP, and curl -I nginx.dock also works fine.

The site domain name is lara.devi, I can ping & resolve it from all the containers but I can not curl it from the containers, only from the host.

Should I change the alias to the domain name?

Thanks,

1 Like

I solved, but I don’t know if this is the right way. The Nginx container IP 172.22.1.5, so in the PHP container /etc/hosts the domain name point to 172.22.1.5.

The issue after restart I need to re-add it again.

Finally, I found the right solution. in PHP container I added:

extra_hosts:
  - "lara.devi:172.22.1.5"

This added to /etc/hosts.

1 Like