如何将作曲家与 docker-compose 一起使用?

2022-08-31 01:19:43

我正在配置文件,我想运行一个包含 Elastic、Redis、Symfony 和 composer 的 PHP 堆栈。docker-compose.yml

现在我遇到的问题是,我不知道如何将 composer 与 docker 一起使用,因为 composer 的某些功能需要有 PHP 和一些扩展。我不想构建一个新的镜像,并在其上安装Nginx和PHP以及PHP的作曲家和扩展,我不会将它们全部放在不同的映像中。

到目前为止,我尝试过的是这样的:

version : '2'

services:
  nginx:
    image: tutum/nginx
    ports:
        - "80:80"
    volumes:        
        - ./nginx/default:/etc/nginx/sites-available/default
        - ./nginx/default:/etc/nginx/sites-enabled/default
        - ./logs/nginx-error.log:/var/log/nginx/error.log
        - ./logs/nginx-access.log:/var/log/nginx/access.log
        - ./app:/usr/share/nginx/html

  phpfpm:
      image: php:fpm
      ports:
          - 9000:9000
      volumes:      
          - ./app:/usr/share/nginx/html
          
  composer:
      image: composer/composer:php7
      command: install
      volumes: 
        - ./app:/app

  elastic2.4.4:
    image: elasticsearch:2.4.4    
    ports:
      - 9200:9200
    volumes:
      - ./esdata1:/usr/share/elasticsearch/data

  redis:
    image: redis:3.2
    ports:
      - 6379:6379

但这不会安装依赖项。


答案 1

我设置了我的文件,以便一个 docker 实例将使用该映像并在共享容器中执行。然后,所有其他映像将能够访问作曲家创建的供应商目录。棘手的部分是意识到图像假设文件将位于目录中。我必须通过将我的共享容器指定为以下情况来覆盖此行为:docker-compose.ymlcomposer/composercomposer installcomposer/composercomposer.json/appworking_dir

version: '3'

services:
  #=====================#
  # nginx proxy service #
  #=====================#
  nginx_proxy:
    image: nginx:alpine
    networks:
      - test_network
    ports:
      - "80:80"
      - "443:443"
    volumes:
      # self-signed testing wildcard ssl certificate
      - "./certs:/certs"
      # proxy needs access to static files
      - "./site1/public:/site1/public"
      - "./site2/public:/site2/public"
      # proxy needs nginx configuration files
      - "./site1/site1.test.conf:/etc/nginx/conf.d/site1.test.conf"
      - "./site2/site2.test.conf:/etc/nginx/conf.d/site2.test.conf"
    container_name: nginx_proxy

  #===============#
  # composer.test #
  #===============#
  composer.test:
    image: composer/composer
    networks:
      - test_network
    ports:
      - "9001:9000"
    volumes:
      - "./composer:/composer"
    container_name: composer.test
    working_dir: /composer
    command: install

  #============#
  # site1.test #
  #============#
  site1.test:
    build: ./site1
    networks:
      - test_network
    ports:
      - "9002:9000"
    environment:
      - "VIRTUAL_HOST=site1.test"
    volumes:
      - "./composer:/composer"
      - "./site1:/site1"
    container_name: site1.test

  #============#
  # site2.test #
  #============#
  site2.test:
    build: ./site2
    networks:
      - test_network
    ports:
      - "9003:9000"
    environment:
      - "VIRTUAL_HOST=site2.test"
    volumes:
      - "./composer:/composer"
      - "./site2:/site2"
    container_name: site2.test

# networks
networks:
  test_network:

以下是目录结构的外观:

certs
    test.crt
    test.key
composer
    composer.json
site1
    app
    public
    Dockerfile
    site1.test.conf
site2
    app
    public
    Dockerfile
    site2.test.conf
docker-compose.yml

答案 2

如果你看一下,那么你会看到,它是基于的,它似乎不包括在内。因此,您可以使用基础映像在其上进行安装。composer/composer:php7Dockerfilephp:7.0-alpinefpmcomposer/composer:php7php-fpm

因此,由于您在所有三个容器中执行项目的映射,因此在一个容器中运行应会导致更改在所有三个容器中都可见。composer install

就我个人而言,我不认为将PHP和nginx分离成2个不同的容器是有意义的,因为一个容器对另一个容器非常可靠。将你的应用映射到两个容器也是无稽之谈的完美示例。这就是为什么我维护自己的nginx + php Docker映像的公共构建。您可以在此处查看。有更多版本具有更多风格。它们里面都有作曲家。


推荐