Giter VIP home page Giter VIP logo

docker-php-tutorial's Introduction

Docker PHP Tutorial


Docker PHP Tutorial - Learning Docker for PHP Developers

In this tutorial I'll take you on "my" journey of learning Docker as a PHP developer. Since I actively use everything explained along the way (and force it upon my team ;)) I can hopefully shed some light onto some more advanced issues like cross-OS-setups and seamless IDE integration.

Two words of caution:

  • I tend to write rather lengthy, in-depth articles, so you should allocate some time to work it all through
  • my main OS is Windows. Although most of the things should be easily transferable into OSX/Linux you might run into some quirks that I didn't encounter.

You can also find an overview of the published articles at the Docker PHP Tutorial site of my homepage.

Setup

Each tutorial is placed in a separate branch and has an accompanying article in my blog I recommend cloning the full repository and check out the corresponding branch for easy reference. Example:

mkdir -p /c/codebase
cd /c/codebase/
git clone https://github.com/paslandau/docker-php-tutorial.git
cd docker-php-tutorial

git checkout part_2_setting-up-phpstorm-with-xdebug-for-local-development-on-docker

Tutorials

Please subscribe to my RSS Feed and/or subscribe via email to be automatically notified when a new part gets published. I'll also probably brag about it on Twitter ;)

Playlist

There a YouTube playlist available containing all videos of the tutorial series.

Published

Planned (already used by us but not put into writing):

Roadmap (wow much want, such little time):

Setting up PHP, PHP-FPM and NGINX for local development on Docker

A primer on Docker. What is Docker? How to install it / transition from Vagrant? How to interact with containers? How to organize multiple services (php-cli, php-fpm, nginx) via docker-compose?

Setting up PhpStorm with Xdebug for local development on Docker

Using docker for Development. How to configure PhpStorm to play nicely with Docker? How to setup Xdebug (including the solution for the dreaded 'Connection with Xdebug was not established.' error)?

Structuring the Docker setup for PHP Projects

Running a battle-tested development infrastructure for PHP developers in Docker. How to organize the docker folder structure (e.g. shared scripts for containers)? How to establish a convenient workflow via make?

Docker from scratch for PHP 8.1 Applications in 2022

An update of the previous article (Part 3) with the learnings of the past 3 years:

  • simplify the setup
  • add more containers (redis, mysql, workers)
  • prepare for additional environments (CI, production)

PhpStorm, Docker and Xdebug 3 on PHP 8.1 in 2022

An update of the previous article on setting up PhpStorm (Part 2) using the latest PHP version (PHP 8.1) as well as the latest Xdebug version (3). The article also covers additional debugging challenges (from the browser, from the CLI, from a long-running worker process).

Run Laravel 9 on Docker in 2022

A step-by-step walk through to set up a new Laravel 9 project on the docker setup of this tutorial, using a couple of common Laravel components (Commands, Controllers, Queues, Databases).

Set up PHP QA tools and control them via make

Set up some PHP QA tools like phpcs, phpstan, etc. in the dockerized PHP application and provide make targets to run them either in parallel or individually.

Use git secret to encrypt secrets in the repository

Set up git secret to encrypt and store secrets directly in a git repository. All required tools are set up in Docker and their usage is defined via make targets.

Create a CI pipeline for dockerized PHP Apps

Create a CI pipeline using the dockerized setup introduced in the previous tutorial that can be executed on any CI provider. Concrete examples for Gitlab Pipelines and Github actions are included.

A primer on GCP Compute Instance VMs for dockerized Apps

Getting started with the Google Cloud Platform (GCP) as infrastructure provider for a container registry to push and pull docker images as well as for virtual machines to run dockerized applications.

Deploy dockerized PHP Apps to production on GCP via docker compose

Introduce a prod environment to build docker images that are then deployed "to production" on a GCP VM to be executed via docker compose as a proof-of-concept.

Create a production infrastructure for dockerized PHP Apps on GCP

Create a production infrastructure for a dockerized PHP application on GCP using multiple VMs and managed services for redis and mysql.

Deploy dockerized PHP Apps to production

  • Status: draft
  • Link:
  • Branch:

Scaling Dockerized PHP Applications with Terraform and Kubernetes on GCP/AWS

  • Status: Dream Caused by the Flight of a Bee Around a Pomegranate a Second Before Awakening
  • Link:
  • Branch:

docker-php-tutorial's People

Contributors

a-l-e-c avatar paslandau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-php-tutorial's Issues

failed to download laravel/laravel from dist

Hi there, I am following your Run Laravel 9 on Docker in 20224.3 tutorial and am on the install laravel step however,

when I run the command:

make execute-in-container DOCKER_SERVICE_NAME="application" COMMAND='composer create-project --prefer-dist laravel/laravel /tmp/laravel "9.*" --no-install --no-scripts'

I get the following error:

ENV=local TAG=latest DOCKER_REGISTRY=docker.io DOCKER_NAMESPACE=dofroscra APP_USER_NAME=application APP_GROUP_NAME=application docker-compose -p dofroscra_local --env-file ./.docker/.env -f ./.docker/docker-compose/docker-compose.yml -f ./.docker/docker-compose/docker-compose.local.yml exec -T --user application application composer create-project --prefer-dist laravel/laravel /tmp/laravel "9.*" --no-install --no-scripts
Creating a "laravel/laravel" project at "/tmp/laravel"
Info from https://repo.packagist.org: #StandWithUkraine
Installing laravel/laravel (v9.1.10)
    Failed to download laravel/laravel from dist: /var/www/app/vendor/composer does not exist and could not be created.
    Now trying to download from source

                                                            
  [RuntimeException]                                        
  git was not found in your PATH, skipping source download  


create-project [-s|--stability STABILITY] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--repository REPOSITORY] [--repository-url REPOSITORY-URL] [--add-repository] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [--no-secure-http] [--keep-vcs] [--remove-vcs] [--no-install] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--ask] [--] [<package>] [<directory>] [<version>]

make: *** [.make/01-01-application-commands.mk:24: execute-in-container] Error 

I have already run make docker-build, and make docker-up. What am I doing wrong?
(Running PopOS 22.04)

docker-test.sh use bash syntax for function and not POSIX one

My default shell is dash and not bash and make docker-test failed with error:

sh ./.docker/docker-test.sh
./.docker/docker-test.sh: 3: Syntax error: "(" unexpected
make: *** [Makefile:29: docker-test] Error 2

To fix it I add a SH variable in the Makefile:
SH=/bin/bash
and change the according line:

-	sh $(DOCKER_COMPOSE_DIR)/docker-test.sh
+	$(SH) $(DOCKER_COMPOSE_DIR)/docker-test.sh

There's many way to make that change usefull and configurable to avoid changing the Makefile directly. You can add a conf.mk file with default values and include it in the main Makefile.

For example I have an another issue that can be solved with that conf.mk file. I change the NGINX_HOST_HTTP_PORT (because the 80 is already used !) but of course the last test in docker-test.sh failed.

Anyway thanks for the stuff. I really like it.

Part 4.1. Env variables problem with make docker-down

First of all, thank you for your great work, it is very very helpful.

But I have a problem in Part 4.1 Docker from scratch for PHP 8.1 Applications in 2022. I downloaded it (e47a3bd) and modified a bit, maybe I have screwed something up myself but I'll ask anyway.

I have built containers with docker-build, all good, no errors/warnings except (which is okay, although little misleading):

WARN[0000] The APPLICATION_SSH_HOST_PORT variable is not set. Defaulting to a blank string. 

...which okay for now, though message is wrong, it defaults to 2222, haven't changed this place (and this might be Docker issue):

    ports:
      - "${APPLICATION_SSH_HOST_PORT:-2222}:22"

make docker-up - fine

ENV=local TAG=latest DOCKER_REGISTRY=docker.io DOCKER_NAMESPACE=foobar APP_USER_ID=10000 APP_GROUP_ID=10001 APP_USER_NAME=application docker compose -p foobar_local --env-file ./.docker/.env -f ./.docker/docker-compose/docker-compose.yml -f ./.docker/docker-compose/docker-compose.local.yml up -d 
WARN[0000] The APPLICATION_SSH_HOST_PORT variable is not set. Defaulting to a blank string. 
[+] Running 3/3
 ⠿ Container foobar_local_application_1  Started                                                                                                                                                                           ⠿ Container foobar_local_node_1         Started                                                                                                                                                                           ⠿ Container foobar_local_mariadb_1      Started

make docker-down

ENV=local TAG=latest DOCKER_REGISTRY=docker.io DOCKER_NAMESPACE=foobar APP_USER_ID=10000 APP_GROUP_ID=10001 APP_USER_NAME=application docker compose -p foobar_local --env-file ./.docker/.env -f ./.docker/docker-compose/docker-compose.yml -f ./.docker/docker-compose/docker-compose.local.yml down
WARN[0000] The NETWORKS_DRIVER variable is not set. Defaulting to a blank string. 
WARN[0000] The VOLUMES_DRIVER variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_SSH_PASSWORD variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_CODE_PATH_HOST variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_CODE_PATH_CONTAINER variable is not set. Defaulting to a blank string. 
WARN[0000] The APPLICATION_SSH_HOST_PORT variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_VERSION variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_DATABASE variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_USER variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_PASSWORD variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_ROOT_PASSWORD variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_DATABASE_PATH_HOST variable is not set. Defaulting to a blank string. 
WARN[0000] The MARIADB_HOST_PORT variable is not set. Defaulting to a blank string. 
WARN[0000] The NODE_VERSION variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_CODE_PATH_HOST variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_CODE_PATH_CONTAINER variable is not set. Defaulting to a blank string. 
WARN[0000] The APP_CODE_PATH_CONTAINER variable is not set. Defaulting to a blank string. 
Invalid template: "required variable APP_CODE_PATH_CONTAINER is missing a value: "
make: *** [docker-down] Error 1

.docker/.env:

# docker-compose env vars
# @see https://docs.docker.com/compose/reference/envvars/
COMPOSE_PROJECT_NAME=foobar
COMPOSE_CONVERT_WINDOWS_PATHS=1

# application
APP_GROUP_NAME=application

# application
APP_SSH_PASSWORD=123456

TIMEZONE=Europe/Tallinn

# mysql
#MYSQL_PASSWORD=secret
#MYSQL_ROOT_PASSWORD=secret_mysql_root_password

# mariadb
MARIADB_DATABASE=xxx
MARIADB_USER=xxx
MARIADB_PASSWORD=xxx
MARIADB_ROOT_PASSWORD=xxx
MARIADB_HOST_PORT=3306
# Database files path. Relative to the location of the first docker-compose.yml file that is used for docker-compose
# Default is Docker-controlled path
MARIADB_DATABASE_PATH_HOST=../../../data/mysql

# The following variables are not supposed to be changed by developers.
# They are only placed here, because there is no other way to specify
# "configuration" variables other than using the .env file.
# Unfortunately, it's also not possible to use multiple .env files for
# docker-compose (only one file is supported for the --env-file option,
# see also https://github.com/docker/compose/issues/6170#issuecomment-443523663 )

# The following commented variables should be passed in via environment variables from the Makefile
#APP_USER_NAME=application
#APP_USER_ID=10000
#APP_GROUP_ID=10001
APP_CODE_PATH_CONTAINER=/var/www/foobar
# APP_CODE_PATH_HOST is relative to the location of the first docker-compose.yml file that is used for docker-compose
APP_CODE_PATH_HOST=../../
APP_HOST=localhost
NETWORKS_DRIVER=bridge
VOLUMES_DRIVER=local

MARIADB_VERSION=10.3.35
PHP_VERSION=8.1
NODE_VERSION=14

Excerpt from 02-00-docker.mk (as you can see they all use the same $(DOCKER_COMPOSE):

.PHONY: docker-build-image
docker-build-image: validate-docker-variables ## Build all docker images OR a specific image by providing the service name via: make docker-build DOCKER_SERVICE_NAME=<service>
	$(DOCKER_COMPOSE) build $(DOCKER_SERVICE_NAME)

.PHONY: docker-build-php
docker-build-php: validate-docker-variables ## Build the php base image
	$(DOCKER_COMPOSE_PHP_BASE) build $(DOCKER_SERVICE_NAME_PHP_BASE)

.PHONY: docker-build
docker-build: docker-build-php docker-build-image ## Build the php image and then all other docker images

.PHONY: docker-up
docker-up: validate-docker-variables ## Create and start all docker containers. To create/start only a specific container, use DOCKER_SERVICE_NAME=<service>
	$(DOCKER_COMPOSE) up -d $(DOCKER_SERVICE_NAME)

.PHONY: docker-down
docker-down: validate-docker-variables ## Stop and remove all docker containers.
	$(DOCKER_COMPOSE) down
  1. Where do these warnings come from, i.e why build and up are okay but down not?
  2. Docker compose manual does not talk about different behavior of env variables vs .env file vs param --env-file but different users such as this are talking that .env and --env-file are not completely same. Still, why build and up work but down not.

I am confused.

My system:

  • MacOS Monterey v12.1
  • Docker version 20.10.7, build f0df350
  • docker-compose version 1.29.2, build 5becea4c

Part_3 doesn't work on Ubuntu 18.04

I am a newbee, following the ReadMe and changing the compose version to '3.3', always encounters errors with :
docker-compose -f ./.docker/docker-compose.yml --project-directory ./.docker rm -fs && \ docker-compose -f ./.docker/docker-compose.yml --project-directory ./.docker build --pull --no-cache --parallel && \ docker-compose -f ./.docker/docker-compose.yml --project-directory ./.docker up -d --force-recreate WARNING: The NGINX_HOST_HTTP_PORT variable is not set. Defaulting to a blank string. WARNING: The NGINX_HOST_HTTPS_PORT variable is not set. Defaulting to a blank string. WARNING: The APP_HOST variable is not set. Defaulting to a blank string. WARNING: The APP_CODE_PATH_HOST variable is not set. Defaulting to a blank string. WARNING: The APP_CODE_PATH_CONTAINER variable is not set. Defaulting to a blank string. WARNING: The APP_GROUP variable is not set. Defaulting to a blank string. WARNING: The APP_GROUP_ID variable is not set. Defaulting to a blank string. WARNING: The APP_USER variable is not set. Defaulting to a blank string. WARNING: The APP_USER_ID variable is not set. Defaulting to a blank string. WARNING: The TIMEZONE variable is not set. Defaulting to a blank string. WARNING: The PHP_VERSION variable is not set. Defaulting to a blank string. WARNING: The WORKSPACE_HOST_SSH_PORT variable is not set. Defaulting to a blank string. WARNING: The NETWORKS_DRIVER variable is not set. Defaulting to a blank string. ERROR: The Compose file '././.docker/docker-compose.yml' is invalid because: services.workspace.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object Makefile:23: recipe for target 'docker-build-from-scratch' failed make: *** [docker-build-from-scratch] Error 1
Hoping supports for Linux.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.