【问题标题】:How to use single docker compose to build containers in different directories如何使用单个 docker compose 在不同目录中构建容器
【发布时间】:2020-02-23 14:08:08
【问题描述】:

我想在 docker-compose up 时启动两个容器

我有两个文件夹 API 和 front。每个都有一个 Dockerfile 和 docker-compose 文件。目前,我必须为每个应用程序做 docker-compose up。 如下所示

- api
 - docker-compose.yml
 - Dockerfile
- front
 - docker-compose.yml
 - Dockerfile

我想要一个 docker-compose.yml 来管理两个容器,如下所示。

- docker-compose.yml
- api
 - Dockerfile
- front
 - Dockerfile

api docker-compose 版本:'3'

services: 
    api:
        build: .
        command: pipenv run start
        image : data-tracker-backend
        volumes: 
            - .:/api/
        ports: 
            - "8000:8000"

前台 docker-compose 版本:'3'

services: 
    web:
        build: .
        command: npm start
        image : data-tracker-front
        volumes: 
            - .:/front/
        ports: 
            - "5000:5000"

我想要类似的东西

version: '3'
    services: 
        api:
            build: .
            command: pipenv run start
            image : data-tracker-backend
            volumes: 
                - .:/api/
            ports: 
                - "8000:8000" 
        front:
            build: .
            command: npm start
            image : data-tracker-front
            volumes: 
                - .:/front/
            ports: 
                - "5000:5000"

帮助从不同的工作目录访问命令。

【问题讨论】:

  • build: 目录和volumes: 的前半部分相对于包含docker-compose.yml 的目录。 (但我建议删除 volumes:command: 以支持 Docker 映像中内置的内容。)

标签: docker docker-compose dockerfile


【解决方案1】:

您可以更改构建目录和卷一。

version: '3'
  services:
    api:
      build: ./api
      volumes:
        - ./api:/api
      # the rest of your commands...

    front:
      build: ./front
      volumes:
        - ./front:/front
      # the rest of your commands...

我的建议是阅读指南。那里有很多informationtutorials

【讨论】:

    【解决方案2】:

    你可以尝试使用buildcontextdockerfile如下图

    - docker-compose.yml
    - api
     - Dockerfile
    - front
     - Dockerfile
    
    version: '3'
      services:
        api:
          build:
            context: api
            dockerfile: Dockerfile
          command: pipenv run start
          image : data-tracker-backend
          volumes:
            - .:/api/
          ports:
            - "8000:8000"
        front:
          build:
            context: front
            dockerfile: Dockerfile
          command: npm start
          image : data-tracker-front
          volumes:
            - .:/front/
          ports:
            - "5000:5000"
    

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2020-08-06
      • 1970-01-01
      • 2022-11-13
      • 2019-11-03
      相关资源
      最近更新 更多