![](/img/trans.png)
[英]How to mount a host directory as volume in docker container using docker compose
[英]How do I mount a host directory as a volume in docker compose
我有一個正在 dockerizing 的開發環境,我希望能夠 livereload 我的更改而不必重建 docker 圖像。 我正在使用 docker compose 因為 redis 是我的應用程序的依賴項之一,我喜歡能夠鏈接 redis 容器
我在docker-compose.yml
中定義了兩個容器:
node:
build: ./node
links:
- redis
ports:
- "8080"
env_file:
- node-app.env
redis:
image: redis
ports:
- "6379"
我已經在我的node
應用程序的 dockerfile 中添加了一個卷,但是如何將主機的目錄掛載到該卷中,以便我對代碼的所有實時編輯都反映在容器中?
這是我當前的 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
有幾個選項
使用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
)掛載為volumes
,您可以按如下方式進行,例如:
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 的 Dockerfile 中,您可能會找到如下的VOLUME
指令,這將其標記為從本機主機等外部安裝的卷(但請注意:盡管將卷安裝到容器中,該指令並不是必須的。 ):
文件
...
VOLUME ["/prometheus"]
...
參考:
這是兩件事:
我在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
我們要創造與主機目錄映射自己的碼頭工人卷之前,我們在泊塢窗,compose.yml外部提
1.創建名為share的卷
docker volume create --driver local \
--opt type=none \
--opt device=/home/mukundhan/share \
--opt o=bind share
2.在你的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
這樣我們就可以與運行在不同容器中的許多服務共享同一個目錄
分享 redis golang docker-compose.yaml
。 使用綁定安裝我已經實現了。
version: '3.0'
services:
redisdb:
image: redis:6.0
restart: always
ports:
- "6379:6379"
container_name: redisdb-container
command: ["redis-server", "--bind", "redisdb", "--port", "6379"]
urlshortnerservice:
depends_on:
- redisdb
ports:
- "7777:7777"
restart: always
container_name: url-shortner-container
image: url-shortner-service
volumes:
- ../pkg/repository/filestorage:/pkg/repository/filestorage #host directory:container directory
這是我的 Node.js 應用程序和 MongoDB 數據庫的工作示例:
碼頭工人-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"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.