Reputation: 14897
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.
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:
...
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.
How can I ensure that Docker compose works in- and outside of VSCode's devcontainers
?
Would it be possible to set the environment variable to a default value? Because in my case the actual value that should be set if the project is not opened inside a devcontainer
is just a simple dot (.
). Because when I run the command echo ${LOCAL_WORKSPACE_FOLDER}
inside the integrated VSCode terminal, the correct path gets printed. So it seems that VSCode just sets normal environment variables?
(If the assumption from above is correct) wouldn't it be possible to write a simple Bash script install.sh
that set's the correct path automatically? This script should only be run once during the setup of the project. How could this file look like?
Upvotes: 2
Views: 2557
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