Skip to content

Commit 06eb3ba

Browse files
authored
Merge pull request #2 from mshuler/docker-as-user
Run container build steps as non-root user
2 parents 5d37bee + 8c5f3d5 commit 06eb3ba

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

Dockerfile

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
FROM debian:stretch
22

3+
# Set up non-root user, 'build', with default uid:gid
4+
# This allows passing --build-arg to use local host user's uid:gid:
5+
# $ docker-compose build \
6+
# --build-arg UID=$(id -u) \
7+
# --build-arg GID=$(id -g) \
8+
# cassandra-website
9+
ARG UID=1000
10+
ARG GID=1000
11+
RUN echo "Setting up user 'build' with UID=${UID} GID=${GID}"
12+
RUN groupadd --gid $GID --non-unique build
13+
RUN useradd --create-home --shell /bin/bash \
14+
--uid $UID --gid $GID --non-unique build
15+
316
# Install tools
417
RUN apt-get update && \
518
apt-get install -y \
@@ -26,16 +39,18 @@ RUN gem install bundler && \
2639
bundle install && \
2740
rm /Gemfile /Gemfile.lock
2841

29-
ENV CASSANDRA_DIR="/usr/src/cassandra"
42+
# Run as build user from here
43+
USER build
44+
45+
ENV CASSANDRA_DIR="/home/build/cassandra"
3046

3147
# Setup repositories to building the docs
32-
RUN mkdir -p /usr/src/cassandra-site && \
48+
RUN mkdir -p /home/build/cassandra-site && \
3349
git clone https://gitbox.apache.org/repos/asf/cassandra.git ${CASSANDRA_DIR}
3450

3551
EXPOSE 4000/tcp
3652

37-
COPY docker-entrypoint.sh /
38-
RUN chmod +x /docker-entrypoint.sh
39-
ENTRYPOINT ["/docker-entrypoint.sh"]
53+
COPY docker-entrypoint.sh /home/build/
54+
ENTRYPOINT ["/home/build/docker-entrypoint.sh"]
4055

4156
CMD [""]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ $ docker-compose build cassandra-website
2727
$ docker-compose run cassandra-website
2828
```
2929

30+
:warning: *Tip:* In order to prevent root-owned modified files in your repository, the container user, `build`, is set up with a default UID=1000:GID=1000, which is usually the first user configured on a linux machine. If your local user is different you should set up the container user with your local host user's UID:GID, replace the above with:
31+
32+
```bash
33+
$ docker-compose build --build-arg UID=$(id -u) --build-arg GID=$(id -g) cassandra-website
34+
$ docker-compose run cassandra-website
35+
```
36+
3037
Go make yourself a cup of coffee, this will take a while...
3138

3239
Once building has completed, the site content will be in the `./cassandra-website/content` directory ready to be committed.

docker-compose.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ services:
44
cassandra-website:
55
build: .
66
image: cassandra-website:latest
7+
user: build
78
volumes:
8-
- ./src:/usr/src/cassandra-site/src
9-
- ./content:/usr/src/cassandra-site/publish
9+
- ./src:/home/build/cassandra-site/src
10+
- ./content:/home/build/cassandra-site/publish
1011

1112
cassandra-website-serve:
1213
build: .
1314
image: cassandra-website:latest
14-
entrypoint: /docker-entrypoint-jekyll-serve.sh
15+
user: build
16+
entrypoint: /home/build/docker-entrypoint-jekyll-serve.sh
1517
ports:
1618
- 4000:4000
1719
volumes:
18-
- ./src:/usr/src/cassandra-site/src
19-
- ./content:/usr/src/cassandra-site/publish
20-
- ./docker-entrypoint-jekyll-serve.sh:/docker-entrypoint-jekyll-serve.sh
20+
- ./src:/home/build/cassandra-site/src
21+
- ./content:/home/build/cassandra-site/publish
22+
- ./docker-entrypoint-jekyll-serve.sh:/home/build/docker-entrypoint-jekyll-serve.sh
2123

2224
preview:
2325
image: nginx
@@ -27,5 +29,3 @@ services:
2729
- "./content:/usr/share/nginx/html"
2830
command: [nginx-debug, '-g', 'daemon off;']
2931

30-
31-

docker-entrypoint-jekyll-serve.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -e
44

5+
export CASSANDRA_SITE_DIR="/home/build/cassandra-site"
6+
57
GREEN='\033[1;32m'
68
YELLOW='\033[0;33m'
79
NC='\033[0m' # No Color
@@ -10,11 +12,11 @@ NC='\033[0m' # No Color
1012
# version in the publish directory
1113
while [ 1 ]
1214
do
13-
sed -i 's/\.\/\.\.\//\.\/\.\.\/\.\.\//g' /usr/src/cassandra-site/publish/doc/*/index.html
15+
sed -i 's/\.\/\.\.\//\.\/\.\.\/\.\.\//g' ${CASSANDRA_SITE_DIR}/publish/doc/*/index.html
1416
sleep 5
1517
done &
1618

17-
cd /usr/src/cassandra-site/src
19+
cd ${CASSANDRA_SITE_DIR}/src
1820

1921
JEKYLL_COMMAND="jekyll serve --host 0.0.0.0"
2022

docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -xe
44

5-
export CASSANDRA_SITE_DIR="/usr/src/cassandra-site"
5+
export CASSANDRA_SITE_DIR="/home/build/cassandra-site"
66

77
jekyll --version
88

0 commit comments

Comments
 (0)