Skip to content

Commit

Permalink
Change (docker): Combine nginx and php to a single image for both `AS…
Browse files Browse the repository at this point in the history
…P` with env var support for configuration

In <= v3.2.0, `asp` had separate `nginx` and `php` images.

Now: `asp` image containing both `nginx` and `php`, with environment variable support, and entrypoint that sets the correct permissions.

Benefits:

- Easier to deploy / upgrade. No need to separate `nginx` and `php` containers
- Environment variable configuration means no more need to mount config into `asp` container
- Entrypoint script sets permissions on volumes. `init-container` should only need to set permissions for `db` volume

Notable changes:
- Add documentation for upgrading docker images to v3.3.x from prior versions
- Update `./docs/examples`
- Add tests for production builds for examples in `./docs/examples`

Closes #69
  • Loading branch information
leojonathanoh committed Nov 7, 2023
1 parent 29a0927 commit a0ed8ce
Show file tree
Hide file tree
Showing 31 changed files with 642 additions and 1,992 deletions.
76 changes: 37 additions & 39 deletions .github/workflows/ci-master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,23 @@ jobs:
matrix:
testenv:
- dev
- prod
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- 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-
${{ runner.os }}-buildx-
# This is commented out, so we use the default 'docker' driver instead of the 'docker-container' driver. When using 'docker-container' driver, there appears to be a rate limit on writes on Github CI which causes buildx to fail with error code 17 when it is exporting to cache
# - name: Set up Docker Buildx
# id: buildx
# uses: docker/setup-buildx-action@v2

- name: Cache Docker layers (php)
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-php
key: ${{ runner.os }}-buildx-php-${{ github.sha }}
path: /tmp/.buildx-asp-cache-nginx
key: ${{ runner.os }}-buildx-asp-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-php-
${{ runner.os }}-buildx-
- name: Print buildx and compose
Expand All @@ -54,14 +46,21 @@ jobs:
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
docker compose -f docker-compose.test.yml --profile dev up
- name: Integration test 1 (prod)
if: matrix.testenv == 'prod'
run: |
set -eux
(
cd docs/full-bf2-stack-example
sed -i '$!N;s@ports:\n - 53:53.*@@;P;D' docker-compose.yml # Remove coredns ports because it conflicts with system-resolved on the host
docker compose -f docker-compose.yml -f docker-compose.build.prod.yml up --build -d
)
docker compose -f docker-compose.test.yml --profile prod up
docker compose -f docker-compose.test.yml --profile dns up
build:
strategy:
matrix:
variant:
- nginx
- php
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -91,10 +90,9 @@ jobs:
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-${{ matrix.variant }}
key: ${{ runner.os }}-buildx-${{ matrix.variant }}-${{ github.sha }}
path: /tmp/.buildx-cache-asp
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.variant }}-
${{ runner.os }}-buildx-
# This step generates the docker tags
Expand All @@ -108,12 +106,12 @@ jobs:
# type=ref,event=branch generates tag(s) on branch only. E.g. 'master-<variant>', 'master-abc0123-<variant>'
# type=ref,event=tag generates tag(s) on tags only. E.g. 'v0.0.0-<variant>', 'v0.0.0-abc0123-<variant>'
tags: |
type=ref,suffix=-${{ matrix.variant }},event=pr
type=ref,suffix=-{{sha}}-${{ matrix.variant }},event=pr
type=ref,suffix=-${{ matrix.variant }},event=branch
type=ref,suffix=-{{sha}}-${{ matrix.variant }},event=branch
type=ref,suffix=-${{ matrix.variant }},event=tag
type=ref,suffix=-{{sha}}-${{ matrix.variant }},event=tag
type=ref,suffix=,event=pr
type=ref,suffix=-{{sha}},event=pr
type=ref,suffix=,event=branch
type=ref,suffix=-{{sha}},event=branch
type=ref,suffix=,event=tag
type=ref,suffix=-{{sha}},event=tag
# Disable 'latest' tag
flavor: |
latest=false
Expand All @@ -131,36 +129,36 @@ jobs:
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v3
with:
file: Dockerfile.${{ matrix.variant }}
file: Dockerfile
context: '.'
target: prod
platforms: linux/amd64
push: false
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache-${{ matrix.variant }}
cache-to: type=local,dest=/tmp/.buildx-cache-${{ matrix.variant }}-new,mode=max
cache-from: type=local,src=/tmp/.buildx-cache-asp
cache-to: type=local,dest=/tmp/.buildx-cache-asp-new,mode=max

- name: Build and push
# Run on master and tags
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')
uses: docker/build-push-action@v3
with:
file: Dockerfile.${{ matrix.variant }}
file: Dockerfile
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 }}
cache-from: type=local,src=/tmp/.buildx-cache-${{ matrix.variant }}
cache-to: type=local,dest=/tmp/.buildx-cache-${{ matrix.variant }}-new,mode=max
cache-from: type=local,src=/tmp/.buildx-cache-asp
cache-to: type=local,dest=/tmp/.buildx-cache-asp-new,mode=max

# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache-${{ matrix.variant }}
mv /tmp/.buildx-cache-${{ matrix.variant }}-new /tmp/.buildx-cache-${{ matrix.variant }}
rm -rf /tmp/.buildx-cache-asp
mv /tmp/.buildx-cache-asp-new /tmp/.buildx-cache-asp
update-draft-release:
needs: [test, build]
Expand Down
32 changes: 27 additions & 5 deletions Dockerfile.php → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
FROM $IMAGE AS build

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

FROM $IMAGE AS dev

# Install nginx and supervisor for multi-process container
RUN apk add --no-cache ca-certificates nginx supervisor

# opcache
RUN docker-php-ext-install opcache

Expand Down Expand Up @@ -37,9 +40,28 @@
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
# Add configs
COPY ./config/ASP/. /
COPY ./src/ASP/system/config /config.sample
RUN chmod +x /docker-entrypoint.sh;
# Disable the built-in php-fpm configs, since we're using our own config
RUN set -eux; \
mv -v /usr/local/etc/php-fpm.d/docker.conf /usr/local/etc/php-fpm.d/docker.conf.disabled; \
mv -v /usr/local/etc/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/www.conf.disabled; \
mv -v /usr/local/etc/php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf.disabled;

# In docker, IPs may be dynamic. This ensures we get access
ENV ADMIN_HOSTS=0.0.0.0/0
VOLUME /src/ASP/system/backups
VOLUME /src/ASP/system/cache
VOLUME /src/ASP/system/config
VOLUME /src/ASP/system/logs
VOLUME /src/ASP/system/snapshots
EXPOSE 80
EXPOSE 9000
WORKDIR /src/ASP
ENTRYPOINT []
CMD ["/docker-entrypoint.sh"]

FROM dev AS prod

Expand Down
18 changes: 0 additions & 18 deletions Dockerfile.nginx

This file was deleted.

32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

[![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/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)
[![docker-image-size](https://img.shields.io/docker/image-size/startersclan/bf2stats/master?label=asp)](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

## Usage
## Usage (docker)

```sh
docker pull startersclan/asp:3.2.0-nginx
docker pull startersclan/asp:3.2.0-php
docker run --rm -it -p 80:80 -e DB_HOST=db -e DB_HOST=db -e DB_PORT=3306 -e DB_NAME=bf2stats -e DB_USER=admin -e DB_PASS=admin startersclan/asp:3.3.0
```

See [this](docs/full-bf2-stack-example) example showing how to deploy [Battlefield 2 1.5 server](https://github.com/startersclan/docker-bf2), [PRMasterserver](https://github.com/startersclan/PRMasterServer) as the master server, and `ASP` as the stats web server, using `docker-compose`.

### Upgrading to v2.6.0 from prior versions

See here for [quick instructions](docs/upgrading-docker-images-to-2.6.md).

## Development

```sh
Expand All @@ -33,12 +35,24 @@ code --install-extension ms-python.python # Python intellisense
# Execute this to allow php to reach the host machine via the docker0 bridge
sudo iptables -A INPUT -i br+ -j ACCEPT

# asp - Exec into container
docker exec -it $( docker-compose ps -q asp ) sh
# asp - List backups
docker exec -it $( docker-compose ps -q asp ) ls -al /src/ASP/system/backups
# asp - List cache
docker exec -it $( docker-compose ps -q asp ) ls -al /src/ASP/system/cache
# asp - List logs
docker exec -it $( docker-compose ps -q asp ) ls -al /src/ASP/system/logs
# asp - List snapshots
docker exec -it $( docker-compose ps -q asp ) ls -alR /src/ASP/system/snapshots/

# Test routes
docker-compose -f docker-compose.test.yml up
docker-compose -f docker-compose.test.yml --profile dev up

# Test production builds locally
docker build -t startersclan/asp:nginx -f Dockerfile.nginx --target prod .
docker build -t startersclan/asp:php -f Dockerfile.php --target prod .
# Test production builds
(cd docs/full-bf2-stack-example && docker compose -f docker-compose.yml -f docker-compose.build.prod.yml up --build)
docker compose -f docker-compose.test.yml --profile prod up
docker compose -f docker-compose.test.yml --profile dns up

# Dump the DB
docker exec $( docker-compose ps | grep db | awk '{print $1}' ) mysqldump -uroot -pascent bf2stats | gzip > bf2stats.sql.gz
Expand Down
Loading

0 comments on commit a0ed8ce

Please sign in to comment.