Docker Community Forums

Share and learn in the Docker community.

Cannot curl by domain name, just by IP!


#1

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,


(Ove "Lime" Lindström) #2

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


#3

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

(Sam) #4

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


#5

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


(Ove "Lime" Lindström) #6

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.