skip to Main Content

I need an example of setting up MySQL and phpmyadmin on a docker compose file.

2

Answers


  1. Chosen as BEST ANSWER

    Added mysqlclient dependency into requirements.txt. required to allow Django to connect with MySQL.

    In the Dockerfile, added the following into the Dockerfile:

    Dockerfile

    FROM python:3.7
    
    
    ENV TZ=Africa/Nairobi
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    
    
    ENV PYTHONUNBUFFERED 1
    
    # add requirements.txt to the image
    ADD requirements.txt /app/
    
    # set working directory to /app/
    WORKDIR /app/
    
    
    RUN pip install --upgrade pip
    
    
    RUN pip install -r requirements.txt
    


    Set up MYSQL configurations on django settings.py:

    settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql', 
            'NAME': 'django_mysql_docker',
            'USER': 'root',
            'PASSWORD': 'mysqladmin',
            'HOST': 'db',
            'PORT': '3306',
        }
    }
    

    I added the following in the **docker-compose** file:


    docker-compose.yml

      db:
        restart: always
        image: mysql:5.7
        command: --default-authentication-plugin=mysql_native_password
        volumes:
          # - ./init-db:/docker-entrypoint-initdb.d
          - ./data-db:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: mysqladmin
          MYSQL_DATABASE: django_mysql_docker
        ports:
          - "3306:3306"
    
    
      phpmyadmin:
        image: phpmyadmin/phpmyadmin
        environment:
          MYSQL_USERNAME: 'root'
          MYSQL_ROOT_PASSWORD: 'mysqladmin'
        restart: always
        ports:
          - 8082:80
        volumes:
          - /sessions
    

    On the port :8082 (http://0.0.0.0:8082/) you will find phpmyadmin:


    phpMyAdmin Dashboard

    enter image description here


  2. you have to setup username and password under environment in docker-compose.yml file.

    version: '3'
    services:
      db:
        image: mysql
        restart: always
        environment:
          MYSQL_DATABASE: 'db'
          MYSQL_USER: 'user'
          MYSQL_PASSWORD: 'password'
          MYSQL_ROOT_PASSWORD: 'password'
        ports:
          - '3306:3306'
        volumes:
          - my-db:/var/lib/mysql
    volumes:
      my-db:
    

    and for phpmyadmin, I had used a phpmyadmin image from docker hub:

    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      ports:
        - 80:80
      environment:
        MYSQL_USERNAME: 'user'
        MYSQL_ROOT_PASSWORD: 'password'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search