SSH under docker when no user exists for uid

Hi, I am sharing the workaround I developed since migrating to docker, that allows SSH to work properly once again.

I use the ssh command to send instructions for remote operations to linux boxes like Raspberry Pis that I have deployed for various purposes. Since migrating my Home Assistant instance to docker these started failing with the error

No user exists for uid xxxx

People who try to run git inside their docker containers might find the same issue too. The error occurs because the docker configuration makes the container run in the context of a user id that only exists in the docker host, not in the container.

No surprisingly the solution is to add the user into the container, so this is done with a Dockerfile, referred to in the Compose file

Dockerfile

# Dockerfile to address issue 'No user exists for uid xxxx'  
# with shell_command 'ssh user@server 2 >command.log' or git or similar

FROM homeassistant/home-assistant

# accept the arguments from .Env (via compose file)
ARG PUID 
ARG PGID
ARG USER

# Add the group (if not existing) 
# then add the user to the numbered group 
RUN addgroup -g ${PGID} ${USER} || true && \
    adduser -D -u ${PUID} -G `getent group ${PGID} | cut -d: -f1` ${USER} || true 

LABEL vcs-url=https://github.com/artmg/home-assistant-docker

docker-compose.yaml

  homeassistant:
#    image: homeassistant/home-assistant
    build: 
      context: .
      args:
        - PUID=${PUID}
        - PGID=${PGID}
        - USER=${USER}
    image: home-assistant:home-assistant-docker-adduser

Instructions

# you can set these by hand, but easiest in a .env file
#PUID=UserID you want to run the Docker containers under
#PGID=GroupID of the docker group
PUID=`id -u $USER`
PGID=`getent group docker | cut -d: -f3`

docker-compose up -d --rebuild

I hope this helps people who run into similar issues - please ask if you have queries or issues.

Hi! Thanks for sharing. How could I navigate to .env file?

Hi @pavel-kru welcome to the HA community. The .env file is typically in the same location as the compose file. You can create the contents from the terminal command line, e.g.

tee docker/compose/.env << EOF!
PUID=1000
PGID=133
USER=hass
EOF!

but the values you specify in this file will very much depend on your own build environment.