什么是volume
数据卷就是将容器的数据存储到主机上,方便进行持久化存储。
含义:Linux文件可以挂载到容器中,Linux文件可以复制到容器中。
文件挂载
目标:Linux主机上面的nginx.conf配置文件挂载到 nginx 容器中。
nginx.conf ------> /usr/local/nginx/conf/nginx.conf
创建 volumes 文件夹,并创建 nginx.conf 文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://mywebapi:80;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
复制 docker-compose.yml 到 volumes
cp network/docker-compose.yml volumes/
修改 docker-compose.yml
version: '3'
services:
mynginx:
build: /root/nginx
ports:
- 8090:80
networks:
- nginx_webapi
volumes:
- /root/volumes/nginx.conf:/usr/local/nginx/conf/nginx.conf
mywebapi:
build: /root/webapi
ports:
- 8091:80
networks:
- nginx_webapi
networks:
nginx_webapi:
external: true
重新执行
docker-compose up -d
这样就可以把 /root/volumes/nginx.conf 文件挂载到容器中的 /usr/local/nginx/conf/nginx.conf。
除了可以把主机文件挂载到容器中,另外也可以把主机目录挂载到容器中或把容器目录挂载到主机中。