PHP docker and using MySQL PDO - php

When creating a LEMP stack using PHP-FPM I can't seem to load the PDO dirver it keeps defaulting to the sqlite driver.
Docker compose:
version: "3.5"
services:
redis:
image: redis:alpine
container_name: errors-redis
mysql:
image: mysql:8.0
container_name: errors-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=test
- MYSQL_DATABASE=errors
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- "9011:3306"
webserver:
image: nginx:latest
container_name: errors-nginx
working_dir: /application
volumes:
- .:/application
- ./dev/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "9010:80"
php-fpm:
image: php:fpm
container_name: errors-php-fpm
working_dir: /application
volumes:
- .:/application
- ./dev/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
Docker file:
FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"
# Install selected extensions and other stuff
RUN apt-get update \
&& apt-get -y --no-install-recommends install php-memcached php7.2-mysql php-redis php-xdebug \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
PHP ini
upload_max_filesize = 100M
post_max_size = 108M
Now when I check phpinfo()
PDO support enabled
PDO drivers sqlite
I've not installed sqlite at any point so I'm not sure how it's gotten it. How do I change it to use MySQL instead of sqlite?

You need to add the PDO bits to your install as well...
apt-get -y --no-install-recommends install php-memcached php-redis php-xdebug mysqli pdo pdo_mysql \
Specifically the last 3. You can probably miss out the php7.2-mysql as this should be covered with the mysqli option.

Related

Cannot use xdebug on CLI , using docker

I can use xdebug on a the web app, but when I try to execute a Symfony command, the execution doesn't stop at breakpoints.
This is my dockerfile:
FROM phpdockerio/php74-fpm:latest
WORKDIR "/application"
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
# Install selected extensions and other stuff
RUN apt-get update \
&& apt-get -y --no-install-recommends install php7.4-gd php7.4-memcached php7.4-mysql php7.4-redis php7.4-xdebug \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
RUN apt-get update \
&& apt-get -y --no-install-recommends install libfontconfig1 libxrender1 libxext6 \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
this is my docker-compose
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: my-memcached
mailhog:
image: mailhog/mailhog:latest
container_name: my-mailhog
ports:
- "1026:8025"
redis:
image: redis:alpine
container_name: my-redis
mariadb:
image: mariadb:10.4
container_name: my-mariadb
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=mydb
- MYSQL_USER=mypass
- MYSQL_PASSWORD=secret
ports:
- "1028:3306"
webserver:
image: nginx:alpine
container_name: my-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
php-fpm:
build: phpdocker/php-fpm
container_name: my-php-fpm
working_dir: /application
environment:
PHP_IDE_CONFIG: "serverName=myServer"
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini
extra_hosts:
- "host.docker.internal:host-gateway"
this is my php-ini-overrides
upload_max_filesize = 100M
post_max_size = 108M
[Xdebug]
;zend_extension="/usr/lib/php/20210902/xdebug.so"
xdebug.mode=debug
xdebug.idekey=PHPSTORM
xdebug.client_port=9003
xdebug.start_with_request=yes
xdebug.discover_client_host=0
xdebug.client_host=host.docker.internal
NO problem with web applications , but is not possible to debug cli apps, I added tried to add some variables to all files but never worked, Do I missed something?
Uncomment the zend_extension and fill the proper path to .so file.
The global $_SERVER variable is not populated by CLI SAPI and then client discovering does not work.

PHP container connect to external DB

I would like to connect my project php with an external MYSQL database.
Currently, i use this docker-compose wtih Dockerfile :
Dockefile
FROM php:5.6-apache-jessie
RUN apt-get update
RUN apt-get install -y apt-file
RUN apt-file update
RUN apt-get install -y vim bash net-tools
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql
EDIT docker-compose :
version: '3.8'
services:
db:
image: mariadb:10.1
ports:
- "3306:3306"
restart: always
environment:
- MYSQL_HOST=IP_EXTERNAL_DB
- MYSQL_ROOT_PASSWORD=AMAZING_PASSWORD
- MYSQL_DATABASE=NAME_OF_DATABASE_TRY_TO_CONNECT
- MYSQL_USER=USER
- MYSQL_PASSWORD=PASSWORD
php:
build:
context: .
dockerfile: Dockerfile
ports:
- "80:80"
volumes:
- .:/var/www/html
environment:
- PHP_FPM_CLEAR_ENV=no
links:
- db
PHP is ok, but the connection to external DB doesn't work.
I know i'm missing something, but i can't find it.
Any ideas ? :)

How to set PHP $_SERVER variable with Docker?

I've created a PHP/Apache/MySQL development environment with Docker and would like to set variable that I can use with $_SERVER in PHP.
Usually I will configure something like that in my virtual host
SetEnv ENV "developement"
Is there a way to do it with my docker_compose.yml file ?
I'll try by using environment: - ENV=developement in my docker-compose file but it doesn't work.
Here is my Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
and my docker-compose.yml
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
- ENV=developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
Consider the below Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
RUN echo 'PassEnv FIRST_NAME' > /etc/apache2/conf-enabled/expose-env.conf
RUN echo '<?php echo $_SERVER["FIRST_NAME"];' > /var/www/html/first.php && echo '<?php echo $_SERVER["LAST_NAME"];' > /var/www/html/last.php
Now run the container using
docker run -it -e FIRST_NAME=TARUN -e LAST_NAME=LALWANI -p 80:80 4ba2aa50347b
Testing
$ curl localhost/first.php
TARUN
$ curl localhost/last.php
$
As you can see the only FIRST_NAME can be accessed, because we exposed the same using PassEnv directive in our apache config insider the container
I know this is a old thread, however, another solution could also be that inside your docker compose, you need to remove the "-" in front of ENV and then instead of "=" it needs to be ":".
It will end up looking like this then:
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
ENV: developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
At least it's working like that in version 3.

Adding mbstring to docker image for laravel 5 app

I am new to docker and I'm trying to set it up in order to run with Laravel 5.1. I am currently getting the following error
Call to undefined function Illuminate\Foundation\Bootstrap\mb_internal_encoding() in /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php:43
I believe this is because the mbstring php extention is not installed. I have tried to add php-mbstring to the Docker file but it doesn't seem to be working.
Here is my full Docker file
FROM php:5.6.30-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-install mcrypt pdo_mysql \
&& docker-php-ext-install php-mbstring
I am then running sudo docker compose up from the application folder. This does not seem to be resolving the error though. How do I know if the extensions are being installed properly?
EDIT: I have included the docker-compose.yml file below
version: '2'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
# The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
volumes:
dbdata:
Remove the php- prefix and it should work fine. You can also run it on the previous docker-php-ext-install command:
docker-php-ext-install mcrypt pdo_mysql mbstring
on ubuntu php-mbstring has dependency with php-common and version specific mbstring like php7.1-mbstring that may cause the problem. You can check dependencies with this command below;
apt-cache depends php-mbstring

how load mysql extensions dockerfile with Docker, Compose with Pfm and Nginx and Mysql

I’m new to Docker & Compose. I’ve created a Docker.yml file with nginx, fpm & mysql. docker-compose.yml runs without errors...
I can execute php commands (ie see the results of php commands in browser) but I can’t execute mysqli commands. phpinfo.php shows '--enable-mysqlnd’ and it appears to be installed. Configuration File (php.ini) Path /usr/local/etc/php is doesn’t have any php.ini… i’m not sure what to do to get this mysql commands to work.
I think I may need to create a Dockerfile for PHP and add mysql extensions after extracting them... but not sure if that's needed....or how to do that correctly - thanks for the help!
Compose file which runs and brings up 3 Docker contains that I can connect to:
docker-compose.yml
version: '2'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
- code-network
links:
- php
php:
image: php:fpm
volumes:
- ./code:/code
networks:
- code-network
links:
- db
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=mypassword
- MYSQL_DATABASE=lbb
networks:
- code-network
networks:
code-network:
driver: bridge
Dockerfile I've started to work on creating but at the moment doesn't extract code correctly .... not sure if I even need it!
Dockerfile:
FROM php:7.0-fpm
RUN docker-php-source extract
RUN apt-get update && \
&& apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mysqli pdo pdo_mysql

Categories