Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run the app container as a non-root user(nobody:nogroup) #263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/laravel-create-project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ jobs:
- name: Docker Version
run: docker version
- name: Docker Compose Settings
run: echo APP_BUILD_TARGET=development-xdebug > .env
run: |
echo APP_BUILD_TARGET=development-xdebug > .env
echo "UID=$(id -u)" >> .env
echo "GID=$(id -g)" >> .env
- name: Build Docker Images
run: docker compose build
- name: Create & Start Docker Containers
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/laravel-git-clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
- name: Docker Version
run: docker version
- name: Docker Compose Settings
run: echo APP_BUILD_TARGET=development-xdebug > .env
run: |
echo APP_BUILD_TARGET=development-xdebug > .env
echo "UID=$(id -u)" >> .env
echo "GID=$(id -g)" >> .env
- name: Build Docker Images
run: docker compose build
- name: Create & Start Docker Containers
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
for-linux-env:
echo "UID=$$(id -u)" >> .env
echo "GID=$$(id -g)" >> .env
install:
@make build
@make up
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Introduction

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

## Usage

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

```bash
$ task for-linux-env # Linux environment only
$ task create-project

# or...

$ make for-linux-env # Linux environment only
$ make create-project

# or...

$ echo "UID=$(id -u)" >> .env # Linux environment only
$ echo "GID=$(id -g)" >> .env # Linux environment only

$ mkdir -p src
$ docker compose build
$ docker compose up -d
Expand All @@ -48,14 +53,19 @@ http://localhost
2. Execute the following command

```bash
$ task for-linux-env # Linux environment only
$ task install

# or...

$ make for-linux-env # Linux environment only
$ make install

# or...

$ echo "UID=$(id -u)" >> .env # Linux environment only
$ echo "GID=$(id -g)" >> .env # Linux environment only

$ docker compose build
$ docker compose up -d
$ docker compose exec app composer install
Expand Down
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
version: '3'

tasks:
for-linux-env:
cmds:
- echo "UID=$(id -u)" >> .env
- echo "GID=$(id -g)" >> .env

install:
cmds:
- docker compose build
Expand Down
5 changes: 4 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ services:
build:
context: .
dockerfile: ./infra/docker/php/Dockerfile
args:
UID: ${UID:-65534}
GID: ${GID:-65534}
target: ${APP_BUILD_TARGET:-development}
volumes:
- type: bind
source: ./src
target: /workspace
- type: volume
source: psysh-store
target: /root/.config/psysh
target: /nonexistent/.config/psysh
volume:
nocopy: true
environment:
Expand Down
39 changes: 28 additions & 11 deletions infra/docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ ENV TZ=UTC \
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8 \
# composer environment
COMPOSER_ALLOW_SUPERUSER=1 \
COMPOSER_HOME=/composer

ARG UID=65534
ARG GID=65534

COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer

RUN <<EOF
Expand All @@ -22,48 +24,63 @@ RUN <<EOF
unzip \
libzip-dev \
libicu-dev \
libonig-dev
libonig-dev \
default-mysql-client
locale-gen en_US.UTF-8
localedef -f UTF-8 -i en_US en_US.UTF-8
docker-php-ext-install \
intl \
pdo_mysql \
zip \
bcmath
composer config -g process-timeout 3600
composer config -g repos.packagist composer https://packagist.org
# permission denied bind mount in Linux environment
groupadd --gid $GID nogroup
useradd --uid $UID --gid $GID nobody
mkdir /composer
chown nobody:nogroup /composer
chown nobody:nogroup /workspace
EOF

FROM base AS development

RUN <<EOF
apt-get -y install --no-install-recommends \
default-mysql-client
apt-get clean
rm -rf /var/lib/apt/lists/*
EOF

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

FROM development AS development-xdebug
USER nobody

FROM base AS development-xdebug

RUN <<EOF
pecl install xdebug
docker-php-ext-enable xdebug
apt-get clean
rm -rf /var/lib/apt/lists/*
EOF

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

USER nobody

FROM base AS deploy

COPY ./infra/docker/php/php.deploy.ini /usr/local/etc/php/php.ini
COPY ./src /workspace
COPY --chown=nobody:nogroup ./src /workspace

RUN <<EOF
composer install -q -n --no-ansi --no-dev --no-scripts --no-progress --prefer-dist
apt-get clean
rm -rf /var/lib/apt/lists/*
EOF

USER nobody

RUN <<EOF
composer install --quiet --no-interaction --no-ansi --no-dev --no-scripts --no-progress --prefer-dist
composer dump-autoload --optimize
chmod -R 777 storage bootstrap/cache
php artisan optimize:clear
php artisan optimize
apt-get clean
rm -rf /var/lib/apt/lists/*
EOF
Loading