Skip to content

Commit

Permalink
Add environment variables for client OS/arch and client user UID/GID
Browse files Browse the repository at this point in the history
When using Docker Compose for development setup you often need to tweak the configuration to vary on Linux, MacOS, and Windows. And some services you would like to run with the same UID/GID as your current client user.

To help with that we introduce four new environment variables while parsing the configuration files:

- `COMPOSE_CLIENT_OS`: set to Go's `runtime.GOOS`
- `COMPOSE_CLIENT_ARCH`: set to Go's `runtime.GOARCH`
- `COMPOSE_CLIENT_UID`: set to the current users UID
- `COMPOSE_CLIENT_GUID`: set to the current users GID

This way we can now have a Docker Compose setup like this:

compose.yaml:
```yaml
services:
  php:
    image: php
    volumes:
      - .:/code
    user: ${COMPOSE_CLIENT_UID}:${COMPOSE_CLIENT_GID}
  web:
    image: apache
    extends:
      file: compose.${COMPOSE_CLIENT_OS}.yaml
      service: web
```

compose.linux.yaml:
```yaml
services:
  web:
    environment:
      VIRTUAL_HOST: mysite.local
```

compose.darwin.yaml:
```yaml
services:
  web:
    environment:
      VIRTUAL_HOST: mysite.docker
```

Signed-off-by: Arne Jørgensen <[email protected]>
  • Loading branch information
arnested committed May 15, 2024
1 parent eb5e018 commit 201ac30
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -616,6 +618,15 @@ func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
}
}
}

os.Setenv("COMPOSE_CLIENT_OS", runtime.GOOS)
os.Setenv("COMPOSE_CLIENT_ARCH", runtime.GOARCH)

if user, err := user.Current(); err == nil {
os.Setenv("COMPOSE_CLIENT_UID", user.Uid)
os.Setenv("COMPOSE_CLIENT_GID", user.Gid)
}

return nil
}

Expand Down

0 comments on commit 201ac30

Please sign in to comment.