【问题标题】:How to include volumes in my docker configuration?如何在我的 docker 配置中包含卷?
【发布时间】:2019-04-13 13:32:43
【问题描述】:

我有一个运行正常的 MEAN 应用程序的 docker-compose 配置。

我希望我的 angular (ng serve) 和 express 服务器 (nodemon) 在我按 ctrl + s 时自动重新运行,就好像我在本地运行我的应用程序一样。

为此,我的容器需要知道文件已更改。

我该怎么做?

Angular 的 Dockerfile:

FROM node:10

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . ./

EXPOSE 4200

CMD ["npm", "start"]

Express 的 Dockerfile :

FROM node:6

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . ./

EXPOSE 3000

CMD ["npm", "start"]

docker-compose.yml:

version: '3'

services:
  angular: # name of the first service
    build: client # specify the directory of the Dockerfile
    ports:
      - "4200:4200"

  express: #name of the second service
    build: server # specify the directory of the Dockerfile
    ports:
      - "3000:3000"
    links:
      - database

  database: # name of the third service
    image: mongo
    ports:
      - "27017:27017"

Angular 和 Express 都有一个用于 node_modules 的 .dockerignore

【问题讨论】:

  • 你希望通过在 Docker 中运行它而不是在标准开发环境中运行 npm start 获得什么?
  • 我想学习使用 Docker 和 Kubernetes。我发现在开发过程中使用它是个好主意。否则,我不会有信心在产品中使用它。
  • 在 Docker 之外开发您的应用程序,并使用您显示的 Dockerfile 设置进行部署。虽然您应该可以很容易地找到“标准”Node-in-Docker 模式,但它同时不如仅使用 Node 方便,并且与生产部署根本不同。

标签: docker docker-compose dockerfile


【解决方案1】:

如果您在开发环境中,您可以在您的docker-compose.yml 中添加volumes 部分,如下所示:

services:
  angular: # name of the first service
    build: client # specify the directory of the Dockerfile
    ports:
      - "4200:4200"
   volumes:
      - /path/in/host/machine:/path/in/container

  express: #name of the second service
    build: server # specify the directory of the Dockerfile
    ports:
      - "3000:3000"
    links:
      - database
    volumes:
      - /path/in/host/machine:/path/in/container

  database: # name of the third service
    image: mongo
    ports:
      - "27017:27017"

参考:

volumes in docker-compose

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2019-12-05
    • 2015-07-23
    相关资源
    最近更新 更多