Docker数据卷挂载相关
挂载的时候,类似于linux u盘插入的操作,宿主机目录会被复制进去。
命令启动挂载
挂载本地目录
必须写绝对路径
[root@warship ~]# docker run -itd -v ./nginx:/etc/nginx --name nginx4 nginx:latest
docker: Error response from daemon: create ./nginx: "./nginx" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
[root@warship ~]# docker run -itd -v /root/data:/data --name nginx4 nginx:latest
f7b03266c886098459adbbe9b2af2c5332527e38c4984bdc9d6f3cf293cb026f
[root@warship ~]# docker run -itd --name nginx5 --mount type=bind,source=/root/data/,target=/data,consistency=consistent nginx:latest
c9a8de286db5b6a68a1babb46f66788f41c6c0c948be1ff380337702a618adeb
Compose命令挂载
挂载可以使用相对路径,应该是根据yaml文件来定位,,普通docker的命令-v不能使用相对路径,没有yaml,不能定位???
version: '3'
services:
mysql:
container_name: blog-mysql
image: mysql/blog
privileged: true
restart: always
environment:
MYSQL_DATABASE: blog
MYSQL_ROOT_PASSWORD: 123456
MYSQL_ROOT_HOST: '%'
volumes:
- ./data/mysql:/var/lib/mysql
- ./data/blog.sql:/docker-entrypoint-initdb.d/blog.sql
expose:
- "3306"
# 测试时使用,正式环境最好不暴露端口到宿主机
# ports:
# - "3306:3306"
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci'
]
更多参考:
https://blog.csdn.net/jb19900111/article/details/89333854