winklerrr
winklerrr

Reputation: 14897

How to mount volumes with docker-compose in- and outside of a VSCode devcontainer?

Scenario

The project I'm working on (a React app) uses docker-compose to setup it's backend, webserver and frontend. I'm working inside a VSCode devcontainer (Node with Typescript).

The Docker in Docker environment I've setup seems to work fine and I'm able to start each of the Docker containers but I had to adapt the code in the following manner because otherwise Docker wasn't able to locate the specified volumes to mount.

Setup

First I needed to set a remote environment variable in my devcontainer.json:

"remoteEnv": {
    // the original host directory which is needed for volume mount commands from inside the container (Docker in Docker)
    "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
}

I'm then using this environment variable in the docker-compose.yaml like so:

services: 

  webserver:
      build: 
          context: ./docker
          dockerfile: webserver/Dockerfile
      image: webserver
      container_name: webserver_nginx
      ports:
          - 8080:80
      volumes:
          -  ${LOCAL_WORKSPACE_FOLDER}/webserver:/etc/nginx/conf.d
          -  ${LOCAL_WORKSPACE_FOLDER}/build:/var/www/html
      restart: unless-stopped
      depends_on: 
        - backend

  backend:
      ...

Problem

On my machine (and on the machine of my colleagues who also use VSCode) everything works fine. But I have some teams members which don't use VSCode. When I commit the adapted docker-compose.yaml file, their setup doesn't work anymore and vice-versa if they adapt the file again to their needs.

Question

How can I ensure that Docker compose works in- and outside of VSCode's devcontainers?

Possible solutions?

Upvotes: 2

Views: 2557

Answers (1)

jehon
jehon

Reputation: 1668

Docker compose allow default value for variables:

${VARIABLE:-default} evaluates to default if VARIABLE is unset or empty in the environment.

See: https://docs.docker.com/compose/environment-variables/

For you case example, you can use:

${LOCAL_WORKSPACE_FOLDER:-.}

PS: I never used that personnaly

Upvotes: 2

Related Questions