docker run
-v 宿主机绝对目录:容器目录 (相当于挂载)
#docker run -d -p 80:80 -it -v /opt/html:/usr/share/nginx/html nginx:latest /bin/bash
-v 容器目录 #创建一个随机卷,来持久化容器的目录下的数据,适合保存变化的数据
docker run -d -v /etc/nginx nginx:latest
#卷的目录在 /var/lib/docker/volumes/卷的名字/_data/
#容器删了宿主机的卷数据还在
#持久化
-v 卷名:容器目录 #创建一个固定名字的卷,来持久化容器的目录下的数据,适合数据共享
docker run -d -v test:/etc/nginx nginx:latest
--volumes-from #跟某一个容器挂载所有相同的卷
docker run -it --volumes-from 容器的ID或名字(sleepy_hoover) 镜像(centos:7)
docker volum ls
查看卷
练习:
只启动一个nginx容器,要求访问80端口,出现nginx默认欢迎首页,访问81端口,出现yiliao界面。
前提:上传代码文件到/opt
方法1:
docker run -d -p 80-81:80-81 -v /opt/yiliao:/html nginx
docker exec -it intelligent_germain /bin/bash
容器:
echo 'server {
listen 81;
server_name localhost;
location / {
root /html;
index index.html index.htm;
}
}' >/etc/nginx/conf.d/yiliao.conf
exit
宿主机:
docker restart intelligent_germain
方法2:
宿主机:
docker run -d -p 80-81:80-81 -v /opt/yiliao:/html nginx
vim yiliao.conf
server {
listen 81;
server_name localhost;
location / {
root /html;
index index.html index.htm;
}
}
docker ps -a -l
docker cp yiliao.conf crazy_sammet:/etc/nginx/conf.d/yiliao.conf
docker restart crazy_sammet
方法3:
宿主机:
vim /root/yiliao.conf
server {
listen 81;
server_name localhost;
location / {
root /html;
index index.html index.htm;
}
}
docker run -d -p 80-81:80-81 -v /opt/yiliao:/html -v /root/yiliao.conf:/etc/nginx/conf.d/yiliao.conf nginx
docker restart crazy_sammet