Learning docker and am struggling with a php server with a mysql and PHPMyAdmin

hey there im learning docker currently and trying to set up a simple testing infrastructure with docker and cant seem to get it to work.
I have problems connecting to my mysql server with my phpmyadmin and with my php server.
Can someone explain what i need to do to make it work i found like 300 things online and none of them worked and now i got many tips mixed up.

version: '3.7'

volumes:
    mariadb_data:
        driver: local
    logs:
        driver: local

services:
    api:
        image: php:8-alpine
        container_name: api
        working_dir: /var/www
        command: php -S 0.0.0.0:8080
        environment:
            docker: "true"
        ports:
            - "8080:8080"
        volumes:
            - .:/var/www
            - ./logs:/var/www/logs
        networks:
            - web
        depends_on:
            - mysql
        restart: always

    mysql:
        container_name: mysql
        hostname: mysql
        image: mariadb:latest
        restart: always
        networks:
            - web
        ports:
            - '3306:3306'
        expose:
            - '3306'
        command: --init-file /data/application/init.sql
        environment:
            MYSQL_DATABASE: 'mydb'
            # So you don't have to use root, but you can if you like
            MYSQL_USER: 'user'
            # You can use whatever password you like
            MYSQL_PASSWORD: '12345678'
            # Password for root access
            MYSQL_ROOT_PASSWORD: '12345678'
            MYSQL_TCP_PORT: 3306
        volumes:
            - ./mariadb_data:/var/lib/mysql
            - ./init.sql:/data/application/init.sql
    phpmyadmin:
        image: phpmyadmin:5.2.2
        restart: always
        ports:
            - 9090:80
        networks:
            - web
        environment:
            MYSQL_DATABASE: 'mydb'
            MYSQL_USER: user
            MYSQL_PASSWORD: '12345678'
            MYSQL_ROOT_PASSWORD: '12345678'
        links:
            - mysql:mysql
networks:
    web:
        # Specify driver options
        driver: bridge

You probably saw an old guide. “links” is not necessary and the version line at the beginning is also ignored by currently supported docker compose. You can delete those from the compose file, but I don’t see how you try to connect to MariaDB or what your issue is with it. Please share any error message you have nd how you try to connect to MariaDB from PHP and PHPMyAdmin..


Like in this image all is running but i cant connect to the db with phpmyadmin.
Via console inside the mysql server it is working

The error message says it tries to connect to the “db” host but there is no such host as you call the compose service mysql not db. You could rename the compose service in the compose file (after first deleting the mysql service using docker compose rm), but the best if you find where the hostname is set and set the right hostname. If you check the image description (https://hub.docker.com/_/phpmyadmin) you can find the PMA_HOST environment variable.

PMA_HOST - define address/host name of the MySQL server

And now I know why you use links. The description still shows the old way.

2 Likes

Thx man. You really really helped me :grinning_face_with_smiling_eyes:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.