How to Dockerize a PHP application in different environments
Materials for the article https://lohvynenko.com/en/blog/php-inside-docker-environment.html
-
Install Docker
-
Install Docker Compose
-
Install Minikube
-
Install kubectl
Launch local version with Docker Compose
docker compose up -dOpen URL http://localhost:8080/ You will see information about PHP provided by phpinfo() function
Stop containers and remove all related resources
docker compose downBuild command with SOURCE_FOLDER build argument
docker build --force-rm=true \
--tag="php-docker-env-demo-code" \
--build-arg="SOURCE_FOLDER=/source" \
--file="Dockerfile" --no-cache .Authorize to Docker Registry (Docker Hub)
docker login -u <your-dockerhub-username> --password-stdinPush Docker image to Docker Hub Registry
docker tag php-docker-env-demo-code <your-dockerhub-username>/php-docker-env-demo-code:latest
docker push <your-username>/php-docker-env-demo-code:latestPublic Docker image with the source code is available here https://hub.docker.com/r/luxurydab/php-docker-env-demo-code
Create a new Swarm cluster
docker swarm init --advertise-addr 127.0.0.1Note: 127.0.0.1 is the manager IP address. You can use 127.0.0.1 to play on your local machine. Use the IP address of your server in Live / Production environment
Deploy Docker Swarm cluster
docker stack deploy --compose-file docker-stack.yml docker-swarm-demoOpen URL http://127.0.0.1/ You will see PHP page running in Docker Swarm
Remove services from Docker Swarm
docker service rm docker-swarm-demo_code docker-swarm-demo_nginx docker-swarm-demo_phpLeave the Docker Swarm
docker swarm leave --forceStart minikube
minikube config set vm-driver docker
minikube start --cpus=2 --memory=2048 --alsologtostderrSwitch to minikube environment
eval $(minikube docker-env)Deploy PHP to minikube environment
kubectl apply -f minikube.yamlGet URL to nginx service
minikube service nginx --urlStop and delete minikube
eval $(minikube docker-env -u)
minikube stop
minikube delete