2

I have build docker environment for my laravel application. I am running app in nginx container with php-fpm configured.

My question is how can I access another containers that I have defined in docker-compose.yml from nginx container?

For example I want to connect libreoffice container that I have created in docker, from my app which is running in nginx container. If I run some command in my app it is getting executed in nginx container.

Let's say I execute soffice ... it will throw an error that soffice not found. Because soffice is not installed in nginx container. It is installed as docker image.

Then How can I access to libreoffice container from nginx container? Should I install libreoffice service to nginx container in Dockerfile or run 2 services in single container? I am really confused

Edit:

docker-compose.yml:

version: "3.9"

services:
  # LibreOffice Service
  libreoffice:
    image: lscr.io/linuxserver/libreoffice:latest
    container_name: libreoffice
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/London
    volumes:
      - ./:/var/www
    ports:
      - "3000:3000"
    restart: unless-stopped
    networks:
      - app-network

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WordToPdfController extends Controller {

    public function convert(Request $request) {
        $this->validate($request, [
            'document' => 'required|file|mimes:docx,doc'
        ]);

        $document = $request->file('document');

        $extension = $request->file('document')->getClientOriginalExtension();
        $fileName = str_replace(".$extension",'', $document->getClientOriginalName());

        $document->move("tmp", $document->getClientOriginalName());





        // Main part is here



        $command = "soffice --headless --invisible --convert-to pdf --outdir \"tmp\" \"tmp/$fileName.$extension\"";
        $result = $this->exec($command);






        sleep(1);

        if(!empty($result['stderr'])) {
            throw new \Exception("tmp/$fileName.$extension: ".$result['stderr']);
        }

        if(!@filesize('tmp/'.$fileName.'.pdf')) {
            $this->convert($request);
        }

        header('Content-Disposition: attachment; filename="' . $fileName . '.pdf"');
        header("Content-length: " . filesize('tmp/'.$fileName.'.pdf'));
        header("Pragma: no-cache");
        header("Expires: 0");

        flush();
        readfile('tmp/'.$fileName.'.pdf');
        unlink('tmp/'.$fileName.'.pdf');
        unlink('tmp/'.$fileName.'.'.$extension);
        exit;
    }
}

I am able to run application but when is comes to running command it throws error that:

`soffice not found`

Because the command getting running in nginx container and there is not soffice installed. Soffice (which is libreoffice service) running as another container in docker. Then how can I run the command in nginx container to access libreoffice service that is running on same host with port 3000 (check docker-compose.yml file)?

John Kage
  • 74
  • 5
  • What is the actual issue you're trying to solve? Why do you need to start apps in a container manually and moreover from another container? – Alexey R. Oct 05 '22 at 06:31
  • each container/service is like a separate computer. You can't run programs on a different computer just by invoking them. They need to expose an API that you can reach through a call to IP:PORT. And then you can make an appropriate call to that api and get the "remote" service to do what you want it to do. – Mihai Oct 05 '22 at 06:33
  • @Mihai Yes you are right. If libreoffice doesn't have any `API` then in my case it is impossible to do with different containers yes? I should actually install this service in `nginx` container and run command there. – John Kage Oct 05 '22 at 08:00
  • 1
    You can run command remotely using ssh. You only have to make sure that you have established some extra stuff for your containers (keys, etc). Check [this topic](https://stackoverflow.com/questions/18502945/how-to-execute-a-remote-command-over-ssh-with-arguments) regarding calling remote command. – Alexey R. Oct 05 '22 at 08:07
  • 1
    I wouldn't go the ssh way. it'a a dirty workaround. Instead try to search the web for "libreoffice rest api". I see there are quite a few implementations. maybe you find something. I also see already made docker images for libreoffice - check how they are used. – Mihai Oct 05 '22 at 08:26

0 Answers0