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)?