-
-
Notifications
You must be signed in to change notification settings - Fork 303
Best Practices for Non-root User #48
Comments
Try |
I think I figured it out. The RUN addgroup -S app && adduser -S -G app app |
Should I add a best practices to this repo? Or are we close to getting alpine in the official docker node and just wait to add it there? See docker-node/pull/156. |
Yeah, let's wait for the official image – I'm sure they'll be getting that in soon Thanks for highlighting this though and finding the appropriate command line options! |
Hey @styfle just wanted to stop by and say thank you for sharing this! I'm thinking about curating a list of best practices for docker / alpine because I'm personally having a hard time making the right decisions. |
@peterpme I am working with the official docker-node image to get these practices updated. |
adduser isn't available on Alpine out of the box? |
@ORESoftware yeah it is – what version you running? $ docker run alpine:3.6 adduser
BusyBox v1.26.2 (2017-06-11 06:38:32 GMT) multi-call binary.
Usage: adduser [OPTIONS] USER [GROUP]
Create new user, or add USER to GROUP
-h DIR Home directory
-g GECOS GECOS field
-s SHELL Login shell
-G GRP Add user to existing group
-S Create a system user
-D Don't assign a password
-H Don't create home directory
-u UID User id
-k SKEL Skeleton directory (/etc/skel)
$ docker run alpine:3.4 adduser
BusyBox v1.24.2 (2017-01-18 14:13:46 GMT) multi-call binary.
Usage: adduser [OPTIONS] USER [GROUP]
Create new user, or add USER to GROUP
-h DIR Home directory
-g GECOS GECOS field
-s SHELL Login shell
-G GRP Add user to existing group
-S Create a system user
-D Don't assign a password
-H Don't create home directory
-u UID User id
-k SKEL Skeleton directory (/etc/skel) |
For some reason using the command @styfle posted, my user stayed at the RUN addgroup -S myawesomegroup
RUN adduser -S myawesomeuser -G myawesomegroup :) |
For latest Alpine image, the
|
|
Here's what I do using docker-compose:
FROM node:carbon-alpine
LABEL author="el que m'est"
ARG UID
ARG GID
USER root
RUN apk add --no-cache shadow sudo && \
if [ -z "`getent group $GID`" ]; then \
addgroup -S -g $GID cetacean; \
else \
groupmod -n cetacean `getent group $GID | cut -d: -f1`; \
fi && \
if [ -z "`getent passwd $UID`" ]; then \
adduser -S -u $UID -G cetacean -s /bin/sh mobydick; \
else \
usermod -l mobydick -g $GID -d /home/mobydick -m `getent passwd $UID | cut -d: -f1`; \
fi && \
echo "mobydick ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/mobydick && \
chmod 0440 /etc/sudoers.d/mobydick
WORKDIR /home/mobydick/app
RUN chown mobydick:cetacean /home/mobydick/app
USER mobydick
version: '3'
services:
app:
build:
context: .
args:
UID: ${UID}
GID: ${GID}
command: sh
tty: true
stdin_open: true
volumes:
- .:/home/mobydick/app
ports:
- ${PORT}:3000
PORT=4000
{
"scripts": {
"dc": "GID=$(id -g) UID=$(id -u) docker-compose",
"dc-build": "yarn dc build"
}
} Finally I run |
As the error points out the group id This happens because the node image already as the user ╭─exadra37@exadra37-Vostro-470 ~
╰─➤ sudo docker run --rm --user 1000 -it node:10-alpine sh
/ $ id
uid=1000(node) gid=1000(node) groups=1000(node)
/ $ cat /etc/passwd | grep -irn 1000 -
29:node:x:1000:1000:Linux User,,,:/home/node:/bin/sh
/ $ cat /etc/group | grep -irn 1000 -
49:node:x:1000:node So no need for you to add your user when extending a node image, unless id is not The @elquimista solution works with any user and group id and you can use it without docker compose, like: sudo docker run --rm --user $(id -u) --env UID=$(id -u) --env GID=$(id -g) -it node:10-alpine sh |
Yeah, that's my point. @elquimista Just a couple of questions: Why is mobydick added to the sudoers? |
|
Yip, understand that, good work! |
First of all, thanks for creating and maintaining such a wonderful docker image! You're always so quick to update when a new version of node comes out 💯. And now for my question...
In the official docker-node repo, there is a Best Practices doc that explains you should create a user instead of running as root.
Does alpine-node have a similar best practice?
What is the equivalent
groupadd
anduseradd
in Alpine?The text was updated successfully, but these errors were encountered: