如何在 docker compose 中将主机目录挂载为卷
How do I mount a host directory as a volume in docker compose
我有一个正在 docker 开发的开发环境,我希望能够实时重新加载我的更改而无需重建 docker 映像。我正在使用 docker compose 因为 redis 是我的应用程序的依赖项之一,我喜欢能够 link 一个 redis 容器
我在 docker-compose.yml
:
中定义了两个容器
node:
build: ./node
links:
- redis
ports:
- "8080"
env_file:
- node-app.env
redis:
image: redis
ports:
- "6379"
我已经在我的 node
应用程序的 docker 文件中添加了一个卷,但是如何将主机的目录挂载到该卷中以便我所有的实时编辑代码是否反映在容器中?
这是我当前的 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"]
我的项目是这样的:
/
- docker-compose.yml
- node-app.env
- node/
- app.js
- Dockerfile.js
查看他们的 documentation
从外观上看,您可以在 docker-compose.yml
上执行以下操作
volumes:
- ./:/app
其中 ./
是主机目录,/app
是容器的目标目录。
编辑:
以前的文档源现在会导致版本历史记录,您必须select您正在使用的 compose 版本并查找参考。
旁注:自本次编辑起,所有版本的语法都保持不变
有两件事:
我在docker-compose.yml
中添加了卷:
node:
volumes:
- ./node:/app
我将 npm install && nodemon app.js
部分移到了 CMD
中,因为 RUN
向联合文件系统添加了东西,而我的卷不是 UFS 的一部分。
# 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"]
# Define working directory
WORKDIR /app
# Expose port
EXPOSE 8080
# Run npm install
CMD npm install && nodemon app.js
有几个选项
短语法
使用 host : guest
格式,您可以执行以下任一操作:
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
长语法
从 docker-compose v3.2 开始,您可以使用长语法,它允许配置可以用短格式表示的附加字段,例如 mount type
(volume、bind 或 tmpfs ) 和 read_only
.
version: "3.2"
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
networks:
webnet:
volumes:
mydata:
查看 https://docs.docker.com/compose/compose-file/#long-syntax-3 了解更多信息。
如果您想在 Docker Compose YAML 文件的 volumes
部分中将特定主机目录(下例中的 /disk1/prometheus-data
)挂载为卷,您可以如下所示,例如:
version: '3'
services:
prometheus:
image: prom/prometheus
volumes:
- prometheus-data:/prometheus
volumes:
prometheus-data:
driver: local
driver_opts:
o: bind
type: none
device: /disk1/prometheus-data
顺便说一句,在prometheus的Docker文件中,您可能会发现如下VOLUME
指令,它标记它持有来自本地主机的外部挂载的卷等。(但注意:这个尽管将卷装载到容器中,指令不是必须的。):
Docker文件
...
VOLUME ["/prometheus"]
...
参考文献:
我们必须创建您自己的docker卷映射到主机目录 在我们 提及 之前 docker-compose.yml 作为 外部
1.Create 卷名为 share
docker volume create --driver local \
--opt type=none \
--opt device=/home/mukundhan/share \
--opt o=bind share
2.Use 它在你的 docker-compose
version: "3"
volumes:
share:
external: true
services:
workstation:
container_name: "workstation"
image: "ubuntu"
stdin_open: true
tty: true
volumes:
- share:/share:consistent
- ./source:/source:consistent
working_dir: /source
ipc: host
privileged: true
shm_size: '2gb'
db:
container_name: "db"
image: "ubuntu"
stdin_open: true
tty: true
volumes:
- share:/share:consistent
working_dir: /source
ipc: host
这样我们就可以与不同容器中的许多服务运行共享同一个目录
在docker-compose.yml中你可以使用这样的格式:
volumes:
- host directory:container directory
根据他们documentation
这是我的 Node.js 应用程序和 MongoDB 数据库的工作示例:
docker-compose.yml
version: '3'
services:
my-app:
container_name: my-app-container
restart: always
build: .
volumes:
- './storage:/usr/src/app/storage'
ports:
- "3000:3000"
links:
- my-app-db
my-app-db:
container_name: my-app-db-container
image: mongo
restart: always
volumes:
- './data:/data/db'
ports:
- "27017:27017"
Dockerfile
FROM node:16.13.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . /usr/src/app/
EXPOSE 3000
CMD [ "npm", "start"]
我有一个正在 docker 开发的开发环境,我希望能够实时重新加载我的更改而无需重建 docker 映像。我正在使用 docker compose 因为 redis 是我的应用程序的依赖项之一,我喜欢能够 link 一个 redis 容器
我在 docker-compose.yml
:
node:
build: ./node
links:
- redis
ports:
- "8080"
env_file:
- node-app.env
redis:
image: redis
ports:
- "6379"
我已经在我的 node
应用程序的 docker 文件中添加了一个卷,但是如何将主机的目录挂载到该卷中以便我所有的实时编辑代码是否反映在容器中?
这是我当前的 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"]
我的项目是这样的:
/
- docker-compose.yml
- node-app.env
- node/
- app.js
- Dockerfile.js
查看他们的 documentation
从外观上看,您可以在 docker-compose.yml
上执行以下操作volumes:
- ./:/app
其中 ./
是主机目录,/app
是容器的目标目录。
编辑:
以前的文档源现在会导致版本历史记录,您必须select您正在使用的 compose 版本并查找参考。
旁注:自本次编辑起,所有版本的语法都保持不变
有两件事:
我在docker-compose.yml
中添加了卷:
node:
volumes:
- ./node:/app
我将 npm install && nodemon app.js
部分移到了 CMD
中,因为 RUN
向联合文件系统添加了东西,而我的卷不是 UFS 的一部分。
# 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"]
# Define working directory
WORKDIR /app
# Expose port
EXPOSE 8080
# Run npm install
CMD npm install && nodemon app.js
有几个选项
短语法
使用 host : guest
格式,您可以执行以下任一操作:
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
长语法
从 docker-compose v3.2 开始,您可以使用长语法,它允许配置可以用短格式表示的附加字段,例如 mount type
(volume、bind 或 tmpfs ) 和 read_only
.
version: "3.2"
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
networks:
webnet:
volumes:
mydata:
查看 https://docs.docker.com/compose/compose-file/#long-syntax-3 了解更多信息。
如果您想在 Docker Compose YAML 文件的 volumes
部分中将特定主机目录(下例中的 /disk1/prometheus-data
)挂载为卷,您可以如下所示,例如:
version: '3'
services:
prometheus:
image: prom/prometheus
volumes:
- prometheus-data:/prometheus
volumes:
prometheus-data:
driver: local
driver_opts:
o: bind
type: none
device: /disk1/prometheus-data
顺便说一句,在prometheus的Docker文件中,您可能会发现如下VOLUME
指令,它标记它持有来自本地主机的外部挂载的卷等。(但注意:这个尽管将卷装载到容器中,指令不是必须的。):
Docker文件
...
VOLUME ["/prometheus"]
...
参考文献:
我们必须创建您自己的docker卷映射到主机目录 在我们 提及 之前 docker-compose.yml 作为 外部
1.Create 卷名为 share
docker volume create --driver local \
--opt type=none \
--opt device=/home/mukundhan/share \
--opt o=bind share
2.Use 它在你的 docker-compose
version: "3"
volumes:
share:
external: true
services:
workstation:
container_name: "workstation"
image: "ubuntu"
stdin_open: true
tty: true
volumes:
- share:/share:consistent
- ./source:/source:consistent
working_dir: /source
ipc: host
privileged: true
shm_size: '2gb'
db:
container_name: "db"
image: "ubuntu"
stdin_open: true
tty: true
volumes:
- share:/share:consistent
working_dir: /source
ipc: host
这样我们就可以与不同容器中的许多服务运行共享同一个目录
在docker-compose.yml中你可以使用这样的格式:
volumes:
- host directory:container directory
根据他们documentation
这是我的 Node.js 应用程序和 MongoDB 数据库的工作示例:
docker-compose.yml
version: '3'
services:
my-app:
container_name: my-app-container
restart: always
build: .
volumes:
- './storage:/usr/src/app/storage'
ports:
- "3000:3000"
links:
- my-app-db
my-app-db:
container_name: my-app-db-container
image: mongo
restart: always
volumes:
- './data:/data/db'
ports:
- "27017:27017"
Dockerfile
FROM node:16.13.2
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . /usr/src/app/
EXPOSE 3000
CMD [ "npm", "start"]