为什么我必须在docker-compose中使用卷指令

我的本地文件夹如下所示:

docker-compose.yml \nginx-proxy \code index.html nginx.conf Dockerfile 

index.html包含纯html代码。 nginx.conf包含这个简单的configuration:

 worker_processes 1; events { worker_connections 1024; } http { sendfile on; server { listent 80; root /code; index index.html; } } 

Dockerfile包含这些说明:

 FROM nginx:latest COPY nginx.conf /etc/nginx/nginx.conf COPY ./code /code 

最后docker-compose包含这些说明:

 nginx: build: ./nginx-proxy container_name: nginx-proxy ports: - "8181:80" 

当我运行docker-compose up并转到localhost:8181出于某种原因,我看到一个欢迎的nginx页面(不是我的index.html),但是,如果,我稍微更改docker-compose.yml:

 nginx: build: ./nginx-proxy container_name: nginx-proxy ports: - "8181:80" volumes: - ./nginx-proxy/code:/code - ./nginx-proxy/nginx.conf:/etc/nginx/nginx.conf 

所以,我的问题是为什么我必须指定这个volumes指示? 我希望我可以使用我的Dockerfile COPY指令。

一般来说,你不需要它们,直到你想要改变主机上的代码,并看到docker集装箱上的变化。

你的设置看起来不错,你需要的只是定义你的构build上下文

build:context:./nginx-proxy

除此之外,您的COPY语句将像构build时期期望的那样正确地复制内容,因此在运行时不需要使用卷(除了顶部列出的同步情况外)