diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b3c0dd2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,35 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose* +!**/composer.json +!**/composer.lock +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/vendor +LICENSE +README.md diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..481ac8f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,30 @@ +name: ci + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v4 + - + name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - + name: Set up Docker Buildx + uses: docker/build-push-action@v5 + with: + context: . + push: true + target: final + tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8d61502 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +# syntax=docker/dockerfile:1 +# Create a stage for installing app dependencies defined in Composer. +FROM composer:lts as prod-deps +WORKDIR /app +RUN --mount=type=bind,source=./composer.json,target=composer.json \ + --mount=type=bind,source=./composer.lock,target=composer.lock \ + --mount=type=cache,target=/tmp/cache \ + composer install --no-dev --no-interaction + +FROM composer:lts as dev-deps +WORKDIR /app +RUN --mount=type=bind,source=./composer.json,target=composer.json \ + --mount=type=bind,source=./composer.lock,target=composer.lock \ + --mount=type=cache,target=/tmp/cache \ + composer install --no-interaction + +FROM php:8.2-apache as base +RUN docker-php-ext-install pdo pdo_mysql +COPY ./src /var/www/html + +FROM base as development +COPY ./tests /var/www/html/tests +RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" +COPY --from=dev-deps app/vendor/ /var/www/html/vendor + +FROM development as test +WORKDIR /var/www/html +RUN ./vendor/bin/phpunit tests/HelloWorldTest.php + +FROM base as final +RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" +COPY --from=prod-deps app/vendor/ /var/www/html/vendor +USER www-data diff --git a/Dockerfile copy b/Dockerfile copy new file mode 100644 index 0000000..3ea13c4 --- /dev/null +++ b/Dockerfile copy @@ -0,0 +1,58 @@ +# syntax=docker/dockerfile:1 +# Create a stage for installing app dependencies defined in Composer. +FROM composer:lts as deps +WORKDIR /app + +# If your composer.json file defines scripts that run during dependency installation and +# reference your application source files, uncomment the line below to copy all the files +# into this layer. +# COPY . . + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a bind mounts to composer.json and composer.lock to avoid having to copy them +# into this layer. +# Leverage a cache mount to /tmp/cache so that subsequent builds don't have to re-download packages. +RUN --mount=type=bind,source=composer.json,target=composer.json \ + --mount=type=bind,source=composer.lock,target=composer.lock \ + --mount=type=cache,target=/tmp/cache \ + composer install --no-dev --no-interaction + +FROM php:8.2-apache as final + +# Your PHP application may require additional PHP extensions to be installed +# manually. For detailed instructions for installing extensions can be found, see +# https://github.com/docker-library/docs/tree/master/php#how-to-install-more-php-extensions +# The following code blocks provide examples that you can edit and use. +# +# Add core PHP extensions, see +# https://github.com/docker-library/docs/tree/master/php#php-core-extensions +# This example adds the apt packages for the 'gd' extension's dependencies and then +# installs the 'gd' extension. For additional tips on running apt-get, see +# https://docs.docker.com/go/dockerfile-aptget-best-practices/ +# RUN apt-get update && apt-get install -y \ +# libfreetype-dev \ +# libjpeg62-turbo-dev \ +# libpng-dev \ +# && rm -rf /var/lib/apt/lists/* \ +# && docker-php-ext-configure gd --with-freetype --with-jpeg \ +# && docker-php-ext-install -j$(nproc) gd +# +# Add PECL extensions, see +# https://github.com/docker-library/docs/tree/master/php#pecl-extensions +# This example adds the 'redis' and 'xdebug' extensions. +# RUN pecl install redis-5.3.7 \ +# && pecl install xdebug-3.2.1 \ +# && docker-php-ext-enable redis xdebug +RUN docker-php-ext-install pdo pdo_mysql +# Use the default production configuration for PHP runtime arguments, see +# https://github.com/docker-library/docs/tree/master/php#configuration +RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" + +# Copy the app dependencies from the previous install stage. +COPY --from=deps app/vendor/ /var/www/html/vendor +# Copy the app files from the app directory. +COPY ./src /var/www/html + +# Switch to a non-privileged user (defined in the base image) that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +USER www-data diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..fefdc92 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,53 @@ +services: + server: + build: + context: . + target: development + ports: + - 9000:80 + depends_on: + db: + condition: service_healthy + secrets: + - db-password + environment: + - PASSWORD_FILE_PATH=/run/secrets/db-password + - DB_HOST=db + - DB_NAME=example + - DB_USER=root + develop: + watch: + - action: sync + path: ./src + target: /var/www/html + db: + image: mariadb + restart: always + user: root + secrets: + - db-password + volumes: + - db-data:/var/lib/mysql + environment: + - MARIADB_ROOT_PASSWORD_FILE=/run/secrets/db-password + - MARIADB_DATABASE=example + expose: + - 3306 + healthcheck: + test: ["CMD", "/usr/local/bin/healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized"] + interval: 10s + timeout: 5s + retries: 5 + phpmyadmin: + image: phpmyadmin + ports: + - 8080:80 + depends_on: + - db + environment: + - PMA_HOST=db +volumes: + db-data: +secrets: + db-password: + file: db/password.txt \ No newline at end of file diff --git a/docker-php-kubernetes.yaml b/docker-php-kubernetes.yaml new file mode 100644 index 0000000..9d9bf61 --- /dev/null +++ b/docker-php-kubernetes.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: docker-php-demo + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + hello-php: web + template: + metadata: + labels: + hello-php: web + spec: + containers: + - name: hello-site + image: dparrab/docker-php-sample + imagePullPolicy: Always + +--- +apiVersion: v1 +kind: Service +metadata: + name: php-entrypoint + namespace: default +spec: + type: NodePort + selector: + hello-php: web + ports: + - port: 80 + targetPort: 80 + nodePort: 30001 + diff --git a/src/database.php b/src/database.php index 88a2692..78864e4 100644 --- a/src/database.php +++ b/src/database.php @@ -14,6 +14,8 @@ // Create a new PDO instance $db_handle = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); +//$db_handle->exec("CREATE DATABASE IF NOT EXISTS example1"); + // Create the "messages" table if it doesn't exist $db_handle->exec(" CREATE TABLE IF NOT EXISTS messages ( diff --git a/src/hello.php b/src/hello.php index 260c4da..31ed531 100644 --- a/src/hello.php +++ b/src/hello.php @@ -1,3 +1,4 @@ \ No newline at end of file diff --git a/tests/HelloWorldTest.php b/tests/HelloWorldTest.php index 4ecd265..c8dce07 100644 --- a/tests/HelloWorldTest.php +++ b/tests/HelloWorldTest.php @@ -12,7 +12,8 @@ public function testOutput() $output = ob_get_clean(); // Assert that the output is "Hello, Docker!" - $this->assertEquals("Hello, Docker!", $output); + //$this->assertEquals("Hello, Docker!", $output); + $this->assertEquals( "Mi prueba", $output); } } ?>