我有一个开发环境,我正在dockerizing,我希望能够重载我的变化,而不必重建docker图像。我使用docker撰写,因为redis是我的应用程序的依赖项之一,我喜欢能够链接一个redis容器
我在docker-compose.yml中定义了两个容器:
node:
build: ./node
links:
- redis
ports:
- "8080"
env_file:
- node-app.env
redis:
image: redis
ports:
- "6379"
我已经在我的节点应用程序的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
如果你想在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的Dockerfile中,你可能会发现如下VOLUME指令,它标志着它保存了来自本机主机的外部挂载的卷等(但是注意:这条指令不是必须的,尽管要将卷挂载到容器中):
Dockerfile
...
VOLUME ["/prometheus"]
...
参考文献:
https://docs.docker.com/compose/compose-file/compose-file-v3/#driver
https://docs.docker.com/compose/compose-file/compose-file-v3/#driver_opts
有两件事:
我在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
共享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