如何在 Docker 中的 `nginx` 容器中运行应用程序时访问另一个容器?

问题描述 投票:0回答:1

我正在 Mac 计算机上本地 Dockerizing laravel (lumen) 应用程序。

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

正如您在

yml
文件中看到的那样,我正在
nginx
容器中运行我的应用程序,一切正常。

但是当我尝试运行命令时:

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

在我的应用程序中,它抛出以下错误:

sh: 1: docker: not found

在浪费了几天时间后,我认为它试图在

docker
容器中而不是在我的本地计算机上找到
nginx
。意味着我在
docker-compose.yml
文件中定义的所有其他服务无法在我的应用程序中访问,因为
my application
=
nginx container
。但为什么?那我该怎么办?我应该如何创建环境来访问应用程序中的其他服务?

我的大问题

为什么它甚至在容器中运行整个应用程序?当我使用 nginx 运行应用程序时,我的应用程序会断开与本地环境的连接,并尝试查找 nginx 容器中的所有其他容器。例如,如果我需要转换一些文件,并且对于该转换,我需要

libreoffice
服务在后台运行。当我尝试使用
soffice --headless --invisible --convert-to pdf --outdir
命令连接它时,它会抛出如下错误:

sh: 1: soffice: not found

因为它正在

soffice
容器中寻找
nginx
,而不是在我本地的
docker
中。如果是这样的话,我该如何使用 nginx 运行我的应用程序呢?我需要在
nginx container
中运行所有其他容器吗?怎么可能?

php laravel docker docker-compose dockerfile
1个回答
0
投票

经过更多研究后,我发现从另一个容器访问该容器并在其中运行某些命令是“不可能”的。 该问题的一些解决方案:

    使用服务的
  1. REST API

    进行连接。

    
    

  2. 如果您的服务没有
  3. REST API

    来访问和运行它

    
    

      删除
    1. libreoffice

      服务作为容器,并使用

      php
      中的linux命令将其安装到
      Dockerfile
      容器:
      RUN apt-get update && apt-get install -y libreoffice
      

    2. 可以使用 ssh 远程运行命令。检查
    3. 这个

      主题(我不推荐)

© www.soinside.com 2019 - 2024. All rights reserved.