Home > database >  In the construction of docker images and containers; it doesn't have the scope over the expecte
In the construction of docker images and containers; it doesn't have the scope over the expecte

Time:10-20

I am trying to help a friend in a project in which he implements docker, despite how strange its structure is, it is relevant to his objectives, if there is a need to go deeper into this topic, let me know in the comments.

The project we are working on is this:

https://github.com/arcanisgk/Last-Hammer-2

the project structure looks like this:

project structure (I don't have enough reputation to make this image viewable.)

The scenery (Context):

To get docker up, run this command:

docker-compose -f docker/docker-compose.yml up --build --force-recreate

note: this is so since the docker structure is in the docker directory.

When the build is complete we should have this:

container runing

To achieve this, the following files were implemented:

docker/.env

# Please Note:
# In PHP Versions <= 7.4 MySQL8 is not supported due to lacking pdo support

# To determine the name of your containers
COMPOSE_PROJECT_NAME=Last-Hammer-2
[email protected]

# Possible values: php54, php56, php71, php72, php73, php74, php8, php81
PHPVERSION=php81
DOCUMENT_ROOT=./../
APACHE_DOCUMENT_ROOT=/var/www/html/public
VHOSTS_DIR=./config/vhosts
APACHE_LOG_DIR=./logs/apache2
PHP_INI=./config/php/php.ini
SSL_DIR=./config/ssl

# PHPMyAdmin
UPLOAD_LIMIT=512M
MEMORY_LIMIT=512M

# Xdebug
XDEBUG_LOG_DIR=./logs/xdebug
XDEBUG_PORT=9003
#XDEBUG_PORT=9000

# Possible values: mysql57, mysql8, mariadb103, mariadb104, mariadb105, mariadb106
#
# For Apple Silicon User: 
# Please select Mariadb as Database. Oracle doesn't build their SQL Containers for the arm Architecure

DATABASE=mysql8
MYSQL_INITDB_DIR=./config/initdb
MYSQL_DATA_DIR=./data/mysql
MYSQL_LOG_DIR=./logs/mysql

# If you already have the port 80 in use, you can change it (for example if you have Apache)
HOST_MACHINE_UNSECURE_HOST_PORT=8008

# If you already have the port 443 in use, you can change it (for example if you have Apache)
HOST_MACHINE_SECURE_HOST_PORT=443

# If you already have the port 3306 in use, you can change it (for example if you have MySQL)
HOST_MACHINE_MYSQL_PORT=3307

# If you already have the port 8080 in use, you can change it (for example if you have PMA)
HOST_MACHINE_PMA_PORT=8009
HOST_MACHINE_PMA_SECURE_PORT=8443

# If you already has the port 6379 in use, you can change it (for example if you have Redis)
HOST_MACHINE_REDIS_PORT=6379

# MySQL root user password
MYSQL_ROOT_PASSWORD=1qazxsw22

# Database settings: Username, password and database name
#
# If you need to give the docker user access to more databases than the "docker" db 
# you can grant the privileges with phpmyadmin to the user.
MYSQL_USER=lh2
MYSQL_PASSWORD=1qazxsw22
MYSQL_DATABASE=lh2

docker-compose.yml

version: "3.9"

services:
  webserver:
    build:
      context: ./bin/${PHPVERSION}
    container_name: "${COMPOSE_PROJECT_NAME}-${PHPVERSION}"
    restart: "always"
    ports:
      - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
      - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
    links:
      - database
    volumes:
      - ${DOCUMENT_ROOT-./../}:/var/www/html:rw
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${SSL_DIR-./config/ssl}:/etc/apache2/ssl/
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
      - ${XDEBUG_LOG_DIR-./logs/xdebug}:/var/log/xdebug
    environment:
      APACHE_DOCUMENT_ROOT: ${APACHE_DOCUMENT_ROOT-/var/www/html}
      PMA_PORT: ${HOST_MACHINE_PMA_PORT}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      HOST_MACHINE_MYSQL_PORT: ${HOST_MACHINE_MYSQL_PORT}
      XDEBUG_CONFIG: "client_host=host.docker.internal remote_port=${XDEBUG_PORT}"
    extra_hosts:
      - "host.docker.internal:host-gateway"
  database:
    build:
      context: "./bin/${DATABASE}"
    container_name: "${COMPOSE_PROJECT_NAME}-${DATABASE}"
    restart: "always"
    ports:
      - "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
    volumes:
      - ${MYSQL_INITDB_DIR-./config/initdb}:/docker-entrypoint-initdb.d
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  phpmyadmin:
    image: phpmyadmin
    restart: always
    container_name: "${COMPOSE_PROJECT_NAME}-phpmyadmin"
    links:
      - database
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      UPLOAD_LIMIT: ${UPLOAD_LIMIT}
      MEMORY_LIMIT: ${MEMORY_LIMIT}
    ports:
      - "${HOST_MACHINE_PMA_PORT}:80"
      - "${HOST_MACHINE_PMA_SECURE_PORT}:443"
    volumes:
      - /sessions
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
  redis:
    restart: always
    container_name: "${COMPOSE_PROJECT_NAME}-redis"
    image: redis:latest
    ports:
      - "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"

/config/vhosts/default.conf

<VirtualHost *:80>
    ServerAdmin ${WEB_MASTER}
    DocumentRoot ${APACHE_DOCUMENT_ROOT}
    ServerName localhost
    <Directory ${APACHE_DOCUMENT_ROOT}>
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
        Allow from all
    </Directory>
</VirtualHost>

The Problem:

We hope to be able to use the public directory as the project's document_root to place our index.php, which we easily accomplished; but when we try to access the compose vendor to access the autoload file declared in compose.json we find that it doesn't exist , i run this to check which directory exists:

$files1 = scandir(dirname(__DIR__).'/../');

var_dump($files1);

get this output:

output scandir

Update:

From what we've investigated, the problem seems to lie with these configuration lines, or they are related to this:

in .env

DOCUMENT_ROOT=./../
APACHE_DOCUMENT_ROOT=/var/www/html/public

in default.conf

<Directory ${APACHE_DOCUMENT_ROOT}>

in docker-compose.yml

volumes:
      - ${DOCUMENT_ROOT-./../}:/var/www/html:rw

I'm pretty sure this is a logic error... since we don't have access to the vendor directory for some reason, it didn't get uploaded to the container in the build; What changes are required to correct this error?

It was also observed that there is no access to the other directories such as: FrameWork and log, in short, there is no access to anything that is in the root of the project.

CodePudding user response:

you must make this changes:

in .yml add to volumen this:

volumes:
  - ./../:/var/www/html:rw

your apache conf must point to:

DocumentRoot ${APACHE_DOCUMENT_ROOT}/public

<Directory ${APACHE_DOCUMENT_ROOT}>

your .env file must be like this:

DOCUMENT_ROOT=./../public
APACHE_DOCUMENT_ROOT=/var/www/html

I have tried it and it has worked for me... although I do not know the exact theory/definition, can someone expand on this answer.

there may even be other more efficient ways to achieve it in the future.

  • Related