在 Docker-compose 中提供 angular 个应用程序的 NX monorepo

Serving an NX monorepo of angular apps in Docker-compose

我有一个带有 2 个应用程序的 NX monorepo:

我希望使用 docker-compose 来 运行 我的整个环境,最终有一些 APIs 和数据库等

我创建了一个 docker 文件,它接受参数并且可以重新用于 Nx 中的 运行 多个 angular 应用程序:

# base image
FROM node

ARG APP


# # install chrome for protractor tests
# RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
# RUN apt-get update && apt-get install -yq google-chrome-stable
# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
COPY decorate-angular-cli.js /app/decorate-angular-cli.js
RUN npm install
RUN npm install -g @angular/cli@latest
RUN npm install reflect-metadata tslib rxjs @nestjs/platform-express

# add app
COPY . /app
# start app
CMD npm start $APP -- --host 0.0.0.0 --port 4200 --disableHostCheck=true --poll 100

我创建了一个 docker-compose 文件来设置 .net 5 API 和 2 个网络应用程序:

version: '3.4'

services:
  sletsgo.user.api:
    image: ${DOCKER_REGISTRY-}sletsgo.user.api
    build:
      context: .
      dockerfile: SletsGo.User.Api/Dockerfile
    ports:
      - "5000:2000"
      - "444:443"

  sletsgo.shop:
    container_name: 'SletsGo.Shop'
    image: ${DOCKER_REGISTRY}sletsgo.shop:dev
    build:
        context: SletsGo
        dockerfile: .docker/Dockerfile
        args:
            - APP=shop
    ports:
        - '4000:4200'
    #volumes:
    #    - sletsgo-web:/app
    #    - '/app/node_modules'

  sletsgo.landing:
    container_name: 'SletsGo.Landing'
    image: ${DOCKER_REGISTRY}sletsgo.landing:dev
    build:
        context: SletsGo
        dockerfile: .docker/Dockerfile
        args:
            - APP=landing
    ports:
        - '4100:4200'
  #  volumes:
  #      - sletsgo-web:/app
  #      - '/app/node_modules'

 


#volumes:
# sletsgo-web:
#    driver: local
#    driver_opts:
#      type: local
#      device: ./SletsGo
#      o: bind

请注意,很多行都被注释掉了。 问题是如果我 运行 这个 docker-compose 我得到商店应用程序的 2 个实例 运行.

我该如何解决这个问题?

P.S。我还想象使用卷可以大大简化事情。但是我尝试安装失败,因为我在 windows 机器上使用 linux 容器。如果有人愿意帮助我达到下一个级别,我非常希望,但现在我只想 运行 两个 Web 应用程序。

TL;DR

ARG's are build time variables. You can persist the ARG's value in the images environment during the build or override the services[*].command in the docker-compose.yaml.


坚持环境

您可以在构建期间在图像环境中保留来自 --build-arg 的环境变量集:

FROM alpine:3
ARG APP
ENV APP=${APP}
CMD echo $APP

构建 docker 图像:docker build --rm --build-arg APP=123 -t so:66935830 .

运行 docker 容器:docker run --rm -it so:66935830

覆盖 command

您可以覆盖 docker-compose.yaml 中每个服务的 command

version: '3.7'
services:
  a:
    image: 'a'
    build:
      context: '.'
    command: ['echo', '123']
  b:
    image: 'b'
    build:
      context: '.'
    command: ['echo', '456']