0

Issue: Any URL put in creating a PDF, MPDF can't find it (throws an Exception).

Exception: cURL error: \"Failed to connect to 127.0.0.1 port 8000: Connection refused\"

I have both web and API in this app, and strangely, my web app HTML can find those images. I have created a pretty basic environment following the tutorials.

I'm using http://127.0.0.1:8000/ as base URL to access for my app and API.

  • Images is located at ROOT_DIR/public/uploads/report_templates/logo/logo-white.png
  • Web can see this via http://127.0.0.1:8000/uploads/report_templates/logo/logo-white.png.

Things I found

  • Any external URL like https://www.google.pl/images/srpr/logo11w.png can be found.
  • If I place absolute path instead of URL it is found (can't resort to this tough since this a lengthy report.)
  • using curl from inside the container for http://127.0.0.1:8000/uploads/report_templates/logo/logo-white.png says Failed to connect to 127.0.0.1 port 8000: Connection refused

I understand, I'm doing it wrong in the 3rd point requesting into my own container. But can't find anything specific in web & too new to docker to think of something my own.

I'm looking for a proper way to handle this in docker.

.env

   ...
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://127.0.0.1:8000/
   ...

docker-compose.yml

version: "3.7"
services:
  app:
    build:
      args:
        user: daniyalnasir
        uid: 1001
      context: ./
      dockerfile: Dockerfile
    image: f-scope
    container_name: f-scope-app
    restart: unless-stopped
    working_dir: /var/www/
    volumes:
      - ./:/var/www
    networks:
      f-scope:


  db:
    image: mysql:5.7
    container_name: f-scope-db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
#      MYSQL_USER: ${DB_USERNAME}
#      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata1:/var/lib/mysql
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
    networks:
      f-scope:
        ipv4_address: 172.24.2.1
  nginx:
    image: nginx:alpine
    container_name: f-scope-nginx
    restart: unless-stopped
    ports:
      - 8000:80
    volumes:
      - ./:/var/www
      - ./public/uploads:/var/www/public/uploads
      - ./docker-compose/nginx:/etc/nginx/conf.d/
    networks:
      f-scope:


networks:
  f-scope:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: "172.24.2.0/16"

volumes:
  dbdata1:

Dockerfile

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libgd-dev \
    libzip-dev \
    zip \
    sendmail \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip


# Configure sendmail path
RUN echo "sendmail_path=/usr/sbin/sendmail -t -i" >> /usr/local/etc/php/conf.d/sendmail.ini

#RUN sed -i '/#!\/bin\/sh/aservice sendmail restart' /usr/local/bin/docker-php-entrypoint
#RUN sed -i '/#!\/bin\/sh/aecho "$(hostname -i)\t$(hostname) $(hostname).localhost" >> /etc/hosts' /usr/local/bin/docker-php-entrypoint

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

HTML

<img src="{{url("uploads/report_templates/logo/logo-white.png")}}" style="width: 250px; height:80px; margin: 70px 0px 40px 30px;" alt="" />

For context

I'm using MPDF to create a PDF that has images URLs (pointing to my application's public folders). It works fine on my regular LAMP environment.

But, to learn I decided to create & run this app in docker container on my local machine (running ubuntu). Everything is working as it should in this docker environment, except for this one thing.

1 Answer 1

0

When utilizing Docker in development environments, it's crucial to appropriately configure the basepath used for relative src and href attributes to ensure accessibility. In the context you provided, Mpdf is attempting to access an image via http://yourapp.test:8000/uploads/report_templates/logo/logo-white.png, yet the nginx container fails to resolve the host (yourapp.test) and establish a connection on port 8000.

Therefore, the basepath should be configured as either "localhost", "127.0.0.1", or the local IP address of the container.

$mpdf = new Mpdf();
$mpdf->SetBasePath('http://localhost');

In cases where the nginx container operates on a port other than 80, it's necessary to specify the port:

// For instance, if the nginx container runs on port 8080.
$mpdf->SetBasePath('http://localhost:8080');

However, it's important to note that this configuration ideally shouldn't be required in production since basepath should be appropriately configured and accessible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.