6

I'm using docker-compose for a Rails setup. To sync the files, I've mounted the directory like (some of the code is omitted):

app:
    build: .
    command: rails server -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app

However, when running the app, I don't want the folder, tmp, to be synced. Adding the folder to dockerignore only solves the problem when building the docker image. How can I ignore the tmp folder from being synced between the host and guest when running docker compose?

mastef
  • 2,787
  • 1
  • 6
  • 16
Anders
  • 181
  • 1
  • 1
  • 13
  • Possible duplicate of [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) – Louis Aug 12 '19 at 15:07

5 Answers5

4

You can add a second volume to a sub-path. It will override the contents of the existing volume. Like so :

app:
    build: .
    command: rails server -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
      - ./empty-tmp:/app/tmp

This will effectively make /app/tmp show the contents of ./empty-tmp on the host system. Instead of showing the contents of ./tmp

mastef
  • 2,787
  • 1
  • 6
  • 16
3

What I ended up doing was to simply map all the folders which I wanted to sync:

- ./app:/app/app
- ./lib:/app/lib
- ./log:/app/log
- ./spec:/app/spec

Which seems to solve the problem

Anders
  • 181
  • 1
  • 1
  • 13
  • yeah - reasonably quick / understandable way to do this - still today. weird there's no explicit exclusion mechanism in docker-compose (not the volume mapping trick reported in other answers) – neonwatty Oct 12 '22 at 22:10
3

This used to be possible as described in Add a volume to Docker, but exclude a sub-folder. The solutions offered there didn't work for me anymore with docker-compose 1.25.0-rc3, file syntax v3.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
2

You can use a data volume to store tmp, as data volumes copy in the data from the built docker image before the app directory is mounted

volumes:
- .:/app
- /path/to/tmp
Siyu
  • 11,187
  • 4
  • 43
  • 55
  • 2
    Thanks for your reply. When the guest creates the tmp folder, it is still created on the host. Effectively what I want is the tmp only to exist in the guests file system – Anders Dec 01 '18 at 14:20
  • so your goal with to prevent the host from seeing the file in a container. this is not possible because even if you can, whoever can run `docker` can just `exec` in to the container and see everything. – Siyu Dec 01 '18 at 14:24
  • 3
    The problem is that rails creates a tmp dir which contains the current process id of the server. If the server has been running previously or the container exits unexpected, this folder is not cleared making a new docker process unable to start – Anders Dec 01 '18 at 14:29
1

The right way to exclude folder from container but still be able to login into container and inspect files (I'm talking about development environment):

volumes:
  - ./app-backend:/srv/www/app
  - type: tmpfs
    target: /srv/www/app/var/

So my ./var folder is excluded from syncing but when I log into the container - cache and log files are there.

Reference: https://docs.docker.com/storage/tmpfs/, a quote:

a tmpfs mount is temporary, and only persisted in the host memory. When the container stops, the tmpfs mount is removed, and files written there won’t be persisted.

This works for:

  • Docker version 20.10.13
  • Docker Compose version v2.3.3
Anton
  • 417
  • 3
  • 9