Fredrik Enetorp
Fredrik Enetorp

Reputation: 425

Problem mounting local directory to container in docker (docker volumes).

I have three files:

Dockerfile:

FROM php:7.2-apache
COPY src/ /var/www/html
EXPOSE 80

docker-compose.yml:

version: '3'

services:
    hello-world:
        build: .
        volumes:
         - ./src:/var/www/html
        ports:
         - 80:80

src/index.php:

<?php

echo "Hello World!";
?>

I then run the command:

docker-compose up

And up until this point everything's fine, but when I access localhost in my browser I get:

403 Forbidden You don't have permission to access / on this server.

...and from the container the error message:

Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive

However, if I from the docker-compose.yml file remove the 2 lines:

        volumes:
         - ./src:/var/www/html

...everything works fine (except the folder is not mounted). What am I doing wrong? It obviously doesn't like me mounting the volume, causing the directory /var/www/html/ to be empty, but why?

Upvotes: 3

Views: 2957

Answers (1)

Software Engineer
Software Engineer

Reputation: 16140

In your build file you are copying the contents of src into the image (under /var/www/html). The src directory is part of the build context, and must be below the 'build' root directory. This is probably where you ran the docker build command from, and is usually the place the dockerfile lives.

In your compose file you are mapping the src directory over the top of /var/www/html, replacing its contents with src at runtime.

I'm guessing that you only actually want to do one of these two things. Either is fine, depending on what you're trying to achieve, but based on your report it looks like the place you're running your docker-compose up from has an empty src directory, hence the error message. To fix this, either use an absolute path to the directory you want to serve or remove the volumes definition for that container.

One way to check this (assuming you tagged your image HelloWorld):

docker run -it -d --name helloworld -p 80:80 HelloWorld

Then, docker ps just to check that it's running. If everything is ok, try this:

docker exec -it -w /var/www/html helloworld ls -gAlFh

This should give you a list of what's in that directory. You can also try this:

docker exec -it -w /var/www/html helloworld sh

To get a shell in that directory so that you can explore more easily.

Upvotes: 2

Related Questions