From 49e422823771e21f7c596a929cb823953b01a4ea Mon Sep 17 00:00:00 2001 From: tecnodidacticahoy Date: Thu, 29 Feb 2024 22:47:10 -0400 Subject: [PATCH 1/4] Primer Probando docker desde cero. --- .dockerignore | 35 +++++++++++++++++++++++++++++ Dockerfile | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ compose.yaml | 52 +++++++++++++++++++++++++++++++++++++++++++ src/database.php | 2 ++ src/hello.php | 2 +- 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml 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/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3ea13c4 --- /dev/null +++ b/Dockerfile @@ -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..6cf9570 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,52 @@ +services: + server: + build: + context: . + 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/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..fa1fad9 100644 --- a/src/hello.php +++ b/src/hello.php @@ -1,3 +1,3 @@ \ No newline at end of file From e023a27ce0f5e4fefd0bea4e674ac0141b0fdb16 Mon Sep 17 00:00:00 2001 From: tecnodidacticahoy Date: Fri, 1 Mar 2024 21:19:27 -0400 Subject: [PATCH 2/4] Tutorial Mejora en el tutorial --- Dockerfile | 69 +++++++++++++--------------------------- Dockerfile copy | 58 +++++++++++++++++++++++++++++++++ compose.yaml | 1 + src/hello.php | 3 +- tests/HelloWorldTest.php | 3 +- 5 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 Dockerfile copy diff --git a/Dockerfile b/Dockerfile index 3ea13c4..8d61502 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,58 +1,33 @@ # syntax=docker/dockerfile:1 # Create a stage for installing app dependencies defined in Composer. -FROM composer:lts as deps +FROM composer:lts as prod-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 \ +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 +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 -# 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 +FROM php:8.2-apache as base 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/ +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 index 6cf9570..fefdc92 100644 --- a/compose.yaml +++ b/compose.yaml @@ -2,6 +2,7 @@ services: server: build: context: . + target: development ports: - 9000:80 depends_on: diff --git a/src/hello.php b/src/hello.php index fa1fad9..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); } } ?> From 3deba0ec7df81156163d88da592792eead1a3c6d Mon Sep 17 00:00:00 2001 From: tecnodidacticahoy <117233539+tecnodidacticahoy@users.noreply.github.com> Date: Sat, 2 Mar 2024 16:57:52 -0400 Subject: [PATCH 3/4] Create main.yml Creando el main yml --- .github/workflows/main.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/main.yml 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 + From 183b6dce7596e59c283a5a8df0e01fd651ab8190 Mon Sep 17 00:00:00 2001 From: tecnodidacticahoy Date: Sat, 2 Mar 2024 19:43:40 -0400 Subject: [PATCH 4/4] Creando kubernetes Creando kubernetes --- docker-php-kubernetes.yaml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 docker-php-kubernetes.yaml 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 +