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

Enhancement (ci): Refactor Dockerfiles to reduce image size #66

Merged
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
39 changes: 34 additions & 5 deletions .github/workflows/ci-master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,38 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Test (integration)
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers (nginx)
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-nginx
key: ${{ runner.os }}-buildx-nginx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-nginx-

- name: Cache Docker layers (php)
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-php
key: ${{ runner.os }}-buildx-php-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-php-

- name: Print buildx and compose
run: |
set -eu
docker-compose up --build -d
docker-compose -f docker-compose.test.yml up
docker buildx ls
docker compose version

- name: Integration test (dev)
if: matrix.testenv == 'dev'
run: |
set -eux
docker compose -f docker-compose.yml -f docker-compose.build.yml up --build -d
docker compose -f docker-compose.test.yml up

build:
strategy:
Expand Down Expand Up @@ -98,8 +125,9 @@ jobs:
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v3
with:
file: Dockerfile.${{ matrix.variant }}.prod
file: Dockerfile.${{ matrix.variant }}
context: '.'
target: prod
platforms: linux/amd64
push: false
tags: ${{ steps.meta.outputs.tags }}
Expand All @@ -111,8 +139,9 @@ jobs:
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')
uses: docker/build-push-action@v3
with:
file: Dockerfile.${{ matrix.variant }}.prod
file: Dockerfile.${{ matrix.variant }}
context: '.'
target: prod
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/s390x
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"port": 9000,
"pathMappings": {
"/src": "${workspaceRoot}/src",
"/src/ASP/system/config/config.php": "${workspaceRoot}/config/ASP/config.php",
},
"xdebugSettings": {
"max_data": 10000,
Expand Down
13 changes: 7 additions & 6 deletions Dockerfile.nginx.prod → Dockerfile.nginx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
FROM nginx:1.21-alpine AS build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM"
ARG IMAGE=nginx:1.21-alpine
FROM $IMAGE AS build

# Set permissions for 'nginx' user
COPY ./src /src
Expand All @@ -10,8 +8,11 @@ RUN chown -R nginx:nginx . \
&& find . -type d -exec chmod 750 {} \; \
&& find . -type f -exec chmod 640 {} \;

FROM nginx:1.21-alpine AS final
FROM $IMAGE AS dev

# Add default configs
COPY --from=build /src /src
COPY config/ASP/nginx/nginx.conf /etc/nginx/nginx.conf

FROM dev AS prod

COPY --from=build /src /src
33 changes: 29 additions & 4 deletions Dockerfile.php.dev → Dockerfile.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
FROM php:7.4-fpm-alpine
ARG IMAGE=php:7.4-fpm-alpine
FROM $IMAGE AS build

# Set permissions for 'www-data' user
COPY ./src /src
WORKDIR /src
RUN chown -R www-data:www-data . \
&& find . -type d -exec chmod 750 {} \; \
&& find . -type f -exec chmod 640 {} \;

FROM $IMAGE AS dev

# opcache
RUN docker-php-ext-install opcache
Expand All @@ -9,7 +19,7 @@
# Xdebug: https://stackoverflow.com/questions/46825502/how-do-i-install-xdebug-on-dockers-official-php-fpm-alpine-image
# PHPIZE_DEPS: autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c
RUN apk add --no-cache --virtual .build-dependencies $PHPIZE_DEPS \
&& pecl install xdebug-3.1.5 \
&& pecl install xdebug-3.1.6 \
&& docker-php-ext-enable xdebug \
&& docker-php-source delete \
&& apk del .build-dependencies
Expand All @@ -22,5 +32,20 @@
echo "xdebug.client_port=9000"; \
} > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini;

RUN php -i
RUN php -m
RUN set -eux; \
echo; \
php -i; \
php -m

# Add default configs
COPY ./config/ASP/php/conf.d/php.ini /usr/local/etc/php/conf.d/php.ini
COPY ./config/ASP/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/www.conf

FROM dev AS prod

# Disable xdebug
RUN set -eux; \
rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \
php -m;

COPY --from=build /src /src
27 changes: 0 additions & 27 deletions Dockerfile.php.prod

This file was deleted.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![github-actions](https://github.com/startersclan/asp/workflows/ci-master-pr/badge.svg)](https://github.com/startersclan/asp/actions)
[![github-release](https://img.shields.io/github/v/release/startersclan/asp?style=flat-square)](https://github.com/startersclan/asp/releases/)
[![docker-image-size](https://img.shields.io/docker/image-size/startersclan/asp/nginx)](https://hub.docker.com/r/startersclan/asp)
[![docker-image-size](https://img.shields.io/docker/image-size/startersclan/asp/master-nginx?label=nginx)](https://hub.docker.com/r/startersclan/asp)
[![docker-image-size](https://img.shields.io/docker/image-size/startersclan/asp/master-php?label=php)](https://hub.docker.com/r/startersclan/asp)

The new BF2Statistics 3.0 ASP, currently in public Beta. The GameSpy server to match is over at https://github.com/BF2Statistics/BattleSpy

Expand All @@ -19,7 +20,7 @@ See [this](docs/full-bf2-stack-example) example showing how to deploy [Battlefie

```sh
# Start
docker-compose up
docker-compose up --build
# ASP available at http://localhost:8081/ASP. Username: admin, password admin. See ./config/ASP/config.php
# phpmyadmin available at http://localhost:8083. Username: root, password: ascent. See ./config/ASP/config.php config file

Expand All @@ -36,8 +37,8 @@ sudo iptables -A INPUT -i br+ -j ACCEPT
docker-compose -f docker-compose.test.yml up

# Test production builds locally
docker build -t startersclan/asp:nginx -f Dockerfile.nginx.prod .
docker build -t startersclan/asp:php -f Dockerfile.php.prod .
docker build -t startersclan/asp:nginx -f Dockerfile.nginx --target prod .
docker build -t startersclan/asp:php -f Dockerfile.php --target prod .

# Dump the DB
docker exec $( docker-compose ps | grep db | awk '{print $1}' ) mysqldump -uroot -pascent bf2stats | gzip > bf2stats.sql.gz
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This is a docker compose override file, for development builds with caching for CI environments
services:
asp-nginx:
build:
cache_from:
- type=local,src=/tmp/.buildx-cache-web
cache_to:
- type=local,dest=/tmp/.buildx-cache-web,mode=max

asp-php:
build:
cache_from:
- type=local,src=/tmp/.buildx-cache-php
cache_to:
- type=local,dest=/tmp/.buildx-cache-php,mode=max
14 changes: 9 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ services:
chown -R 999:999 /var/lib/mysql

asp-nginx:
image: nginx:1.21-alpine
build:
dockerfile: Dockerfile.nginx
context: .
target: dev
volumes:
- ./src:/src
- ./src:/src:ro
- ./config/ASP/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- 8081:80
Expand All @@ -63,16 +66,17 @@ services:

asp-php:
build:
dockerfile: Dockerfile.php.dev
dockerfile: Dockerfile.php
context: .
target: dev
volumes:
- ./src:/src
- ./config/ASP/config.php:/src/ASP/system/config/config.php # Main config file. Must be writeable or else ASP will throw an exception. Customize as needed
# - ./config/ASP/armyAbbreviationMap.php:/src/ASP/system/config/armyAbbreviationMap.php:ro # Optional: Customize as needed if using a custom mod
# - ./config/ASP/backendAwards.php:/src/ASP/system/config/backendAwards.php:ro # Optional: Customize as needed if using a custom mod
# - ./config/ASP/ranks.php:/src/ASP/system/config/ranks.php:ro # Optional: Customize as needed if using a custom mod
- ./config/ASP/php/conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
- ./config/ASP/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
# - ./config/ASP/php/conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
# - ./config/ASP/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
- backups-volume:/src/ASP/system/backups # This volume is effectively unused since ASP doesn't allow DB backups for a remote DB, but mount it anyway to avoid errors.
- cache-volume:/src/ASP/system/cache
- logs-volume:/src/ASP/system/logs
Expand Down