Docker compose how to mount path from one to another container?

DockerDocker ComposeDockerfile

Docker Problem Overview


I've nignx container and one asset container which have all my assets build from grunt or some other tools.

Now in docker compose file, i want to mount asset container's 's folder path into nginx container so nginx can serve that files.

  • How can we do that? i don't remember but i think there is a option where we can share path of one container with another.

  • Suppose if i scale up nginx to 2 container then will that mount works for all instance of nginx?

  • if i scale up asset container then what will happen?

  • i also want to mount that with my host so development can be done be easily.

Docker Solutions


Solution 1 - Docker

What you want to do is use a volume, and then mount that volume into whatever containers you want it to appear in.

Completely within Docker

You can do this completely inside of Docker.

Here is an example (stripped-down - your real file would have much more than this in it, of course).

version: '3'
services:
  nginx:
    volumes:
      - asset-volume:/var/lib/assets
  asset:
    volumes:
      - asset-volume:/var/lib/assets

volumes:
  asset-volume:

At the bottom is a single volume defined, named "asset-volume".

Then in each of your services, you tell Docker to mount that volume at a certain path. I show example paths inside the container, just adjust these to be whatever path you wish them to be in the container.

The volume is an independent entity not owned by any particular container. It is just mounted into each of them, and is shared. If one container modifies the contents, then they all see the changes.

Note that if you prefer only one can make changes, you can always mount the volume as read-only in some services, by adding :ro to the end of the volume string.

services:
  servicename:
    volumes:
      - asset-volume:/var/lib/assets:ro

Using a host directory

Alternately you can use a directory on the host and mount that into the containers. This has the advantage of you being able to work directly on the files using your tools outside of Docker (such as your GUI text editor and other tools).

It's the same, except you don't define a volume in Docker, instead mounting the external directory.

version: '3'
services:
  nginx:
    volumes:
      - ./assets:/var/lib/assets
  asset:
    volumes:
      - ./assets:/var/lib/assets

In this example, the local directory "assets" is mounted into both containers using the relative path ./assets.

Using both depending on environment

You can also set it up for a different dev and production environment. Put everything in docker-compose.yml except the volume mounts. Then make two more files.

  • docker-compose.dev.yml
  • docker-compose.prod.yml

In these files put only the minimum config to define the volume mount. We'll mix this with the docker-compose.yml to get a final config.

Then use this. It will use the config from docker-compose.yml, and use anything in the second file as an override or supplemental config.

docker-compose -f docker-compose.yml \
    -f docker-compose.dev.yml \
    up -d

And for production, just use the prod file instead of the dev file.

The idea here is to keep most of the config in docker-compose.yml, and only the minimum set of differences in the alternative files.

Example:

docker-compose.prod.yml

version: '3'
services:
  nginx:
    volumes:
      - asset-volume:/var/lib/assets

docker-compose.dev.yml

version: '3'
services:
  nginx:
    volumes:
      - ./assets:/var/lib/assets

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondev.meghrajView Question on Stackoverflow
Solution 1 - DockerDan LoweView Answer on Stackoverflow