我有一个开发环境,我正在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.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
通过这种方式,我们可以与运行在不同容器中的许多服务共享同一个目录
下面是我的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 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
共享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