简体   繁体   中英

How to access another containers while running app in `nginx` container in Docker?

I am dockerizing laravel (lumen) app locally on Mac computer.

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:
      - ./:/home
    ports:
      - "3000:3000"
    restart: unless-stopped

  #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

As you see in yml file I am running my app in nginx container and everything works fine.

But when I try to run command:

docker exec libreoffice soffice --headless --invisible --convert-to pdf --outdir "home/public/tmp" "home/public/tmp/hi.docx"

in my application, it throws the following error:

sh: 1: docker: not found

After wasting days I thing that it is trying to find docker in nginx container not on my local computer. Means all other services I have defined in docker-compose.yml file can not be accessed in my application because my application = nginx container . But why? What should I do then? How should I create environment to access another services in my application?

MY BIG QUESTION

Why it is even running whole app in container? When I run app with nginx, then my app breaks the connection with my local environment and trying to find all other containers in nginx container. For example if I need to convert some files and for that convertation I need libreoffice service to run in background. And when I try to connect it with soffice --headless --invisible --convert-to pdf --outdir command it throws an error like:

sh: 1: soffice: not found

Because it is looking for soffice in nginx container not in my local docker at all. If that is the case then How can I even run my application with nginx? Do I need to run all other containers in nginx container ? How is it possible?

After making some more research I have found that it is impossible to access to the container from another container and run some command inside.

Some solutions to that problem:

  1. Use service's REST API to connect.

  2. If your service doesn't have REST API to access and run it

    1. Removing libreoffice service as container and installing it to php container with linux command in Dockerfile :

       RUN apt-get update && apt-get install -y libreoffice
    2. Command can be run remotely using ssh. Check this topic (I don't recommend)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM