如何在 docker compose 中将主机目录挂载为卷 [英] How do I mount a host directory as a volume in docker compose

查看:141
本文介绍了如何在 docker compose 中将主机目录挂载为卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在 dockerizing 的开发环境,我希望能够实时重新加载我的更改,而无需重新构建 docker 镜像.我正在使用 docker compose 因为 redis 是我的应用程序的依赖项之一,我喜欢能够链接 redis 容器

我在 docker-compose.yml 中定义了两个容器:

节点:构建:./节点链接:- Redis端口:- 8080"环境文件:- 节点-app.envRedis:图片:redis端口:- 6379"

我已经在我的 node 应用程序的 dockerfile 中添加了一个卷,但是如何在卷中安装主机的目录,以便我对代码的所有实时编辑都是反映在容器中?

这是我当前的 Dockerfile:

# 设置基础镜像为Ubuntu从节点:硼# 文件作者/维护者维护者 Amin Shah Gilani # 安装 nodemon运行 npm install -g nodemon# 添加一个/app 卷音量 [/app"]# TODO:链接当前的 .到/应用程序#定义工作目录工作目录/应用程序# 运行 npm install运行 npm 安装# 暴露端口曝光 8080# 使用 nodemon 运行应用程序CMD [nodemon",/app/app.js"]

我的项目如下所示:

<预><代码>/- docker-compose.yml- 节点-app.env- 节点/- 应用程序.js- Dockerfile.js

解决方案

查看他们的 文档

从它的外观来看,您可以在 docker-compose.yml 上执行以下操作

卷:- ./:/应用程序

其中 ./ 是宿主目录,/app 是容器的目标目录.

<小时>
以前的文档源现在会指向版本历史记录,您必须选择您正在使用的 compose 版本并查找参考.

对于懒人 – v3/v2/v1

附注:自本次编辑起,所有版本的语法都保持不变

I have a development environment I'm dockerizing and I would like the ability to livereload my changes without having to rebuild docker images. I'm using docker compose because redis is one of my app's dependencies and I like being able to link a redis container

I have two containers defined in my docker-compose.yml:

node:
  build: ./node
  links:
    - redis
  ports:
    - "8080"
  env_file:
    - node-app.env

redis:
  image: redis
  ports:
    - "6379"

I've gotten to the point in my node app's dockerfile where I add a volume, but how do I mount the the host's directory in the volume so that all my live edits to the code are reflected in the container?

Here's my current Dockerfile:

# Set the base image to Ubuntu
FROM    node:boron

# File Author / Maintainer
MAINTAINER Amin Shah Gilani <amin@gilani.me>

# Install nodemon
RUN npm install -g nodemon

# Add a /app volume
VOLUME ["/app"]

# TODO: link the current . to /app

# Define working directory
WORKDIR /app

# Run npm install
RUN npm install

# Expose port
EXPOSE  8080

# Run app using nodemon
CMD ["nodemon", "/app/app.js"]

My project looks like this:

/
- docker-compose.yml
- node-app.env
- node/
  - app.js
  - Dockerfile.js

解决方案

Checkout their documentation

From the looks of it you could do the following on your docker-compose.yml

volumes:
    - ./:/app

Where ./ is the host directory, and /app is the target directory for the containers.


EDIT:
Previous documentation source now leads to version history, you'll have to select the version of compose you're using and look for the reference.

For the lazy – v3 / v2 / v1

Side note: Syntax remains the same for all versions as of this edit

这篇关于如何在 docker compose 中将主机目录挂载为卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆