Skip to content

Commit

Permalink
Initial Release (#2)
Browse files Browse the repository at this point in the history
Merging develop to master for the initial release.
  • Loading branch information
Pezhvak authored Feb 3, 2021
1 parent acf1813 commit 8624a3b
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
ARG PHP_VERSION=8.0.1
FROM php:${PHP_VERSION}-alpine
LABEL maintainer="Pezhvak <[email protected]>"
# NOTE: ARGs before FROM cannot be accessed during build time (https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact)
ARG COMPOSER_VERSION=2
ARG USER_ID=1235
ARG GROUP_ID=1235
ENV USER_ID=${USER_ID}
ENV GROUP_ID=${GROUP_ID}

# Setting up
WORKDIR /var/www/html
RUN addgroup -g ${GROUP_ID} deployer
RUN adduser -DHS -u ${USER_ID} -G deployer deployer
RUN mkdir /etc/sudoers.d/ && echo 'deployer ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/deployer

# Copy PHP Extension Installer (https://github.com/mlocati/docker-php-extension-installer)
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

# Copy Scripts
COPY scripts /tmp
RUN chmod +x /tmp/*.sh
COPY scripts/start-container /usr/local/bin
RUN chmod +x /usr/local/bin/start-container

# Install
RUN ash /tmp/install-packages.sh
RUN ash /tmp/install-php.sh

# Cleanup
RUN ash /tmp/cleanup.sh
RUN rm -rf /tmp/*

# Serving
EXPOSE 80
ENTRYPOINT ["start-container"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Pezhvak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
143 changes: 141 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,141 @@
# app
A lightweight Laravel app image based on Alpine
<div align="center">

# Laravel App Container

![GitHub](https://img.shields.io/github/license/laramatics/app)
![Docker Image Size (tag)](https://img.shields.io/docker/image-size/laramatics/app/latest)
![Docker Pulls](https://img.shields.io/docker/pulls/laramatics/app)
![Docker Image Version (latest semver)](https://img.shields.io/docker/v/laramatics/app)
![Docker Cloud Automated build](https://img.shields.io/docker/cloud/automated/laramatics/app)
![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/laramatics/app)

</div>

## About

This repository contains a `Dockerfile` which builds an [image](https://hub.docker.com/r/laramatics/app)
for serving your Laravel app.

### Table of Contents

- [Usage](#usage)
- [Folder Structure](#folder-structure)
- [Packages and Services](#packages-and-services)
- [Customizing build versions](#customizing-build-versions)
- [Adding more PHP extensions](#adding-more-php-extensions)
- [Adding more packages](#adding-more-packages)
- [Testing](#testing)
- [References](#references)

## Usage

Using the image is straight-forward and easy to use, create a `Dockerfile` in your app and copy your files into the
container, do whatever is necessary:

```dockerfile
FROM laramatics/app:latest

# (optional) copy your own configurations to the container
COPY /docker/config/php.ini "$PHP_INI_DIR/conf.d/laramatics-app.ini"

# copy app to the container
COPY ./ /var/www/html
RUN chown -R $USER_ID:$USER_GROUP /var/www/html
```

Once your files are added to the container, you will have to build an image from your `Dockerfile`:

```bash
docker build -t <image_name> .
```

All done! run your container and enjoy!

## Folder Structure

Although folder structure is self-explanatory, description is below:

```
.
├── Dockerfile
├── LICENSE
├── readme.md
├── scripts
│   ├── cleanup.sh # Removes build dependencies for lighter image size.
│   ├── install-packages.sh # OS packages will be installed by this file.
│   ├── install-php.sh # PHP extensions and installation.
│   └── start-container # Container entry-point script
└── tests
└── goss.yaml # See "testing" section
```

## Packages and Services

We created the `Dockerfile` with image size in mind, so only packages and PHP extensions which are absolutely necessary
are installed.

|Service|Version|Argument|
|---|:---:|:---:|
|PHP|8.0.1|`PHP_VERSION`|
|Composer|2.0.9|`COMPOSER_VERSION`|

### Customizing build versions

As you can see in the table above, some services have an argument in `Dockerfile` for you to modify the installation version.
To do so, you need to clone the repo and build the image yourself:

```bash
git clone https://github.com/laramatics/app.git
cd app
# Modify files...
docker build \
--build-args USER_ID=1235
--build-args GROUP_ID=1235
--build-arg PHP_VERSION=8.0.1 \
--build-arg COMPOSER_VERSION=2 \
-t <image_name> .
```

***Note:*** By default `deployer` user inside the container will use `uid` and `gid` of `1235`, you can change that to
match your own setup, the username doesn't matter, once you bind `/var/www/html` to your docker host, the only thing
that matters is `uid` and `gid` of the files (that's how linux works).

### Adding more PHP extensions

If you want to add more extensions to the PHP installation, you will have to build your own image based on the one
already built or modify the `Dockerfile` and `scripts/*` to your liking and build your own image from that as
described [here](#adding-more-packages).

See [Docker PHP Extension Installer](https://github.com/mlocati/docker-php-extension-installer)
for available extensions, however you can also install them from the source.

```dockerfile
FROM laramatics/app:latest
# add your extentions here...
RUN docker-php-ext-install -j "$(nproc)" <package_name>
```

### Adding more packages

Sometimes you need a specific package for your pipeline; as described in the previous section, you can build your own
image from `laramatics/app` or clone this repo and modify files to suit your needs.

```shell
git clone https://github.com/laramatics/app.git
cd app
# Modify files...
docker build -t <image_name> .
```

### Testing

Tests are written using [GOSS](https://github.com/aelsabbahy/goss/tree/master/extras/dcgoss), to test your changes after
modifying source files and building your own image, run:

```shell
GOSS_FILES_PATH=tests dgoss run -it <image_name> /bin/ash -l
```

# References

- [Useful gist](https://gist.github.com/avishayp/33fcee06ee440524d21600e2e817b6b7)
6 changes: 6 additions & 0 deletions scripts/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ash

# Remove Build Dependencies
apk del -f .build-deps
rm /usr/local/bin/install-php-extensions
rm /usr/local/bin/docker-php-*
18 changes: 18 additions & 0 deletions scripts/install-packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ash
apk --update add --no-cache --virtual .build-deps \
gnupg \
zlib-dev \
libjpeg-turbo-dev \
libpng-dev \
libxml2-dev \
bzip2-dev \
libzip-dev

# Add Production Dependencies
apk add --update --no-cache \
freetype-dev \
mysql-client \
libwebp-dev \
icu-dev \
su-exec \
sudo
24 changes: 24 additions & 0 deletions scripts/install-php.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ash

# Installing Composer
install-php-extensions @composer-${COMPOSER_VERSION}

# Installing PHP Extensions
docker-php-ext-configure opcache --enable-opcache &&
docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp &&
docker-php-ext-configure zip &&
docker-php-ext-install -j "$(nproc)" \
mysqli \
pdo_mysql \
sockets \
intl \
bz2 \
pcntl \
bcmath \
exif

# note: for some reason if we build gd with the rest of the extensions it will trow an error in php -v
docker-php-ext-install -j "$(nproc)" gd

# Enable production environment
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
7 changes: 7 additions & 0 deletions scripts/start-container
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ash

if [ $# -gt 0 ]; then
exec su-exec $USER_ID:$GROUP_ID "$@"
else
exec su-exec $USER_ID:$GROUP_ID sudo /usr/local/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80
fi
52 changes: 52 additions & 0 deletions tests/goss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
command:
composer --version:
exit-status: 0
php --version:
exit-status: 0
php -m:
exit-status: 0
stdout:
- exif
- bcmath
- bz2
- Core
- ctype
- curl
- date
- dom
- fileinfo
- filter
- ftp
- gd
- hash
- iconv
- intl
- json
- libxml
- mbstring
- mysqli
- mysqlnd
- openssl
- pcntl
- pcre
- PDO
- pdo_mysql
- pdo_sqlite
- Phar
- posix
- readline
- Reflection
- session
- SimpleXML
- sockets
- sodium
- SPL
- sqlite3
- standard
- tokenizer
- xml
- xmlreader
- xmlwriter
- zlib
stderr: [ ]
timeout: 10000

0 comments on commit 8624a3b

Please sign in to comment.