Nginx reverse proxy to multiple docker apps

Updated: 11 August 2024

I often create and edit Customer demo apps, with Docker. I’ve found it very useful to serve all of these apps behind one Nginx reverse proxy webserver. The notes below show how I achieve this with the Digital Ocean Nginx config tool and deployment of the config with git.

Getting started

  1. Create a Digital Ocean LEMP Droplet.
  2. Install Docker on the LEMP Droplet.
  3. Install and run your Docker applications, binding each to a different host port.

Domains

Each demo needs a URL which can be given to the Customer. I tend to use subdomains of my business domain e.g. http://a-demo.christaylordeveloper.co.uk. For each subdomain I configure an A-record, pointing at the IP address of the LEMP server.

NGINXConfig

  1. Create nginx config for your site(s) with this Digital Ocean tool https://www.digitalocean.com/community/tools/nginx
  2. On the Reverse proxy tab, insert into the proxy_pass field the port numbers you set earlier when running-up your docker apps.
  3. Download your Nginx config.

Git

  1. Make a local git repo of the downloaded config and push to a remote.
  2. Add to your repo a .gitignore file which will ignore everything else in the LEMP droplet /etc/nginx directory.
  3. Clone the git repo into the LEMP server /etc/nginx/ directory
  4. Restart the Nginx service.

Edits

At some point you will need to add, edit or remove the Docker applications:

  1. Load the URL in file nginxconf.txt. This restores your configuration into the Digital Ocean tool.
  2. Make the required edits and download again.
  3. Commit the changes into your local git repo and push to remote.
  4. Pull changes into your LEMP server.

How to configure PHP, Xdebug, Docker and PHPStorm

Updated: 15 November 2023

1. Select these two items in PHPStorm:
Run > Start Listening for PHP Debug Connections
Run > Break at first line in PHP scripts

2. Load a page in a web browser.

3. PHPStorm should display an Incoming Connection From Xdebug dialog box.

4. From the bottom of the dialog, select the local file which matches the ‘File path on server’ value and click Accept.

cron

Updated: 22 August 2024

Maintain crontab files for individual users.

Edit the current crontab. After exiting the editor, the modified crontab will be installed automatically.

crontab -e

Display the current crontab on standard output

crontab -l

Display the crontab of a particular user

sudo crontab -u ec2-user -l

Start a Docker project when host system is rebooted

@reboot sleep 60 && \
touch /home/chris/docker-project/reboot.log && \
echo "$(date): System rebooted, attempting to start." >> /home/chris/docker-project/reboot.log && \
cd /home/chris/docker-project && \
docker compose up -d

Save output to file

*   *   *   *   *   /home/chris/purge.sh > /bin/purge.log 2>&1

PHP in Docker

Updated: 17 May 2024

Pre-configured Docker images (including Xdebug) from Jetbrains https://github.com/JetBrains/phpstorm-docker-images


Run a php script

docker run -it --rm --name my-running-script \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
php:7.4-cli php script.php

Run the php development server

docker run -it --rm --name php-server \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
-p 8000:8000 \
php:8.2-cli php -S 0.0.0.0:8000

Docker

Updated: 28 August 2024

Build and tag an image from a Dockerfile in the same directory

docker build -t node-demo .

Start a container

docker run -i -t ubuntu /bin/bash
docker run -it node:19 /bin/bash
docker run -it --rm -d -p 8080:80 --name web nginx

Docker compose

docker-compose up --build
docker-compose up --remove-orphans -d

Stop all containers

docker stop $(docker ps -aq)

List images

docker images

See which containers are running

docker ps

See all containers, including non-running

docker ps -a

Run a php script inside a container

docker run --rm -v $(pwd):/app -w /app php:cli php hello.php

Get ip address of container

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name>
sudo docker inspect <container name> | grep IPAddress

Command prompt into a container

sudo docker exec -it  bash
sudo docker exec -it wp-local-docker_phpfpm_1 /bin/sh -c "[ -e /bin/bash ] && /bin/bash || /bin/sh"
docker-compose exec --user www-data phpfpm bash

Keep a container running. From docker-compose.yml

services:
  myservice:
    command: bash -c "while true; do echo 'sleeping...' && sleep 10; done"

Remove all stopped containers

docker container prune

Remove all containers

docker rm $(docker ps -aq)

Delete all images

sudo docker rmi $(sudo docker images -q) --force

Remove stopped containers and images, including unused and dangling images

docker system prune -a

phpMyAdmin in Docker

Updated: 16 November 2023

phpMyAdmin is available as a Docker image. The MySQL server connection parameters are provided when the container is instantiated.

For a remote MySQL server

docker run --name myadmin \
-e PMA_HOST=https://example.com \
-e PMA_PORT=3306 \
-e PMA_USER=user \
-e PMA_PASSWORD=password \
-p 8080:80 \
phpmyadmin/phpmyadmin

Browse to phpMyAdmin at http://localhost:8080

MySQL server running in a local Docker network some-network

docker run --name myadmin \
--network=some-network \
-e PMA_HOST=db \
-e PMA_PORT=3306 \
-p 8080:80 \
phpmyadmin/phpmyadmin

--network The docker network the phpMyAdmin container will join.

PHPDoc

Updated: 06 May 2024

PHPDoc reference

Generate php documentation with phpdoc tool and Docker

docker pull phpdoc/phpdoc
cd path/to/my-php-project
docker run --rm -v $(pwd):/data phpdoc/phpdoc -d src -t doc

Display cli manual

docker run --rm phpdoc/phpdoc --help