0

Supposed I have a Docker container for running my app (let's say a Laravel app), so what I normally do is have the Dockerfile and the docker-compose.yml file under my root directory. So the folder structure would look something like this:

laravel_app/
- app/
- config/
- bootstrap/
etc etc
- public/
- node_modules/
- Dockerfile
- .dockerignore
- docker-compose.yml
- composer.json
- package.json

Now, in my docker-compose.yml file I would normally have the service for the app and the volumes mapping like this

version: "3"

services:
   app:
     image: ...
     container_name: ...
     build: ...
     ports: ...
     volumes:
        - .:/var/www/html

The problem is that if I ssh to the app container and list the content of my working dir with ls -la I can see that some folders that I have in my .dockerignore file, like for example the node_modules, are listed in the running container and I believe this is because volumes are mapping all the folders listed in my working directory locally (i.e. laravel_app).

So is there a way to specify in volumes to exclude the directories I don't wan't to be mapped or to read through all I've listed in my dockerignore file?

ltdev
  • 4,037
  • 20
  • 69
  • 129
  • Does that `volumes:` block include your actual code? A Docker best practice would be to `COPY` it into your image, and not mount `volumes:` for it at all. – David Maze Mar 24 '23 at 13:19
  • Yes, in my Dockerfile I use `COPY . /var/www/html` which puts the code of the app in the container, but with volumes it reflects also the changes that I do without building the image again and again, right? – ltdev Mar 24 '23 at 14:35
  • [Add a volume to Docker, but exclude a sub-folder](https://stackoverflow.com/questions/29181032/add-a-volume-to-docker-but-exclude-a-sub-folder) asks basically this same question; the most common answer involves a hack of storing the library dependencies (the excluded folders) in Docker named volumes and depending on a Docker feature that populates the volumes on first use. I tend to recommend just using a host-based development environment without Docker for day-to-day development to avoid this sort of problem. – David Maze Mar 24 '23 at 15:00

1 Answers1

0

So is there a way to specify in volumes to exclude the directories I don't wan't to be mapped or to read through all I've listed in my dockerignore file?

No, this is not possible.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111