目录挂载
[root@192 ~]# docker run -d --name app01 -p 80:80 -v /root/nginxhtml:/usr/share/nginx/html 9bbf2937ac71
35733ef692f3c6e169a003cfb77f47e364e3526fe38c735fc7e1e02c504f92c9
[root@192 ~]# ll
总用量 133992
-rw-------. 1 root root 1404 8月 26 20:39 anaconda-ks.cfg
-rw-------. 1 root root 137201664 8月 27 15:07 mynginx.tar
drwxr-xr-x. 2 root root 6 8月 27 21:11 nginxhtml
[root@192 nginxhtml]# ll
总用量 0
此时以外部的nginxhtml的内容为准
在外部创建index.html
[root@192 nginxhtml]# echo 22222 > index.html
[root@192 nginxhtml]# ll
总用量 4
-rw-r--r--. 1 root root 6 8月 27 21:21 index.html
从容器内部修改index.html
[root@192 nginxhtml]# docker exec -it 35733ef692f3 bash
root@35733ef692f3:/#
root@35733ef692f3:/#
root@35733ef692f3:/# cd /usr/share/nginx/html/
root@35733ef692f3:/usr/share/nginx/html# ls
index.html
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html# echo 3333 >> index.html
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html#
root@35733ef692f3:/usr/share/nginx/html# cat index.html
22222
3333
外部的文件同样也自动修改了
[root@192 nginxhtml]# cat index.html
22222
3333
卷映射
和目录挂载不同的是,卷映射在创建容器时直接同步容器内部的目录下数据到外部固定位置
创建容器
docker run -d --name app02 -v nginxconf:/etc/nginx -p 88:80 9bbf2937ac71
nginxconf是卷名
[root@192 nginxhtml]# docker run -d --name app02 -v /root/nginxhtml:/usr/share/nginx/html -v nginxconf:/etc/nginx -p 88:80 9bbf2937ac71
2fa62e43bff92da899a628df84073560935effe9a17eac7c4d041d514be3eda3
查看卷
[root@192 nginxhtml]# docker volume ls
DRIVER VOLUME NAME
local nginxconf
查看容器的详细信息
docker inspect 容器id
88端口访问
从容器外部修改nginx配置文件
进入容器内部查看并修改nginx.conf文件
root@2fa62e43bff9:/etc/nginx# echo '#update data' >> nginx.conf
root@2fa62e43bff9:/etc/nginx# cat nginx.conf
#测试,修改容器内部数据
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#update data
再到容器外部查看数据是否同步
[root@192 _data]# cat nginx.conf
#测试,修改容器内部数据
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#update data