docker-compose pdo-mysql driver not found - php

I am trying to Dockerize my project (PHP MYSQL and PDO). Even though I added the scripts to install the extensions to my Dockerfile and every time I build it is installing them I am still getting: "Could not find driver". I checked in the phpinfo() and the driver is not there. I deleted all images and containers, built from scratch. Same result. Any ideas? In my docker file I have the following:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 80
and my docker-compose.yaml file:
version: '3.3'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php74
depends_on:
- db
links:
- db
volumes:
- ./php:/var/www/html/
ports:
- 8008:80
db:
container_name: mysql8
command: --default-authentication-plugin=mysql_native_password
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: realDE
MYSQL_USER: khaldoun
MYSQL_PASSWORD: password
ports:
- 6033:3306

For the test I performed, I did the following:
Dockerfile -
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
EXPOSE 80
# start Apache2 on image start
CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
index.php
<?php
phpinfo();
?>
run command (I named the image pdo-test):
docker run --name=pdo-test -p 8080:80 -d pdo-test
Once the container was started I navigated to HTTP://localhost:8080/index.php and saw the PDO driver is loaded:
Please note the only difference between my Dockerfile and yours is that I copied a PHP page into /var/www/html and added a command that would start Apache when the container is run.
Things you should check:
is the volume you're mounting correct ./php:/var/www/html
since you have no command to execute Apache, confirm it is starting properly in the container. I tested both ways and it started each time, but you should bash into the container and make sure Apache is running as you expect.
EDIT I copied one of the php.ini files from the container
docker cp pdo-test:usr/local/etc/php/php.ini-production php.ini
and uncommented the PDO drivers:
;extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
Then I rebuilt the container, copying in the updated php.ini file:
FROM php:7.4-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql
COPY $PWD/index.php /var/www/html
COPY $PWD/php.ini /usr/local/etc/php
EXPOSE 80
# start Apache2 on image start
# CMD ["/usr/sbin/apache2ctl","-DFOREGROUND"]
I can now see the php.ini file in phpinfo()

Related

I cannot connect my dockerize symfonfy 4.2 project with mySql database in another container

I am trying to move an existing Symfony 4.2 project to a docker container with the existing database engine but the project doesn't seem to connect with the database.
As I viewed the log on the php-apache container, I saw this error message displayed **Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused" ** And on a browser I got this message: An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
These are my steps to set up the project on docker if needed to diagnose my issue
Downloaded a php apache docker image using php-apache-7.4
Downloaded MySQL 8.0 image, 5.4/5.7 isn't supported on the Mac M1 processor
Note my docker-compose.yml file my environment: MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
I installed the following PHP module into the php apache container: pdo, MySQL, pdo_mysql and zip
Created a self-signed SSL certificate and installed it on the PHP apache container
The following ports were exposed 3304: MySQL, 80: web and 443: SSL port
created a network without a subnet in the docker-compose.yml file
added a name server test domain name in the /etc/hosts file on the local machine - restarted apache
Below is my full docker-compose.yml file
version: "3.8"
networks:
abc-network:
services:
#mysql
mysql-service:
image: mysql
networks:
- abc-network
container_name: abc_db_container
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- ./mysql:/var/lib/mysql
restart: always
#php
php-service:
build:
context: .
dockerfile: ./symfony/Dockerfile
container_name: abc_php_container
networks:
- abc-network
ports:
- "80:80"
- "443:443"
volumes:
- ./symfony/docker/ssl/mycert.crt:/etc/ssl/certs/mycert.crt
- ./symfony/docker/ssl/mycert.key:/etc/ssl/private/mycert.key
- ./symfony:/var/www/symfony
depends_on:
- mysql-service
Below is my DockerFile configuration
# PHP + Apache
FROM php:7.4-apache
#Enable apache module and ssl self sign installation
RUN a2enmod rewrite
RUN a2enmod ssl && a2enmod socache_shmcb
RUN sed -i '/SSLCertificateFile.*snakeoil\.pem/c\SSLCertificateFile \/etc\/ssl\/certs\/mycert.crt' /etc/apache2/sites-available/default-ssl.conf && sed -i '/SSLCertificateKeyFile.*snakeoil\.key/cSSLCertificateKeyFile /etc/ssl/private/mycert.key\' /etc/apache2/sites-available/default-ssl.conf
RUN a2ensite default-ssl
RUN apt-get update && apt-get upgrade -y
#updates apt get and install essential php libraries
RUN apt-get update \
&& apt-get install -y libzip-dev git wget --no-install-recommends \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
#Install mysql driver for php and zip
RUN docker-php-ext-install pdo mysqli pdo_mysql zip;
#Installs composer
RUN wget https://getcomposer.org/download/2.0.9/composer.phar \
&& mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer
RUN chmod 777 /var/lib
#Copies ABC's virtual host conf file to the docker container
COPY ./symfony/docker/apache.conf /etc/apache2/sites-enabled/000-default.conf
COPY ./symfony/docker/ssl-conf.conf /etc/apache2/sites-available/default-ssl.conf
#Copies ABC CMS Symfony project
COPY ./symfony /var/www/symfony/
#Points the working directory
WORKDIR /var/www/symfony
CMD ["apache2-foreground"]

PHP extensions not installed / loaded / enabled, using the official PHP docker image

I've been trying all day long to install some PHP extensions into a custom made docker image (based on the official php:7.1.21-fpm-alpine3.8 image), but without success. I really don't know what I'm doing wrong as I was able to do the same thing in the past.
When I run docker-compose up, it seems that the images (php-fpm, nginx and mysql) are built correctly and that 3 docker containers are running.
However, when I check my phpinfo file, I can see that the extensions aren't loaded. (pdo_mysql, zip, imap and xdebug)
When I go into the php-fpm container and check the /usr/local/etc/php/conf.d directory I can see the ini files for all these extensions:
docker-php-ext-imap.ini
docker-php-ext-pdo_mysql.ini
docker-php-ext-xdebug.ini
docker-php-ext-zip.ini
my-firm.ini
The phpinfo also informs me that "Scan this dir for additional .ini files" is empty (no value)
app.dockerfile:
FROM php:7.1.21-fpm-alpine3.8
# Clean & update package manager
RUN rm -rf /var/cache/apk/* && \
rm -rf /tmp/*
RUN apk update
# Install tools
RUN apk add --no-cache ${PHPIZE_DEPS} \
imap-dev \
openssl-dev \
zlib-dev
# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql zip
RUN docker-php-ext-configure imap --with-imap --with-imap-ssl \
&& docker-php-ext-install imap
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
# Copy configuration file(s)
COPY php-ini/my-firm.ini /usr/local/etc/php/conf.d/my-firm.ini
# Set workdir
WORKDIR /var/www
docker-compose.yml file:
version: '3'
services:
api-lumen-app:
build:
context: ./api-lumen
dockerfile: app.dockerfile
# name our image
image: my-firm/api-lumen-app-development
# set working dir
working_dir: /var/www
# bound volume for easy file sharing between host & docker container
volumes:
- ../api-lumen/:/var/www
env_file:
- .env
links:
- api-database
# can be used as DNS name to access another (external) service
container_name: api-lumen.app.my-firm.local
# environment variables
environment:
# Application
APP_ENV: ${APP_ENV}
APP_KEY: ${APP_KEY_LUMEN}
APP_DEBUG: ${APP_DEBUG}
APP_URL: ${APP_URL_LUMEN}
# Database
DB_HOST: ${DB_HOST}
DB_DATABASE: ${DB_DATABASE}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
# Caching
CACHE_DRIVER: ${CACHE_DRIVER}
# Queue
QUEUE_DRIVER: ${QUEUE_DRIVER}
# Mail general
MAIL_DRIVER: ${MAIL_DRIVER}
MAIL_ENCRYPTION: ""
MAIL_FROM_ADDRESS: ""
MAIL_FROM_NAME: ""
MAIL_HOST: ""
MAIL_PORT: ""
MAIL_USERNAME: ""
MAIL_PASSWORD: ""
# Imap
IMAP_USERNAME: ${IMAP_USERNAME}
IMAP_PASSWORD: ${IMAP_PASSWORD}
IMAP_HOST: ${IMAP_HOST}
IMAP_PORT: ${IMAP_PORT}
IMAP_ENABLE_SSL: ${IMAP_ENABLE_SSL}
IMAP_VALIDATE_CERTIFICATE: ${IMAP_VALIDATE_CERTIFICATE}
IMAP_FOLDER: ${IMAP_FOLDER}
# Mail generator
MAILGENERATOR_BASE_URI: ${MAILGENERATOR_BASE_URI}
# Exact
EXACT_BASE_URI: ${EXACT_BASE_URI}
EXACT_DIVISION: ${EXACT_DIVISION}
EXACT_CLIENT_ID: ${EXACT_CLIENT_ID}
EXACT_CLIENT_SECRET: ${EXACT_CLIENT_SECRET}
# Google
GOOGLE_MAPS_BASE_URI: ${GOOGLE_MAPS_BASE_URI}
GOOGLE_MAPS_API_KEY: ${GOOGLE_MAPS_API_KEY}
GOOGLE_MAIL_BASE_URI: ${GOOGLE_MAIL_BASE_URI}
GOOGLE_MAIL_APPLICATION_NAME: ${GOOGLE_MAIL_APPLICATION_NAME}
GOOGLE_MAIL_AUTH_CONFIG: ${GOOGLE_MAIL_AUTH_CONFIG}
GOOGLE_MAIL_ACCESS_TYPE: ${GOOGLE_MAIL_ACCESS_TYPE}
GOOGLE_MAIL_PROMPT: ${GOOGLE_MAIL_PROMPT}
# The API (Lumen) Web Server
api-lumen-web:
build:
context: ./api-lumen
dockerfile: web.dockerfile
# name our image
image: my-firm/api-lumen-web-development
# set working dir
working_dir: /var/www
# bound volume for easy file sharing between host & docker container
volumes:
- ../api-lumen/:/var/www
ports:
- 8003:80
links:
- api-lumen-app
# can be used as DNS name to access another (external) service
container_name: api-lumen.web.my-firm.local
# The API Database
api-database:
# official (public) image
image: mysql:5.6.41
# named volume for persistent storage
volumes:
- dbdata:/var/lib/mysql
env_file:
- .env
ports:
- 3306:3306
# can be used as DNS name to access another (external) service
container_name: api.database.my-firm.local
# environment variables
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
# named volumes
volumes:
dbdata:
See my next post in this question for the docker-compose log, as body is limited to 30 000 chars
EDIT:
What makes it even stranger is that I've created an additional service in my docker-compose file with a dockerfile that looks more or less the same. When I check the phpinfo file for that new service, it does load the pdo_mysql?!
app.dockerfile (new service):
FROM php:7.1.21-fpm-alpine3.8
# Clean & update package manager
RUN rm -rf /var/cache/apk/* && \
rm -rf /tmp/*
RUN apk update
# Install tools
RUN apk add --no-cache ${PHPIZE_DEPS}
# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
# Copy configuration file(s)
COPY php-ini/my-firm.ini /usr/local/etc/php/conf.d/my-firm.ini
# Set workdir
WORKDIR /var/www
I've found the issue: I am loading an .env file in my docker-compose.yml file and the values in that .env file contained double-quotes. Removing the quotes fixed the issue.

Apache exit with code 0 docker apache attempt

I currently have a problem running my apache web-server successfully while using Docker...
Here is my docker file:
FROM fedora:27
# Container Owner
MAINTAINER nzhiti#gmail.com
# Update & install Apache & clean dnf
RUN dnf upgrade -y
RUN dnf install -y httpd
RUN dnf clean packages
RUN dnf install -y mod_ssl
# Configuring hosts
ADD ./hosts/hosts /etc/hosts
# Port
EXPOSE 443
# Starting httpd
ENTRYPOINT ["/usr/sbin/httpd"] & CMD ["-D", "FOREGROUND"]
No errors during building. But when I try to compose it, it never works, and the only message outputted is apache exiting with code 0
version: '3'
services:
php-apache:
image : httpd_fedora
ports:
- 443:443
volumes:
- ./Apache/www/:/var/www/html
- ./Apache/vhosts/:/etc/httpd/conf.d/
- ./Apache/SSLcert/:/etc/httpd/ssl/
- ./Apache/errorlogs/error.log:/var/log/httpd/error.log
tty: true
I'm out of ideas...
Thanks,
DRK
Try indenting tty: true so it's aligned with php_apache properties. Also copy the Dockerfile to the same directory of docker-compose.yml, and change image to build: ..
version: '3'
services:
php-apache:
build: .
ports:
- 443:443
volumes:
- ./Apache/www/:/var/www/html
- ./Apache/vhosts/:/etc/httpd/conf.d/
- ./Apache/SSLcert/:/etc/httpd/ssl/
- ./Apache/errorlogs/error.log:/var/log/httpd/error.log
tty: true

Docker Slimframework MySQL

i'm new to docker and slimframework, i'm trying to make a docker file or docker-compose file that when i send to someone else they can run and have my sql db running with the tables already created i dont know if this is right but i get an error.
dockerfile
FROM php:7.1-apache
COPY apache2.conf /etc/apache2
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
this is my docker-compose
version: '2'
volumes:
logs:
driver: local
services:
db-mysql:
image: mysql:5
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: path
MYSQL_USER: root
MYSQL_PASSWORD: root
slim:
image: php:7-alpine
working_dir: /var/www
command: php -S 0.0.0.0:8080 -t public
environment:
docker: "true"
depends_on:
- db-mysql
ports:
- 80:8080
volumes:
- .:/var/www
- logs:/var/www/logs

php docker link apache docker

I build 2 dockers, one docker with apache, one docker with php5, and I use docker-compose to start.
apache2 Dockerfile in directoy apache2:
FROM debian:latest
RUN apt-get update && apt-get install -y apache2
ADD test.php /var/www/html
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
and test.php:
<?php
phpinfo();
?>
php5 Dorckerfile in directory php:
FROM debian:latest
RUN apt-get update && apt-get install -y php5
docker-compose.yml:
apache:
build: ./apache2
container_name: apache
ports:
- "80:80"
links:
- "php5"
php5:
build: ./php
container_name: php
then I run:
docker-compose up
apache2 server start successfully. Then I access this server by http://server_ip, then I get index of debian.But when I access http://server_ip/test.php, just occur this:
<?php
phpinfo();
?>
php just doesn't work.And I don't why.
You can separate Apache and PHP with PHP-FPM. It is however that the DocumentRoot must be mounted on both containers.
Apache must be able to access the files locally (inside its container) as well as the PHP-FPM server.
I am currently working on the same, have a look at my docker-compose.yml here
https://github.com/cytopia/devilbox/blob/master/docker-compose.yml
Both volumes (in PHP and apache) are mounted to /shared/httpd
I would say its not possible to run seperate containers for php as apache module. I guess this is what Wolfgang meant.
If you want to seperate apache and php in two different containers you need to run php as fpm.
Have a look here for inspiration: How to correctly link php-fpm and Nginx Docker containers together?
If you need to run apache and php as apache_mod use a combined container like this: https://github.com/docker-library/php/blob/fec7f537f049aafd2102202519c3ca9cb9576707/5.5/apache/Dockerfile
from: https://hub.docker.com/_/php/
If you don't specifically need to separate Apache from PHP, then you might be good to go with an official php:5.6-apache image which comes with Apache out of the box.
For example, your docker-compose.yml might look something like this:
version: '3'
services:
web:
image: php:5.6-apache
ports:
- "8080:80" # Map container port 80 to host machine port 8080
volumes:
- ".:/var/www/html" # Mount current folder as volume to container at /var/www/html
Or, for a more real-life example, if you also need at least one of the following:
A custom web root (for Laravel, Symfony, etc)
Other Apache modules installed
Other PHP extensions installed
You might do something more like this:
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80" # Map container port 80 to host machine port 8080
environment:
APACHE_DOCUMENT_ROOT: "/var/www/yourapp.com/public"
volumes:
- ".:/var/www/yourapp.com" # Mount current folder as volume to container at /var/www/yourapp.com
And then your Dockerfile (which we reference from the docker-compose.yml above):
FROM php:5.6-apache
# Declare an environment variable with a default value for changing Apache's document root
# We will override this in docker-compose.yml
ENV APACHE_DOCUMENT_ROOT /var/www/html
# Configure web root
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Install additional Apache modules
# This example: mod_rewrite & mod_headers
RUN a2enmod rewrite headers
# Install additional PHP extensions
# This example: memcached & mysqli
# For other extensions see official docs:
# https://hub.docker.com/_/php (section: How to install more PHP extensions)
RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \
&& pecl install memcached-2.2.0 \
&& docker-php-ext-enable memcached \
&& docker-php-ext-install -j$(nproc) mysqli

Categories