Skip to content

Commit 17e252a

Browse files
committed
add non-root user for workspace container.
1 parent dd06f1e commit 17e252a

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ services:
1010
args:
1111
- INSTALL_MONGO=false
1212
- INSTALL_XDEBUG=false
13-
- INSTALL_NODE=false
13+
- INSTALL_NODE=true
1414
- COMPOSER_GLOBAL_INSTALL=false
15+
- PUID=1000
16+
- PGID=1000
1517
volumes_from:
1618
- volumes_source
1719
tty: true

workspace/Dockerfile

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,25 @@ RUN if [ ${INSTALL_MONGO} = true ]; then \
6060
echo "extension=mongodb.so" >> /etc/php/7.0/cli/php.ini \
6161
;fi
6262

63+
#####################################
64+
# Non-Root User:
65+
#####################################
66+
67+
# Add a non-root user to prevent files being created with root permissions on host machine.
68+
ARG PUID=1000
69+
ARG PGID=1000
70+
RUN groupadd -g $PGID laradock && \
71+
useradd -u $PUID -g laradock -m laradock
72+
73+
# Now switch to our laradock user for the rest of user setup
74+
USER laradock
75+
6376
#####################################
6477
# Composer:
6578
#####################################
6679

6780
# Add the composer.json
68-
ADD ./composer.json /root/.composer/composer.json
81+
ADD ./composer.json /home/laradock/.composer/composer.json
6982

7083
# Check if global install need to be runned
7184
ARG COMPOSER_GLOBAL_INSTALL=true
@@ -84,13 +97,13 @@ ARG INSTALL_NODE=true
8497
ENV INSTALL_NODE ${INSTALL_NODE}
8598
RUN if [ ${INSTALL_NODE} = true ]; then \
8699
# Install nvm (A Node Version Manager)
87-
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash \
100+
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash \
88101
;fi
89102
# Again check if NVM needs to be installed
90-
# I had to split this condifiton link this because when I get it inside the above if statment is refuses to work!
103+
# I had to split this condition link this because when I get it inside the above if statement is refuses to work!
91104
ENV if [ ${INSTALL_NODE} = true ]; then \
92105
# Set the ENV
93-
NVM_DIR=/root/.nvm \
106+
NVM_DIR=/home/laradock/.nvm \
94107
# Install NodeJS with NVM
95108
RUN . ~/.nvm/nvm.sh && \
96109
nvm install stable && \
@@ -99,13 +112,22 @@ ENV if [ ${INSTALL_NODE} = true ]; then \
99112
npm install -g gulp bower \
100113
;fi
101114

115+
# Wouldn't execute when added to the RUN statement in the above block
116+
# Source NVM when loading bash since ~/.profile isn't loaded on non-login shell
117+
RUN if [ ${INSTALL_NODE} = true ]; then \
118+
echo "" >> ~/.bashrc && \
119+
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
120+
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> ~/.bashrc \
121+
;fi
122+
102123
#
103124
#--------------------------------------------------------------------------
104125
# Final Touch
105126
#--------------------------------------------------------------------------
106127
#
107128

108129
# Clean up
130+
USER root
109131
RUN apt-get clean && \
110132
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
111133

0 commit comments

Comments
 (0)