我有一个开发环境,我正在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
共享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
如果你想在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
有几个选择
短的语法
使用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类型(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.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
在我们提到docker-compose之前,我们必须创建自己的与主机目录映射的docker卷。Yml作为外部
1.创建名为share的卷
docker volume create --driver local \
--opt type=none \
--opt device=/home/mukundhan/share \
--opt o=bind share
2.在你的码头写作中使用它
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