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

[BUILD] fix docker configuration for the Quickstart application #90

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
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic
Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fixed Quickstart app so that it runs in a Docker container or with a local PHP server

## [v1.0.5] - 2024-10-14
### Fixed
Expand Down
15 changes: 0 additions & 15 deletions Dockerfile

This file was deleted.

80 changes: 24 additions & 56 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
ARGS_PHPUNIT ?=

DOCKER := $(if $(LRN_SDK_NO_DOCKER),,$(shell which docker))
DOCKER_COMPOSE := docker compose

# PHP Evolution
SUPPORTED_PHP_VERSIONS = 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3
PHP_VERSION = $(lastword ${SUPPORTED_PHP_VERSIONS})
PHP_VERSION ?= $(lastword ${SUPPORTED_PHP_VERSIONS})
DEBIAN_VERSION-7.1 = buster
DEBIAN_VERSION-7.2 = buster
DEBIAN_VERSION-7.3 = bullseye
DEBIAN_VERSION-7.4 = bullseye
DEBIAN_VERSION-8.0 = bullseye
DEBIAN_VERSION-def = bookworm
DEBIAN_VERSION = $(or $(DEBIAN_VERSION-$(PHP_VERSION)),$(DEBIAN_VERSION-def))
DEBIAN_VERSION ?= $(or $(DEBIAN_VERSION-$(PHP_VERSION)),$(DEBIAN_VERSION-def))
COMPOSER_VERSION-7.1 = 2.2
COMPOSER_VERSION-def = 2.7.6
COMPOSER_VERSION = $(or $(COMPOSER_VERSION-$(PHP_VERSION)),$(COMPOSER_VERSION-def))
LOCALHOST = 0.0.0.0

IMAGE = php-cli-composer:$(PHP_VERSION)-$(DEBIAN_VERSION)-$(COMPOSER_VERSION)
COMPOSER_VERSION ?= $(or $(COMPOSER_VERSION-$(PHP_VERSION)),$(COMPOSER_VERSION-def))

TARGETS = all build devbuild prodbuild \
quickstart check-quickstart install-vendor \
dist dist-test dist-zip release \
lint test test-coverage test-integration-env test-unit \
clean clean-dist clean-test clean-vendor

.PHONY: $(TARGETS)
.default: all

ifneq (,$(DOCKER))
TTYFLAGS := $(shell if [ -t 0 ] ; then echo "-it"; else echo "-t"; fi)
# Re-run the make command in a container
DKR = docker container run $(TTYFLAGS) --rm \
-v $(CURDIR):/srv/sdk/php:z,delegated \
-v lrn-sdk-php_cache:/root/.composer \
-w /srv/sdk/php \
-e LRN_SDK_NO_DOCKER=1 \
-e ENV -e REGION -e VER \
-p 8000:8000 \
$(if $(findstring dev,$(ENV)),--net host) \
$(IMAGE)

$(TARGETS): $(if $(shell docker image ls -q --filter reference=$(IMAGE)),,docker-build)
$(DKR) make -e MAKEFLAGS="$(MAKEFLAGS)" $@

docker-build:
docker image build \
--progress plain \
--build-arg PHP_VERSION=$(PHP_VERSION) \
--build-arg DEBIAN_VERSION=$(DEBIAN_VERSION) \
--build-arg COMPOSER_VERSION=$(COMPOSER_VERSION) \
-t $(IMAGE) .
.PHONY: docker-build lrn-test-all lrn-test-clean


else
docker-build: install-vendor
$(DOCKER_COMPOSE) build php nginx

.PHONY: docker-build

# Local development targets without Docker
DIST_PREFIX = learnosity_sdk-
SRC_VERSION := $(shell git describe | sed s/^v//)
DIST = $(DIST_PREFIX)$(SRC_VERSION)
Expand All @@ -64,15 +42,19 @@ COMPOSER_INSTALL_FLAGS = --no-interaction --optimize-autoloader --classmap-autho
PHPCS= ./vendor/bin/phpcs
PHPUNIT = ./vendor/bin/phpunit

###
# quickstart rules
###
quickstart: VENDOR_FLAGS = --no-dev
quickstart: install-vendor
cd docs/quickstart && php -S $(LOCALHOST):8000
quickstart: $(if $(DOCKER),docker-build) $(if $(DOCKER),docker,local)-quickstart

docker-quickstart: VENDOR_FLAGS = --no-dev
docker-quickstart: install-vendor
$(DOCKER_COMPOSE) up -d

local-quickstart: VENDOR_FLAGS = --no-dev
local-quickstart: install-vendor
php -S localhost:8000 -t docs/quickstart

check-quickstart: vendor/autoload.php
$(COMPOSER) install $(COMPOSER_INSTALL_FLAGS) --no-dev;

###
# internal tooling rules
####
Expand Down Expand Up @@ -103,23 +85,18 @@ test-integration-env: build

###
# dist rules
#
# build a dist zip file from the distdir, THEN run the tests in the dist dir,
# to avoid polluting the distfile with dev dependencies
###
dist: dist-test

# We want to clean first before copying into the .distdir so that we have a clean copy
dist-zip: clean-test clean-dist
mkdir -p .$(DIST) # use a hidden directory so that it doesn't get copied into itself
mkdir -p .$(DIST)
cp -R * .version .$(DIST)
mv .$(DIST) $(DIST)
rm -rf $(DIST)/vendor/
$(COMPOSER) install --working-dir=$(DIST) $(COMPOSER_INSTALL_FLAGS) --no-dev
rm -rf $(DIST)/release.sh
zip -qr $(DIST).zip $(DIST)

# run tests in the distdir
dist-test: dist-zip install-vendor
$(PHPUNIT) --do-not-cache-result --no-logging --configuration=$(DIST)/phpunit.xml

Expand All @@ -132,6 +109,7 @@ composer.lock: composer.json

clean: clean-dist clean-test clean-vendor
rm -rf $(DIST_PREFIX)*.zip
$(DOCKER_COMPOSE) down -v

clean-dist:
rm -rf $(DIST_PREFIX)*/
Expand All @@ -145,16 +123,7 @@ clean-vendor:
rm -rf vendor
rm -f composer.lock

# Aliases

devbuild: build
prodbuild: dist

# The following are real targets, not phony ones

vendor:
$(COMPOSER) install $(COMPOSER_INSTALL_FLAGS)

# Package contents
PKG_CONTENTS = .version \
CONTRIBUTING.md LICENSE.md README.md REFERENCE.md ChangeLog.md \
composer.json bootstrap.php phpunit.xml \
Expand All @@ -169,4 +138,3 @@ $(DIST)/vendor: $(DIST)

$(DIST).zip: $(DIST)/vendor
zip -qr $(DIST).zip $(DIST)
endif
46 changes: 38 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,44 @@ For production use, you should install the SDK using the Composer package manage
## Quick start guide
Let's take a look at a simple example of the SDK in action. In this example, we'll load an assessment into the browser.

### **Start up your web server**
You can start the demo by running the following command in the SDK directory:

make quickstart

If your PHP server is up, we'll assume that your web server is available at this local address:

http://localhost:8000
### Running the Quickstart Application

### **Option 1: Running with Docker (Recommended)**
To run the quickstart application using Docker, ensure you have Docker and Docker Compose installed on your system. Then follow these steps:

1. Clone the repository (if you haven't already):

```bash
git clone https://github.com/Learnosity/learnosity-sdk-php.git
cd learnosity-sdk-php
```

2. Start the Docker containers:

```bash
make quickstart
```

### **Option 2: Running Locally**
If you prefer to run the application locally without Docker, follow these prerequisites:

1. Clone the repository (if you haven't already):

```bash
git clone https://github.com/Learnosity/learnosity-sdk-php.git
cd learnosity-sdk-php
```

2. Ensure you have PHP 8.x installed
3. Install Composer (https://getcomposer.org/download/)

4. Start the built-in PHP development server:

```bash
make quickstart
```

**Note:** Local setup requires PHP 8 runtime libraries and may have additional system dependencies. The Docker method provides a more consistent and isolated environment.

(For more information about the web server configuration, [click here](https://help.learnosity.com/hc/en-us/articles/360000757757-Environment-Setup-Guide))

Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.8'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obsolete.

Suggested change
version: '3.8'


networks:
quickstart-network:
driver: bridge

services:
nginx:
container_name: learnosity-php-sdk-nginx
build:
context: .
dockerfile: docker/nginx/Dockerfile
ports:
- "8000:8000"
volumes:
- .:/var/www/html
environment:
APPLICATION_SERVER_LANGUAGE: php
UPSTREAM_APPLICATION_SERVICE: php:9000
depends_on:
- php
networks:
quickstart-network:
aliases:
- nginx

php:
container_name: learnosity-php-sdk-php-frm
build:
context: .
dockerfile: docker/php/Dockerfile
args:
PHP_VERSION: ${PHP_VERSION:-8.3}
DEBIAN_VERSION: ${DEBIAN_VERSION:-bookworm}
COMPOSER_VERSION: ${COMPOSER_VERSION:-2.7.6}
volumes:
- .:/var/www/html
environment:
- SERVER_NAME=localhost
networks:
quickstart-network:
aliases:
- php
11 changes: 11 additions & 0 deletions docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM nginx:stable-alpine

# Copy custom nginx configuration
COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf

WORKDIR /var/www/html

# Optional: Add any additional Nginx-specific configurations or setup
EXPOSE 8000

CMD ["nginx", "-g", "daemon off;"]
19 changes: 19 additions & 0 deletions docker/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server {
listen 8000;
server_name localhost;
root /var/www/html/docs/quickstart;
index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
24 changes: 24 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ARG PHP_VERSION=8.3
ARG DEBIAN_VERSION=bookworm
ARG COMPOSER_VERSION=2.7.6

FROM php:${PHP_VERSION}-fpm-${DEBIAN_VERSION}

# Install necessary dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
git=1:2.* \
libzip-dev=1.* \
unzip=6.0* \
zip=3.0* \
&& docker-php-ext-install zip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Configure PHP-FPM to listen on TCP/IP
RUN sed -i 's/listen = \/run\/php-fpm.sock/listen = 127.0.0.1:9000/g' /usr/local/etc/php-fpm.d/www.conf

WORKDIR /var/www/html

EXPOSE 9000

CMD ["php-fpm"]
Loading