Skip to content

Instantly share code, notes, and snippets.

@zultron
Created February 4, 2019 02:25
Show Gist options
  • Save zultron/2c199229e53b6852d0523c69bb413c25 to your computer and use it in GitHub Desktop.
Save zultron/2c199229e53b6852d0523c69bb413c25 to your computer and use it in GitHub Desktop.
Docker entrypoint script to edit `/etc/hosts` and `/etc/passwd` at container init time
#!/bin/bash -e
# add hostname to hosts
sh -c 'echo "127.0.2.1 `hostname`" >> /etc/hosts'
# Start rsyslog for MK logging
service rsyslog start
# Add user and group to system
echo "${USER}:x:${UID}:${GID}::${HOME}:/bin/bash" >>/etc/passwd
echo "${USER}:x:${GID}:" >>/etc/group
sed -i '/.*dialout.*:/ s/$/'${USER}'/' /etc/group
sed -i '/.*plugdev.*:/ s/$/'${USER}'/' /etc/group
# Create RT cgroup
if test -n "${RT_CPUS}"; then
export RT_CGNAME=/rt
cgcreate -g cpuset:${RT_CGNAME}
cgset -r cpuset.mems=0 ${RT_CGNAME}
cgset -r cpuset.cpus=${RT_CPUS} ${RT_CGNAME}
cgset -r cpuset.cpu_exclusive=1 ${RT_CGNAME}
cgset -r cpuset.sched_load_balance=0 ${RT_CGNAME}
fi
# Run command as user
default_cmd=(/bin/bash --login -i)
exec sudo -u ${USER} -E "${@:-${default_cmd[@]}}"
@zultron
Copy link
Author

zultron commented Feb 4, 2019

In the Dockerfile, add:

COPY entrypoint.sh /usr/bin/entrypoint
ENTRYPOINT ["/usr/bin/entrypoint"]

Then run the container with appropriate variables set:

docker run -e USER -e HOME  -e UID=$(id -u) -e GID=$(id -g) [...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment