x-yuri
x-yuri

Reputation: 18883

Make a directory from one container available to another one while keeping files from the original one

Let's say you have an image with a Rails application, containing assets. And you want to serve them from another container running Nginx.

From what I gather, mounting a volume makes the contents of a directory disappear. So, if you mount one volume into two containers, like,

volumes:
  assets:
services:
  app:
    volumes:
      assets:/app/public/assets
  nginx:
    volumes:
      assets:/assets

they both will see an empty folder. You can very well fill it up by hand. But if you were to deploy a newer version of the Rails app image, those two won't see the changes.

Am I missing something? Is there a way to handle files without proxying them to Rails app or copying them from container to container?

UPD First container with non-empty directory that gets the volume mounted determines its initial content.

Upvotes: 1

Views: 302

Answers (1)

Siyu
Siyu

Reputation: 12089

You can add the following lines before starting Rails to your Rails image's Dockerfile (CMD or ENTRYPOINT):

rm -r /assets/*
cp -r /app/public/assets/* /assets

And mount the volume into /assets for both services.

This way every time your container restarts (on docker stack deploy when it's changed), the volume is refilled with fresh assets that is visible to nginx container.

Upvotes: 1

Related Questions