Docker is how to mount a path from one container to another?

I have a nignx container and one asset container , in which all my assets are built from grunt or some other tools.

Now in the docker build file, I want to set the path to the asset container's folder in nginx container so that nginx can serve these files.

  • How can we do this? I don’t remember, but I think there is an option where we can share the path to one container with another.

  • Suppose if I scale nginx to 2 containers, then this one is mounted for the whole nginx instance?

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

  • I also want to install this using my host, so development can be done easily.

+7
docker docker-compose dockerfile
source share
1 answer

What you want to do is use volumes, and then mount that volume into all the containers in which you want to include it.

Completely in Docker

You can do this completely inside Docker.

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

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

Below is a single volume named "asset volume."

Then, in each of your services, you specify Docker to mount this volume in a specific path. I am showing examples of paths inside a container, just configure them so that they are in any container.

A volume is an independent object that does not belong to any particular container. It is simply mounted in each of them and is common. If one container changes the contents, they all see the changes.

Please note: if you prefer that only one can make changes, you can always set the volume as read-only in some services by adding :ro to the end of the volume line.

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

Using the host directory

Alternatively, you can use the directory on the host and mount it in containers. This has the advantage that you can work directly with files using your tools outside of Docker (for example, a GUI text editor and other tools).

The same thing, except that you do not define a volume in Docker, instead set 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 in both containers using the relative path ./assets .

Using both depending on the environment

You can also configure it for a different development and production environment. Put everything in docker-compose.yml, except the volumes. Then create two more files.

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

In these files, only the minimum configuration for determining the volume is set. We will mix this with docker-compose.yml to get the final config.

Then use this. It will use the configuration from docker-compose.yml and use anything in the second file as an overridden or additional configuration.

 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 save most of the configuration in the docker-compose.yml file and only a minimal 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 
+11
source share

All Articles