首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Docker中运行‘`nginx`’容器中的应用程序时访问另一个容器?

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

Stack Overflow用户
提问于 2022-10-04 11:43:37
回答 1查看 136关注 0票数 0

我正在Mac电脑上本地制作laravel (流明)应用程序。

docker-compose.yml:

代码语言:javascript
复制
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容器中运行我的应用程序,一切都很好。

但当我试图运行命令时:

代码语言:javascript
复制
docker exec libreoffice soffice --headless --invisible --convert-to pdf --outdir "home/public/tmp" "home/public/tmp/hi.docx"

在我的应用程序中,它引发以下错误:

代码语言:javascript
复制
sh: 1: docker: not found

在浪费了几天时间之后,我发现它在nginx容器中寻找nginx,而不是在我的本地计算机上。意味着我在应用程序中定义的所有其他服务都不能在我的应用程序中访问,因为my application = nginx container.但是为什么呢?那我该怎么办呢?如何创建环境以访问应用程序中的其他服务?

我的大问题

为什么它甚至在容器中运行整个应用程序?当我使用nginx运行app时,我的应用程序断开了与本地环境的连接,并试图在nginx容器中查找所有其他容器。例如,如果我需要转换一些文件,为了进行转换,我需要libreoffice服务在后台运行。当我尝试用soffice --headless --invisible --convert-to pdf --outdir命令连接它时,它会抛出一个错误,如下所示:

代码语言:javascript
复制
sh: 1: soffice: not found

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-07 11:16:26

经过进一步的研究,我发现从另一个容器访问容器并在内部运行一些命令是不可能的

解决这一问题的一些办法:

  1. 使用服务的REST API连接.

  1. 如果您的服务没有REST API来访问并运行

代码语言:javascript
复制
1. Removing `libreoffice` service as container and installing it to `php` container with linux command in `Dockerfile`:

运行apt-get更新&& apt-获取安装-y libreoffice

代码语言:javascript
复制
2. Command can be run remotely using ssh. Check [this](https://stackoverflow.com/questions/18502945/how-to-execute-a-remote-command-over-ssh-with-arguments) topic (I don't recommend)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73947372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档