Docker Desktop使用宿主机Windows的配置文件持久化存储数据

《微软官网Windows 上的 Docker 引擎帮助文档》
以Nginx为例

拉取Nginx镜像

docker pull nginx:1.23.1

先启动Nginx,然后把Nginx的配置文件复制到Windows

docker run --name=nginx-1.23.1 -d -p 80:80 nginx:1.23.1

把Nginx的配置文件复制到Windows

注意:Windows的盘符是小写的,以及目录直接是用反斜杠的!(我掉这坑里好久!!!)

docker cp nginx-1.23.1:/etc/nginx/nginx.conf d:\software\nginx\nginx.conf
docker cp nginx-1.23.1:/usr/share/nginx/html d:\software\nginx\html

修改宿主机的nginx.conf配置文件


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
	server {
        listen       80;
        server_name  localhost;

        location / {
            #root   html;
            root   /usr/share/nginx/html; # nginx工作目录是容器的而非宿主机
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
	}


    include /etc/nginx/conf.d/*.conf;
}

删除nginx容器

docker rm -f nginx-1.23.1

重启启动一个新的Nginx容器

docker run -p 80:80 --name nginx-1.23.1 --privileged=true --restart=always -v d:\software\nginx\html:/usr/share/nginx/html -v d:\software\nginx\log:/var/log/nginx -v d:\software\nginx\nginx.conf:/etc/nginx/nginx.conf -d nginx:1.23.1

访问localhost

Docker Desktop使用宿主机Windows的配置文件持久化存储数据_第1张图片

你可能感兴趣的:(Linux,Windows,Docker,docker,windows,容器)