Skip to content

Commit 33b701a

Browse files
authored
Merge pull request laradock#188 from computerfr33k/workspace-user
Use workspace as non-root user
2 parents dd06f1e + e0c1837 commit 33b701a

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ You can select your own combination of container form this list:
227227
docker exec -it {Workspace-Container-Name} bash
228228
```
229229
Replace `{Workspace-Container-Name}` with your Workspace container name.
230+
<br />
231+
Add `--user=laradock` to have files created as your host's user. (don't forget to change the PUID (User id) and PGID (group id) variables in docker-compose.yml).
230232
<br>
231233
To find the containers names type `docker-compose ps`.
232234

@@ -657,7 +659,7 @@ More details about this [here](https://github.com/jenssegers/laravel-mongodb#ins
657659
7 - Test it:
658660

659661
- First let your Models extend from the Mongo Eloquent Model. Check the [documentation](https://github.com/jenssegers/laravel-mongodb#eloquent).
660-
- Enter the Workspace Container `docker exec -it laradock_workspace_1 bash`.
662+
- Enter the Workspace Container `docker exec -it --user=laradock laradock_workspace_1 bash`.
661663
- Migrate the Database `php artisan migrate`.
662664

663665

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ services:
1212
- INSTALL_XDEBUG=false
1313
- INSTALL_NODE=false
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)