Skip to content

Commit 569cf0d

Browse files
authored
feat: Run the app container as a non-root user (#264)
1 parent 0176f00 commit 569cf0d

File tree

7 files changed

+60
-20
lines changed

7 files changed

+60
-20
lines changed

.github/workflows/laravel-create-project.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ jobs:
1111
- name: Docker Version
1212
run: docker version
1313
- name: Docker Compose Settings
14-
run: echo APP_BUILD_TARGET=development-xdebug > .env
14+
run: |
15+
echo APP_BUILD_TARGET=development-xdebug > .env
16+
echo "UID=$(id -u)" >> .env
17+
echo "GID=$(id -g)" >> .env
1518
- name: Build Docker Images
1619
run: docker compose build
1720
- name: Create & Start Docker Containers

.github/workflows/laravel-git-clone.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ jobs:
1313
- name: Docker Version
1414
run: docker version
1515
- name: Docker Compose Settings
16-
run: echo APP_BUILD_TARGET=development-xdebug > .env
16+
run: |
17+
echo APP_BUILD_TARGET=development-xdebug > .env
18+
echo "UID=$(id -u)" >> .env
19+
echo "GID=$(id -g)" >> .env
1720
- name: Build Docker Images
1821
run: docker compose build
1922
- name: Create & Start Docker Containers

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
for-linux-env:
2+
echo "UID=$$(id -u)" >> .env
3+
echo "GID=$$(id -g)" >> .env
14
install:
25
@make build
36
@make up

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## Introduction
1313

14-
Build a simple laravel development environment with docker-compose. Compatible with Windows(WSL2), macOS(M1) and Linux.
14+
Build a simple laravel development environment with Docker Compose. Support with Windows(WSL2), macOS(Intel and Apple Silicon) and Linux.
1515

1616
## Usage
1717

@@ -22,14 +22,19 @@ Build a simple laravel development environment with docker-compose. Compatible w
2222
3. Execute the following command
2323

2424
```bash
25+
$ task for-linux-env # Linux environment only
2526
$ task create-project
2627

2728
# or...
2829

30+
$ make for-linux-env # Linux environment only
2931
$ make create-project
3032

3133
# or...
3234

35+
$ echo "UID=$(id -u)" >> .env # Linux environment only
36+
$ echo "GID=$(id -g)" >> .env # Linux environment only
37+
3338
$ mkdir -p src
3439
$ docker compose build
3540
$ docker compose up -d
@@ -48,14 +53,19 @@ http://localhost
4853
2. Execute the following command
4954

5055
```bash
56+
$ task for-linux-env # Linux environment only
5157
$ task install
5258

5359
# or...
5460

61+
$ make for-linux-env # Linux environment only
5562
$ make install
5663

5764
# or...
5865

66+
$ echo "UID=$(id -u)" >> .env # Linux environment only
67+
$ echo "GID=$(id -g)" >> .env # Linux environment only
68+
5969
$ docker compose build
6070
$ docker compose up -d
6171
$ docker compose exec app composer install

Taskfile.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
version: '3'
22

33
tasks:
4+
for-linux-env:
5+
cmds:
6+
- echo "UID=$(id -u)" >> .env
7+
- echo "GID=$(id -g)" >> .env
8+
49
install:
510
cmds:
611
- docker compose build

compose.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
volumes:
22
db-store:
3-
psysh-store:
43

54
configs:
65
db-config:
@@ -11,16 +10,14 @@ services:
1110
build:
1211
context: .
1312
dockerfile: ./infra/docker/php/Dockerfile
13+
args:
14+
UID: ${UID:-1000}
15+
GID: ${GID:-1000}
1416
target: ${APP_BUILD_TARGET:-development}
1517
volumes:
1618
- type: bind
1719
source: ./src
1820
target: /workspace
19-
- type: volume
20-
source: psysh-store
21-
target: /root/.config/psysh
22-
volume:
23-
nocopy: true
2421
environment:
2522
# Please remove this environment variable, after created the Laravel project. Please write in .env
2623
- DB_CONNECTION=${DB_CONNECTION:-mysql}

infra/docker/php/Dockerfile

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ ENV TZ=UTC \
99
LANGUAGE=en_US:en \
1010
LC_ALL=en_US.UTF-8 \
1111
# composer environment
12-
COMPOSER_ALLOW_SUPERUSER=1 \
1312
COMPOSER_HOME=/composer
1413

14+
ARG UID=1000
15+
ARG GID=1000
16+
1517
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer
1618

1719
RUN <<EOF
@@ -22,48 +24,65 @@ RUN <<EOF
2224
unzip \
2325
libzip-dev \
2426
libicu-dev \
25-
libonig-dev
27+
libonig-dev \
28+
default-mysql-client
2629
locale-gen en_US.UTF-8
2730
localedef -f UTF-8 -i en_US en_US.UTF-8
2831
docker-php-ext-install \
2932
intl \
3033
pdo_mysql \
3134
zip \
3235
bcmath
33-
composer config -g process-timeout 3600
34-
composer config -g repos.packagist composer https://packagist.org
36+
# permission denied bind mount in Linux environment
37+
groupadd --gid $GID phper
38+
useradd --uid $UID --gid $GID phper
39+
mkdir /composer
40+
mkdir -p /home/phper/.config/psysh
41+
chown phper:phper /composer
42+
chown phper:phper /workspace
43+
chown phper:phper /home/phper/.config/psysh
3544
EOF
3645

3746
FROM base AS development
3847

3948
RUN <<EOF
40-
apt-get -y install --no-install-recommends \
41-
default-mysql-client
4249
apt-get clean
4350
rm -rf /var/lib/apt/lists/*
4451
EOF
4552

4653
COPY ./infra/docker/php/php.development.ini /usr/local/etc/php/php.ini
4754

48-
FROM development AS development-xdebug
55+
USER phper
56+
57+
FROM base AS development-xdebug
4958

5059
RUN <<EOF
5160
pecl install xdebug
5261
docker-php-ext-enable xdebug
62+
apt-get clean
63+
rm -rf /var/lib/apt/lists/*
5364
EOF
5465

5566
COPY ./infra/docker/php/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
5667

68+
USER phper
69+
5770
FROM base AS deploy
5871

5972
COPY ./infra/docker/php/php.deploy.ini /usr/local/etc/php/php.ini
60-
COPY ./src /workspace
73+
COPY --chown=phper:phper ./src /workspace
6174

6275
RUN <<EOF
63-
composer install -q -n --no-ansi --no-dev --no-scripts --no-progress --prefer-dist
76+
apt-get clean
77+
rm -rf /var/lib/apt/lists/*
78+
EOF
79+
80+
USER phper
81+
82+
RUN <<EOF
83+
composer install --quiet --no-interaction --no-ansi --no-dev --no-scripts --no-progress --prefer-dist
84+
composer dump-autoload --optimize
6485
chmod -R 777 storage bootstrap/cache
6586
php artisan optimize:clear
6687
php artisan optimize
67-
apt-get clean
68-
rm -rf /var/lib/apt/lists/*
6988
EOF

0 commit comments

Comments
 (0)