docker - 容器运行后如何将容器目录挂载到另一个目录中?

标签 docker nginx mount

我有一个 Angular 应用程序,该应用程序是在容器运行时生成的,如Dockerfile所示,它是由两个图像构建的。

FROM node:10.15.3-alpine as builder

RUN mkdir -p /usr/src/banax_education_platform
WORKDIR /usr/src/banax_education_platform

RUN apk add git
COPY package*.json /usr/src/banax_education_platform/
RUN npm i

COPY . /usr/src/banax_education_platform

RUN npm i -g @angular/cli && npm run-script build


FROM nginx:1.13.12-alpine

RUN rm -rf /usr/share/nginx/html/*

COPY /nginx/default.conf /etc/nginx/conf.d/default.conf

COPY --from=builder /usr/src/banax_education_platform/dist/edu-app /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

因此,首先,它获取节点图像并根据所有要求构建应用程序。然后获取另一张nginx图像并创建服务器块以显示该应用程序。现在,我想拆分两个图像,因此一个容器包含 Angular 应用程序,第二个容器包含nginx反向代理。

想到了这个docker-compose.yml
version: "2"
services:
 nginx:
  build: ./nginx
  container_name: angular-nginx
  ports:
   - "80:80"
  volumes:
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  depends_on:
   - web
 web:
  build: ./angular
  container_name: angular-app
  ports:
   - "8000"

但是问题是,我想要这个任务:COPY --from=builder /usr/src/banax_education_platform/dist/edu-app /usr/share/nginx/html是通过docker-compose完成的,因为现在我有2个Dockerfile,一个用于angular,一个用于nginx,所以我不知道如何将该 Angular 容器的目录与nginx容器链接。有人有主意吗?

最佳答案

在典型的Web应用程序中,您运行某种打包程序(例如Webpack),将您的应用程序“编译”为一组静态文件。然后,您需要一些Web服务器将这些文件发送给客户端,但是一旦构建,就不会在服务器端运行该代码。

这与您在问题中显示的多阶段Docker镜像非常匹配。第一阶段仅构建 Artifact ,一旦完成,就不会运行构建的图像。 (例如,注意缺少CMD。)第二阶段COPY --from=build是它构建的静态 Artifact ,仅此而已。

由于没有实际的应用程序在第一阶段中运行,因此将其作为单独的Docker Compose服务运行或以您所显示的方式“拆分”为没有意义。

如果您不介意使用多个构建工具,则可以做的一件事是从Nginx的 Angular 将打包的应用程序视为“数据”(它不了解这些文件的内容,它只是发送网络上的数据)。您可以在主机上选择一个目录,并将其挂载到Nginx容器中

version: "3"
services:
  nginx:
    image: nginx:1.13.12-alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./dist:/usr/share/nginx/html

然后在主机系统上需要重建和重新部署应用程序时使用npm run-script build

关于docker - 容器运行后如何将容器目录挂载到另一个目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56336105/

相关文章:

amazon-web-services - AWS EB、Play Framework 和 Docker : Application Already running

node.js - 无法访问本地主机:8080 with docker on windows 10

docker - 如何为dockerized Rasa-NLU配置要公开的端口

linux - 在 docker 容器中挂载 linux 镜像

docker mount nfs with local_lock=all

cocoa - 如何让我的应用程序挂载稀疏包而不在 Finder 中显示它?

django - 无法使用 Docker 数据库 : 'could not translate host name "db"to address: Name or service not known' 在 VS Code 中调试 Django 应用程序

docker - 为docker-compose Web应用程序使用与localhost不同的主机名/URL

php - 在信号 11 xhprof 上退出

nginx - 使用 nginx-rtmp-module 将 rtmp 流保存到 mp4 而不是 flv