0

I'm learning Docker so I tried to dockerize an old proyect I made a little ago, it is a Symfony 5 app, its like a shop app so it uses a mysql database.

I have the following docker compose

version: '3.7'
services:
  php:
    build: 
        context: .
        dockerfile: docker/build/php/Dockerfile
    ports: 
        - "9090:80"

  mysql:
    image: mysql:5.7
    environment: 
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root}

Now, inside docker/build/php/Dockerfile

FROM php:7.3.3-apache

COPY . /var/www/html

When I build the image and then run docker-compose up it all seems to work fine, when I go to localhost:9090 I get an error about permissions so I run chmod -R 777 var inside the php-apache container to fix it, I refresh the page then I got An exception occurred in driver: could not find driver

Searching on google I found several solutions that says something like I need to run this command to fix it

sudo apt-get install php7.2-mysql

When I run that this happens

E: Unable to locate package php7.2-mysql

E: Couldn't find any package by glob 'php7.2-mysql'

E: Couldn't find any package by regex 'php7.2-mysql'

Or this one

apt-get install php-mysql

This throws this error

Package 'php-mysql' has no installation candidate

Any other command about installing something throws an error like one of the two from above, how can I fix this driver error?

There are some info about my container

root@a15f68bd3605:/# cat /etc/*-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@a15f68bd3605:/# uname -a
Linux a15f68bd3605 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 GNU/Linux

This is my .env from the symfony project

DATABASE_URL=mysql://root:@127.0.0.1:3306/popcollector?serverVersion=5.7
Community
  • 1
  • 1
H3lltronik
  • 562
  • 8
  • 26
  • What is your DATABASE_URL in .env file? Just change your confidential info before posting. – Alexander Dimitrov Jun 07 '20 at 11:06
  • Does this answer your question? [Docker Laravel Mysql: could not find driver](https://stackoverflow.com/questions/56759646/docker-laravel-mysql-could-not-find-driver) – Leprechaun Jun 07 '20 at 19:05
  • Hi @AlexanderDimitrov I just edited my question – H3lltronik Jun 07 '20 at 19:54
  • @Leprechaun Thx for ur comment, I already tried that, it didn't worked for me – H3lltronik Jun 07 '20 at 19:55
  • I just noticed, the DATABASE_URL must be changed to work inside a container enviroment, right? I must know what the mysql container ip is to change my .env before building the docker compose, is that right? is has nothing to do with the driver not found error since if my symfony project could make a connection then it would throw another error. This will eventually will cause me another error – H3lltronik Jun 07 '20 at 20:18
  • 2
    @H3lltronik you should replace 127.0.0.1 with mysql. 127.0.0.1 refers to the php container and you don’t have the dB engine there. – Alexander Dimitrov Jun 08 '20 at 04:22

2 Answers2

7

You need to enable the pdo_mysql extension in the container. You can do that by adding this line to your Dockerfile:

RUN docker-php-ext-install pdo_mysql
BenjaminH
  • 324
  • 3
  • 13
-2

in general it is a bad practice to install software inside a container afterwards. make sure to use the correct container in first place.

you can use the webdevops container for example. To use that, simply replace the line FROM php:7.3.3-apache in your Dockerfile with FROM webdevops/php:debian-8-php7

More info about the webdevops container on https://github.com/webdevops/Dockerfile

StFroyd
  • 53
  • 4