Cannot log in through phpmyadmin to mysql in my container

I’m trying to have one docker container with: ubuntu 22.04, on top of which I add apache, php, mysql and phpmyadmin all on the same container (it must be in one Container)

This is my Dockerfile:

FROM ubuntu:22.04


USER root


ENV DEBIAN_FRONTEND=noninteractive


RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    libapache2-mod-php \
    php-mysql \
    mysql-server \
    phpmyadmin \
    && apt-get clean


RUN chown -R www-data:www-data /var/www/html /var/log/apache2 /var/run/apache2


RUN a2enmod php8.1


RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf


RUN service mysql start && \
    mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES;"


RUN echo "Include /etc/phpmyadmin/apache.conf" >> /etc/apache2/apache2.conf


COPY my.cnf /etc/mysql/my.cnf
COPY admin/config-db.php /etc/phpmyadmin/config-db.php
COPY admin/config.inc.php /etc/phpmyadmin/config.inc.php


RUN echo "<?php phpinfo(); ?>" > /var/www/html/index.php


ENV HOME=/home/ubuntu
WORKDIR $HOME
RUN mkdir -p $HOME && chown -R www-data:www-data $HOME


COPY start.sh /start.sh
RUN chmod +x /start.sh


USER www-data


ENTRYPOINT ["/start.sh"]

This is my start.sh:

#!/bin/bash

# MySQL and Apache starting
service mysql start
service apache2 start

# keep Container running
tail -f /dev/null

I can log into Mysql in the container via the console

But as soon as I try to log in to phpmyadmin via the web, the error appears

mysqli::real_connect(): (HY000/2002): Permission denied

myadmin

I’ve already tried everything possible without success, I would be very grateful for a tip

Greetings Mario

Sorry to tell you, but this is totally not what containers are made for. It’s not a VM. Containers are for isolation.

Also note that this is totally not best practice to use apt upgrade inside a container, as it will prevent any reproducible build. Every time you build, a library might have changed, so every resulting image will be different, hard to find any errors if something is wrong with dependencies.

The “right way” would be do use a docker-compose.yml file with docker compose, use the standard containers for each service. Docker DNS enables them to talk to each other via their service name.

“Permission denied” should mean that the user/pass is incorrect, it does not seem to be a network issue.

Thanks for your answer though

sorry, why shouldn’t a container be made for something like that?

Yes, normally I would use 3 containers for something
like this:

services:
  #webserver
  apache:
    image: webdevops/apache:latest
    container_name: "${IMAGE_NAME_APACHE}"
    environment:
      - WEB_PHP_SOCKET=php:9000
    volumes:
      - /home/torsten/php/src:/app
    ports:
      - 80:80
    
  #php
  php:
    build: ./php
    container_name: "${IMAGE_NAME_PHP}"
    extra_hosts:
      - host.docker.internal:host-gateway
    #environment:
      #XDEBUG_MODE: debug
      #XDEBUG_CONFIG: client_host=host.docker.internal client_port=9003
    volumes:
      - /home/torsten/php/src:/app

  # Database
  db:
   platform: linux/x86_64
   image: mysql:5.7
   container_name: "mysql"
   volumes:
     - db_data:/var/lib/mysql
   restart: always
   ports:
      - "3306:3306"
   environment:
     MYSQL_ROOT_PASSWORD: password
     MYSQL_DATABASE: devdb
     MYSQL_PASSWORD: password
     MYSQL_USER: devuser


   #myadmin
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - "8077:80"
    environment:
        - PMA_HOST=db
    restart: always
    depends_on:
        - db


volumes:
  db_data:
    name: ${VOL_NAME}

Unfortunately, phpmyadmin has to be in the same container as mysql
And it is not a user problem since the user works fine in bash and also in the application with mysqli()

It must be due to the control from phpmyadmin

now it works fine with php adminer:

# offizielle Ubuntu 22.04 LTS Image
FROM ubuntu:22.04


USER root


ENV DEBIAN_FRONTEND=noninteractive

# Aktualisieren der Paketliste und installieren von Apache, PHP und MySQL
RUN apt-get update && apt-get install -y \
    apache2 \
    php \
    libapache2-mod-php \
    php-mysql \
    mysql-server \
    && apt-get clean

# Installieren von Adminer
COPY adminer/adminer.php /var/www/html/adminer/index.php
# Setzen derBerechtigungen für das Web-Verzeichnis und die Logdateien

RUN chown -R www-data:www-data /var/www/html /var/log/apache2 /var/run/apache2 /var/www/html/adminer

# Apache-PHP-Modul aktivieren
RUN a2enmod php8.1

# Setzen des ServerName in der Apache-Konfiguration
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf


# Kopiere die MySQL-Initialisierungsdatei
COPY init.sql /docker-entrypoint-initdb.d/


# MySQL-Dienst starten und Root-Passwort setzen
RUN service mysql start && \
    mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES;"


# Konfigurieren von Apache, um Adminer zu verwenden
RUN echo "<Directory /var/www/adminer>" >> /etc/apache2/apache2.conf
RUN echo "    AllowOverride All" >> /etc/apache2/apache2.conf
RUN echo "</Directory>" >> /etc/apache2/apache2.conf

# Datei von lokalem System in das Image kopieren
COPY my.cnf /etc/mysql/my.cnf


# Ein einfaches PHP-Skript zum Testen erstellen
RUN echo "<?php phpinfo(); ?>" > /var/www/html/index.php

# Benutzer und Startverzeichnis einstellen
ENV HOME=/home/ubuntu
WORKDIR $HOME
RUN mkdir -p $HOME && chown -R www-data:www-data $HOME

# Startskript für Apache und MySQL erstellen
COPY start.sh /start.sh
RUN chmod +x /start.sh

# Standardbenutzer ändern
USER www-data

# Startskript als Entrypoint festlegen
ENTRYPOINT ["/start.sh"]

but as @bluepuma77 said putting everything in one container is not the best way

Greetings

Have you actually tried what the image description on Dockerhub says?

Usage with external server

You can specify a MySQL host in the PMA_HOST environment variable. You can also use PMA_PORT to specify the port of the server in case it’s not the default one:

docker run --name myadmin -d -e PMA_HOST=dbhost -p 8080:80 phpmyadmin
1 Like

Thanks for the tip, I think I’ve already tried it, with two images it works great with “PMA_HOST=dbhost” but with one image i.e. mysql and phpmyadmin in one image it didn’t work, but I’ll try it again just to be on the safe side