Starting app structure:

./DockerfilePHP (file)

FROM php:8.3-cli

LABEL maintainer="DevWL"
# Install necessary extensions and tools
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    && docker-php-ext-install pdo_mysql

COPY . .
COPY entrypoint.sh /entrypoint.sh
WORKDIR /
RUN chmod +x /entrypoint.sh

COPY --from=composer:2.7.7 /usr/bin/composer /usr/bin/composer

# Set the working directory to the app directory
WORKDIR /var/www/html

# Copy application files to the app directory
# COPY ./app/ /var/www/html

# Expose the port the server will run on
EXPOSE 8000
ENV PORT=8000

# Start the PHP built-in web server
# CMD ["php", "-S", "0.0.0.0:8000", "-t", "/var/www/html/"]
ENTRYPOINT ["/entrypoint.sh"]

/entrypoint.sh

#!/bin/bash

echo "hello from script"

## Uncoment below to build from composer.json if present
# composer install --no-interation --no-progress

php -S 0.0.0.0:8000 -t /var/www/html/

exec docker-php-entrypoint "$@"

./docker-compose.yaml (file)

services:
  web:
    container_name: web
    build: 
      context: .
      dockerfile: DockerfileHttpd
    image: myhttpd
    ports:
      - 8080:80
    volumes:
      - ./app:/usr/local/apache2/htdocs
    restart: always
    networks:
      mybridge:
        ipv4_address: 172.15.0.2

  php:
    container_name: php
    build: 
      context: .
      dockerfile: DockerfilePHP
    ports:
      - 8000:8000
    volumes:
      - ./app:/var/www/html
    # restart: always
    networks:
      mybridge:
        ipv4_address: 172.15.0.5

  db:
    container_name: db
    image: mariadb
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MARIADB_USER: ${DB_USERNAME}
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_DATABASE: ${DB_DATABASE}
      ## Access from WSL2/Windos - CLI: mysql -h 127.0.0.1 -P 3307 -p -u root app1
    ports:
      - 3307:3306
    networks:
      mybridge:
        ipv4_address: 172.15.0.3
    volumes: 
      - mariadb_vol:/var/lib/mysql

  adminer:
    container_name: adminer
    image: adminer
    restart: always
    ports:
      - 8081:8080
    networks:
      mybridge:
        ipv4_address: 172.15.0.4

volumes:
  mariadb_vol:

networks:
  mybridge:
    # Specify driver options
    name: mybridge
    driver: bridge
    ipam:
      config:
        - subnet: "172.15.0.0/24"
    driver_opts:
      com.docker.network.bridge.host_binding_ipv4: 127.0.0.1
      ## List all netowrks - CLI: docker network ls
      ## Inspect specyfic netowrk - CLI: docker network inspect mybridge 
      ## List open ports on WSL2 - CLI: sudo ss -tulpn | grep LISTEN
      ## Access from WSL2/Windos - CLI: mysql -h 127.0.0.1 -P 3307 -p -u root app1
  default:
    # Use a custom driver
    driver: mybridge

.env (file)

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=app1
DB_PORT=3307
DB_USERNAME=admin
DB_PASSWORD=admin
DB_ROOT_PASSWORD=root

Useful commands:

docker-compose up -d
docker-compose down
## List all netowrks - CLI: 
docker network ls      
## Inspect specyfic netowrk - CLI: 
docker network inspect mybridge       
## List open ports on WSL2 - CLI: 
sudo ss -tulpn | grep LISTEN      
## Access from WSL2/Windos - CLI: 
mysql -h 127.0.0.1 -P 3307 -p -u root app1

Screens

After connecting to “adminer” app in web browser under 172.0.0.1:8081, use docker internal IP address to connect to database. We use internal IP because adminer app runs in our docker network, and it has access to other containers (including db).

We can also connect from the host machine application like HeidiSQL or PhpMyAdmin using host maping on lolcahost:3307/127.0.0.1:3307.

We could also use CLI from WSL2 or Windows PowerShell running host IP with mapped port… as in example below:

mysql -h 127.0.0.1 -P 3307 -p -u root app1

Free tutorials:

Docker tutorials:

0
Would love your thoughts, please comment.x
()
x