Search code examples
dockerlaravel-sail

How to set up .dockerignore with Laravel Sail?


After Laravel Sail install, I published the Laravel Sail docker files to my project following the official docs. The directory is at {project root}/docker/8.1. This directory is also the one specified as build context in the docker-compose.yml file. I added a .dockerignore file in that directory with the following content:

**/.devcontainer
**/.git
**/.vscode
**/docker
[...]

When I terminal into the laravel container, the directories and files in my .dockerignore have been copied over despite [internal] load .dockerignore log statement on sail build. sail build is basically an alias for docker compose build.

How can I set-up my .dockerignore for it to be applied on container build?

As a side-note, I don't really understand how the project source files are copied over to the container, as the Dockerfile does not seem to contain COPY or ADD instructions wrt these.

I have looked on this site for answers and on the web without success. I also went through the laravel/sail github issues.

Any input would be greatly appreciated.


Solution

  • By default, the project files are copied over to the container using a volume. This is why the .dockerignore file is ignored. I decided to replace the volume by a COPY command in the Dockerfile:

    COPY --chown=$WWWUSER:$WWWGROUP . /var/www/html/
    

    I had to change the docker-compose.yml build directive as follow:

     build:
        context: .
        dockerfile: ./docker/8.1/Dockerfile
        args:
            WWWUSER: "${WWWUSER}"
            WWWGROUP: "${WWWGROUP}"
    

    I adjusted relative paths to local files in the Dockerfile.

    I placed the .dockerignore in the project root and it is now taken into account as expected.