dongyihang3575
dongyihang3575
2017-11-16 18:16
浏览 109
已采纳

如何在与docker-compose链接到另一个容器的容器中运行linux守护进程?

I have the following docker-compose.yml file which runs nginx with PHP support:

version: '3'
services:
  nginx:
    container_name: my-app-nginx
    image: nginx:1.13.6
    ports:
      - 8080:80
    volumes:
      - ./nginx-default.conf:/etc/nginx/conf.d/default.conf
      - ./my-app:/var/www/my-app
    restart: always
    depends_on:
      - php
  php:
    container_name: my-app-php
    image: php:7.1-fpm
    volumes:
      - ./my-app:/var/www/my-app
    restart: always

The PHP application inside /var/www/my-app needs to communicate with a linux daemon (let's call it myappd).

The way I see it, I need to either:

  1. Copy the myappd into the nginx container to /usr/local/bin, make it executable with chmod +x and run it in the background.
  2. Create a different container, copy myappd to /usr/local/bin, make it executable with chmod +x and run it in the foreground.

Now, I'm new to Docker and I'm researching and learning about it but my best guess, given that I'm using Docker Composer, is that option 2 is probably the recommended one? Given my limited knowledge about Docker, I'd have to guess that this container would require some sort of linux-based image (like Ubuntu or something) to run this binary. So maybe option 1 is preferred? Or maybe option 2 is possible with a minimal Ubuntu image or maybe it's possible without such image?

Either way, I have no idea how would I implement that on the composer file. Especially option 2, how would the PHP application communicate with the daemon in a different container? Just "sharing" a volume (where the binary is located) like I did for nginx/php services would suffice? Or something else is required?

图片转代码服务由CSDN问答提供 功能建议

我有以下 docker-compose.yml 文件,该文件在PHP支持下运行nginx:

  version:'3'
services:
 nginx:
 container_name:my-app-nginx 
 image:nginx:1.13.6 
 ports:
  -  8080:80 
卷:
  -  ./nginx-default.conf:/etc/nginx/conf.d/default.conf
  -  ./my-app:/var/www/my-app\  restart:always 
 depends_on:
  -  php 
 php:
 container_name:my-app-php 
 image:php:7.1-fpm 
卷:
  -  ./my-app:/var/www  / my-app 
 restart:always 
   
 
 

/ var / www / my-app 中的PHP应用程序需要与 linux守护进程(我们称之为 myappd )。

我看到它的方式,我需要:

    \ n
  1. myappd 复制到 nginx 容器中,转到 / usr / local / bin ,用 chmod + x使其可执行 并在后台运行。
  2. 创建一个不同的公司 ntainer,将 myappd 复制到 / usr / local / bin ,使用 chmod + x 使其可执行,并在前台运行。

    现在,我是Docker的新手,我正在研究和学习它,但我最好的猜测是,鉴于我使用的是Docker Composer,选项2是 可能推荐的一个? 鉴于我对Docker的了解有限,我不得不猜测这个容器需要某种基于linux的图像(比如Ubuntu或其他东西)来运行这个二进制文件。 那么选项1是首选吗? 或者也许选项2可以使用最小的Ubuntu图像,或者可能没有这样的图像?

    无论哪种方式,我都不知道如何在composer文件上实现它。 特别是选项2,PHP应用程序如何与另一个容器中的守护进程通信? 只是“共享”一个卷(二进制文件所在的位置)就像我为nginx / php服务所做的那样就够了吗? 还是需要其他东西?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbi8089
    duanbi8089 2017-11-16 20:51
    已采纳

    Simple answer is adding command entry to php service in docker-compose.yml.

    Given that myappd is at ./my-app/ on host machine and at /var/www/my-app/, updated docker-compose.yml is something like following.

    version: '3'
    services:
      nginx:
        container_name: my-app-nginx
        image: nginx:1.13.6
        ports:
           - 8080:80
        volumes:
          - ./nginx-default.conf:/etc/nginx/conf.d/default.conf
          - ./my-app:/var/www/my-app
        restart: always
        depends_on:
           - php
      php:
        container_name: my-app-php
        image: php:7.1-fpm
        volumes:
          - ./my-app:/var/www/my-app
        restart: always
        command: ["/bin/sh", "/var/www/my-app/mappd", "&&", "php-fpm"]
    

    Better answer is to create the third container which runs linux daemon.

    New Dockerfile is something like following.

    FROM debian:jessie
    
    COPY ./myappd /usr/src/app/
    
    EXPOSE 44444
    
    ENTRYPOINT ['/bin/sh']
    CMD ['/usr/src/app/myappd']
    

    Build image and name it myapp/myappd.

    Updated docker-compose.yml is something like following.

    version: '3'
    services:
      nginx:
        container_name: my-app-nginx
        image: nginx:1.13.6
        ports:
           - 8080:80
        volumes:
          - ./nginx-default.conf:/etc/nginx/conf.d/default.conf
          - ./my-app:/var/www/my-app
        restart: always
        depends_on:
           - php
      php:
        container_name: my-app-php
        image: php:7.1-fpm
        volumes:
          - ./my-app:/var/www/my-app
        restart: always
        networks:
          - network1
        depends_on:
           - daemon
      daemon:
        container_name: my-app-daemon
        image: myapp/myappd
        restart: always
        networks:
          - network1
    networks:
      network1:
    

    You can send request with hostname daemon from inside php. Docker container has capability to resolve hostname of another container in the same network.

    点赞 评论

相关推荐