Home > other >  Mounting host volumes in development container
Mounting host volumes in development container

Time:01-26

I have created a development container with some custom dependency compilation. After a few hours building it is time to run my first tests. My tests require loading files that are on the host disk (pretrained models and some data), in a separate folder. Running with tools like docker compose or docker command line I can share files of the host with the container by specifying volumes. The development container does the same with the code folder, but how can I specify additional volumes for development containers.

CodePudding user response:

You can use Multi Compose files for that:

Say you have a docker-compose.yml file where you have a base configuration for all your environments (dev, prod, etc.):

web:
  image: example/my_web_app:latest
  ports:
    - "1234:1234"

And then you have other environment files like docker-compose.env.yml where you add or override configuration based on the environment.

With your example, you'd have a docker-compose.dev.yml where you'd add the additional volumes required for your development containers:

web:
  volumes:
    - 'path/to/host/folder:/path/to/container/folder'

During development, you would then launch your stack with:

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

Note: make sure to check how properties are handled when overriding files here, and especially volumes in your case.

Edit: now to use several docker-compose.yml files in your devcontainer.json, use the dockerComposeFile property. Here you'll remove the "build" property and supply a list of docker-compose files:

...
  "dockerComposeFile": [
    "path/to/docker-compose.yml",
    "path/to/docker-compose.dev.yml"
  ],
  "service": "web",
  "shutdownAction": "stopCompose",
...

/!\ Note that the order of the files is important since later files override previous ones.

  •  Tags:  
  • Related