Skip to content

Instantly share code, notes, and snippets.

@barata0
Created January 31, 2023 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barata0/5954f0c0f771d45da3e826dc65656399 to your computer and use it in GitHub Desktop.
Save barata0/5954f0c0f771d45da3e826dc65656399 to your computer and use it in GitHub Desktop.
docker-compose dev with same user and group as host
version: "3"
# to execute this compose for a user and group different than 1000:1000
# UNAME=$(id -un) GNAME=$(id -gn) MY_UID=$(id -u) MY_GID=$(id -g) docker compose up --build
services:
app:
tty: true
build:
args:
UID: ${MY_UID}
GID: ${MY_GID}
UNAME: ${UNAME:-app}
GNAME: ${GNAME:-app}
context: .
user: ${UNAME:-app}
volumes:
- .:/workspace
working_dir: /workspace
FROM golang:1.19
RUN apt-get update && apt-get -y install sudo
# get args from docker-compose args: or docker build --build-arg
ARG UNAME
ARG GNAME
ARG UID=1000
ARG GID=1000
# create the group and user
RUN groupadd -g $GID -o $GNAME
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME
# allow sudo and set new files mask to `-rw-r--r--`
RUN adduser $UNAME sudo && umask 0002
# set a password to run sudo commands
RUN echo "${UNAME}:${UNAME}" | chpasswd
#!/bin/bash
UNAME=$(id -un) GNAME=$(id -gn) MY_UID=$(id -u) MY_GID=$(id -g) docker compose up $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment