Home > Net >  Docker Container cant connect to other container - nginx in alpine to nginx in alpine
Docker Container cant connect to other container - nginx in alpine to nginx in alpine

Time:10-27

I'm confused about making a connection from nginx alpine to nginx alpine

both use laravel 9

on the host I can access both using http://localhost:8080 and http://localhost:5001

but when I try to use guzzle in frontend like this

$response = $http->get('http://dashboard:5001');

result is

cURL error 7: Failed to connect to dashboard_service port 5001 after 0 ms: 
Connection refused 

and I try to curl from frontend container to dashboard container the result is connection refused.I can ping it, but curl not work

this is my docker-compose.yml

version: "3.8"

networks:
  networkname:

services:
  frontend:
    build:
      context: .
      dockerfile: ./file/Dockerfile
    container_name: frontend
    ports:
      - 8080:80
    volumes:
      - ./frontend:/code
      - ./.docker/php-fpm.conf:/etc/php8/php-fpm.conf
      - ./.docker/php.ini-production:/etc/php8/php.ini
      - ./.docker/nginx.conf:/etc/nginx/nginx.conf
      - ./.docker/nginx-laravel.conf:/etc/nginx/modules/nginx-laravel.conf
    networks:
      - networkname

  dashboard:
    build:
      context: .
      dockerfile: ./file/Dockerfile
    container_name: dashboard
    ports:
      - 5001:80
    volumes:
      - ./dashboard:/code
      - ./.docker/php-fpm.conf:/etc/php8/php-fpm.conf
      - ./.docker/php.ini-production:/etc/php8/php.ini
      - ./.docker/nginx.conf:/etc/nginx/nginx.conf
      - ./.docker/nginx-laravel.conf:/etc/nginx/modules/nginx-laravel.conf
    networks:
      - networkname

this is my dockerfile

FROM alpine:latest

WORKDIR /var/www/html/

# Essentials
RUN echo "UTC" > /etc/timezone
RUN apk add --no-cache zip unzip curl sqlite nginx supervisor

# Installing PHP
RUN apk add --no-cache php8 \
    php8-common \
    php8-fpm \

# Installing composer
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN rm -rf composer-setup.php

# Configure supervisor
RUN mkdir -p /etc/supervisor.d/
COPY .docker/supervisord.ini /etc/supervisor.d/supervisord.ini

# Configure PHP
RUN mkdir -p /run/php/
RUN mkdir -p /test
RUN touch /run/php/php8.0-fpm.pid

CMD ["supervisord", "-c", "/etc/supervisor.d/supervisord.ini"]

this is my nginx conf

server {
listen 80;
server_name localhost;
root /code/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index index.php;

charset utf-8;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}


location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
    fastcgi_pass localhost:9000;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ /\.(?!well-known).* {
    deny all;
}
}

I'm confused about having to set it up in docker, nginx or alpine linux

Thanks.

CodePudding user response:

Hello if you try this from inside container's php you needn't check it with port

Type such from php:


$response = $http->get('http://dashboard'); 

But, if you check this from out container you may enter like ip:port


$response = $http->get('http://127.0.0.1:5001'); 

CodePudding user response:

If you are connecting to a docker container within the same network, then use the internal port. If not, use the external port.

In your case, you are trying to connect to the dashboard container from within networkname network. So try http://dashboard instead of http://dashboard:5001

  • Related