简体   繁体   中英

Why do I have to use volumes instruction in docker-compose

My local folder looks like so:

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

index.html contains plain html code. nginx.conf contains this simple configuration:

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

Dockerfile contains these instructions:

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

And finally docker-compose contains these instructions:

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

When I run docker-compose up and go to localhost:8181 for some reason I see a welcoming nginx page (not my index.html), if, however, I slightly change 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

So, my question is why do I have to specify this volumes instructions? I wish I could instead use my Dockerfile with COPY instructions.

Generally you will not need them, until you for example want to change code on the host and see the changes on the docker container.

Your setup looks good, all you need is defining your build context like

build: context: ./nginx-proxy

Other then that your COPY statements will copy the contents right like you expecting during build time , so there is no need to use volumes during runtime for this ( except the sync case as outlined at the top )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM